The best list to order when ordering

I am expanding my question here Dynamically creating a GridView and trying a different approach as suggested, but I'm stuck again.

I have a table that processes my names and order of key values.

KeyNames: ID Int primary key KeyName1 varchar(20) KeyOrder int KeyName2 varchar(20) KeyOrder int ... KeyNameN varchar(20) KeyOrder int 

These Keys represent the Name and Order of another table to be displayed in the gridview. i.e:

  DocumentTable: ID int primary key PDF_Folder varchar(30) Key1value varchar(100) Key2value varchar(100) .. KeyNvalue varchar(100) 

Thus, in my gridview, the user can define the column names of the Keys and the value (I could call Key1 "description" order 1 and Key2 "Named names" and order 2.

if Order <0, then it will not be displayed in the gridview.

I'm stuck on how I create a gridview to display each column for KeyNValue with the name colmn KeyNameN and the order of KeyOrderN ..

+4
source share
2 answers

You can use the DisplayIndex property of a DataGridViewColumn to set the order. You must use the HeaderText property to set the name. I see that you have no rows, so there will only be one row with many columns in the grid.

So, if you have a grid named MyGrid, each column is in a class called MyEntityRecord, you put all the columns in the list and want to display the data after the user clicks the button, here is an example code for calling from the Click event.

 void ShowCustomGrid(List<MyEntityRecord> aList) { MyGrid.Columns.Clear(); MyGrid.Rows.Clear(); for(int loop=0;loop<aList.Count;loop++) { MyGrid.Columns.Add(new DataGridViewColumn(new DataGridViewTextBoxCell())); MyGrid.Columns[loop].Text=aList[loop].KeyName; if(aList[loop].KeyOrder<0) MyGrid.Columns[loop].Visible=false; } //Now that all columns have been added, change the display index for(int loop=0;loop<aList.Count;loop++) { if(aList[loop].KeyOrder>=0) MyGrid.Columns[loop].DisplayIndex=aList[loop].KeyOrder; } //Finally, put the values MyGrid.Rows.Add(); for(int loop=0;loop<aList.Count;loop++) { if(aList[loop].KeyOrder>=0) MyGrid.Rows[0].Cells[loop].Value=aList[loop].KeyValue; } } 

If I understood correctly, this should do what you wanted.

+1
source

crack table script:
CREATE TABLE [dbo]. [KeyNames] (
[ID] [int] NOT NULL,
[KeyName] [varchar] (50) NOT NULL,
[keyOrder] [int] NOT NULL
) ON [PRIMARY]


CREATE TABLE [dbo]. [DocumentTable] (
[ID] [int] NOT NULL,
[PDF_Folder] [varchar] (50) NOT NULL,
[Key1Value] [varchar] (100) NOT NULL,
[Key2Value] [varchar] (100) NOT NULL,
[Key3Value] [varchar] (100) NOT NULL
) ON [PRIMARY]

=============== Key name table: strings ========
1 PDF_Folder 2
2 Key1Value 3
3 Key3Value 5
4 Key2Value 4
5 ID 1
=================================================== ====================================

================ Document table: rows ========
1 aa a1 a2 a3
2 bbb b1 b2 b3
3 ccc c1 c2 c3
=================================================== ====================================


the request according to below generates a dynamic order request:

DECLARE @str_column VARCHAR (100);
DECLARE @query VARCHAR (100);
SELECT @str_column = COALESCE (@str_column + ',', '') + KeyName
FROM Keynames sort by subscription;
set @query = 'select' + @str_column + 'from DocumentTable';
Exec (@query);

0
source

All Articles