Click here to Skip to main content
15,891,248 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

How to change the order of datagrid columns? One think I would like to mention here is it's datagrid not the datagridview. So DisplayIndex property is not available which is available in datagridview. And AutoGeneratedColumns is set to false. And I don't want to set it to true(as there are limitations to use this).

Thanks,
Surendra
Posted
Comments
BulletVictim 7-Oct-13 5:08am    
Where would you like to change the order of the columns? On run time with drag and drop? or just the initial order when it is populated?

Are you selecting the data for the datagrid from a database? Assuming you do you could just change the select statement.
Surendra Patil 7-Oct-13 5:45am    
Hi BulletVictim,
Here is the scenario,

I am populating the data from the database. I want have the functionality where user is capable of selecting the columns which user want to see and change the order according to his context/preference. I am able to hide /unhide the column according to the user selection. But I am not able to change the order.

Example:

Let's say, I have Column1,Column2, Column3, Column4, Column5 from the database and I have some icons like 'View', 'Print' in one column which will be fixed at left most side. Now user want to see Column1 Column2 Column3 with tools in the order Column3 Column1 Column2. I can't set the autogeneratecolumns to true. So what should be done on this?
BulletVictim 7-Oct-13 5:55am    
Do you currently have any type of way for the user to select in which order they want it?
Because you say you cant set autogeneratecolumns to true, I keep on thinking that you will need to look at the select statement. Now that being said, with the ordering from the user in place perhaps using that order to write the select statement?
Surendra Patil 7-Oct-13 6:10am    
I have a view which populates the data. I have provided the modalpopup with datagrid where user will select his context.

I can's set autogeneratecolumns to true bcaz there are some tools like print option, view option which are bounded to the one column. Which will be fixed always. And for the printer option they have used image of the print. So I don't think i can bound those images at code behind.

What i was thinking initially, bound the all the columns to the datagrid and then hide/unhide or change the order by some property of datagrid. Means there are some properties like 'DisplayIndex' in datagridview where we can select the index of the column.
BulletVictim 7-Oct-13 6:37am    
Currently how do the user select the columns to display? Is it with checkboxes or comboboxes?
I'm thinking use comboboxes in a set order, listing all the columns that could be displayed.
Then use your select statement with the comboboxes selected item.

For instance. Have the comboboxes populated with column 1 until column x
Have a combobox for each column.
Your select statement is then something like, select combobox1.seleectedItem.Tostring(), combobox2.seleectedItem.Tostring() FROM Table

1 solution

 
Share this answer
 
Comments
Surendra Patil 7-Oct-13 6:17am    
Hey Saroj, I can't use this. Because I am using datagrid not datagridview.
BulletVictim 7-Oct-13 8:39am    
Please Remove this solution as it is not correct pertaining to Surendra's question so that it can be added back to the unanswered questions to help him resolve this issue

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900