Hi
I have a datagridview. I set the allowusertoordercolumns property to true. I can reorder the columns as per my need but cannot save the new order.
How can i save the new order of columns.
Thanks in advance | | rowter Thursday, July 30, 2009 7:22 PM | Hello rowter, When youtry toset DisplayIndex property of datagridviewColumn, it adjust/change other columns displayIndex to reflect the new order. Supposed you changed 5 columns displayindex, now youare changing column 6th, itmay alterDisplayIndexthose 5 columns. So to avoid this, always set DisplayIndex from 0 and proceed in numerical order without skipping any values.
In your case, before youset DisplayIndex , order DisplayIndexin ascending order. You can use SortedList<int /* DisplayIndex*/,int/* Index of DataGridviewColumn*/>, first store all the displayIndex in this sortedList , and then you will know which column index ishaving 0 as display index. Likewise set all columnsdisplayIndex . hope this will solve your problem.
- Marked As Answer byAland LiMSFT, ModeratorFriday, August 07, 2009 12:25 PM
-
| | NareshG Monday, August 03, 2009 9:18 AM | For saving order of columns , you have to save it somewhere, like database or file. You can use any collection to save it, Try to use name value collection. Name is column name and value is DisplayIndex. Before you set display index, make sure you start it from 0 and then in increament order, means sort on display index. | | NareshG Thursday, July 30, 2009 7:36 PM | Hi NareshG
Thanks for the info. I tried a different way using isolatedstoragefile/isolatedstoragefilestream . It is erroring out at a loop. can you tell me why it is going wrong? If this does not work out i have to try your way.
Private Sub SetDisplayOrder()
Dim isofile As IsolatedStorageFile = IsolatedStorageFile.GetUserStoreForAssembly() Dim fileNames() As String = isofile.GetFileNames("*") Dim found As Boolean = False
For Each fileName As String In fileNames If fileName = "DisplayCache" Then found = True End If Next If Not found Then Return End If Dim isostream As IsolatedStorageFileStream = New IsolatedStorageFileStream("DisplayCache", IO.FileMode.Open, isofile) Try Dim ser As New XmlSerializer(GetType(Integer())) Dim displayIndicies As Integer() = DirectCast(ser.Deserialize(isoStream), Integer()) ' For i As Integer = 0 To displayIndicies.Length - 1
For i As Integer = 0 To displayIndicies.Count MessageBox.Show(i) Dim num As Integer = displayIndicies(i) dgvSchedules.Columns(i).DisplayIndex = num ****** Skipping out of loop here after 1 iteration
Dim nm As String = dgvSchedules.Columns(i).Name MessageBox.Show(nm) 'i = i + 1
Next 'While i <= displayIndicies.Count - 1 'End While 'MessageBox.Show("Exit?") Catch End Try isostream.Dispose()
End Sub - Unmarked As Answer byAland LiMSFT, ModeratorFriday, August 07, 2009 12:25 PM
- Marked As Answer byAland LiMSFT, ModeratorFriday, August 07, 2009 12:25 PM
-
| | rowter Thursday, July 30, 2009 10:20 PM | Hi rowter,
Based on the code snippet, we need to check the items below to diagnose the issue:
1. displayIndicies.Count: We need to make sure this count is larger than 1.
2. dgvSchedules.Columns(i).DisplayIndex = num: We need to make sure i is in a legal column index. In other words, i is between 0 and dgvSchedules.Count-1.
From my experience, we can create a DataTable and copy data from DataGridView to save the data. We can create a empty DataTable and for each column in DataGridView, we can add a column of the same name and type to the DataTable. Then we can traverse the rows of the DataGridView and add rows to the DataTable. When all these done, we can save the data from DataTable to database or save the data to a xml file. We can define a DataAdapter and call the Update method to update the data to database. We can call the WriteXml method of the DataTable to save the data to a xml file.
Let me know if this helps. Aland Li
Please mark the replies as answers if they help and unmark if they don't. This can be beneficial to other community members reading the thread. | | Aland Li Monday, August 03, 2009 8:49 AM | Hello rowter, When youtry toset DisplayIndex property of datagridviewColumn, it adjust/change other columns displayIndex to reflect the new order. Supposed you changed 5 columns displayindex, now youare changing column 6th, itmay alterDisplayIndexthose 5 columns. So to avoid this, always set DisplayIndex from 0 and proceed in numerical order without skipping any values.
In your case, before youset DisplayIndex , order DisplayIndexin ascending order. You can use SortedList<int /* DisplayIndex*/,int/* Index of DataGridviewColumn*/>, first store all the displayIndex in this sortedList , and then you will know which column index ishaving 0 as display index. Likewise set all columnsdisplayIndex . hope this will solve your problem.
- Marked As Answer byAland LiMSFT, ModeratorFriday, August 07, 2009 12:25 PM
-
| | NareshG Monday, August 03, 2009 9:18 AM | Aland li, Naresh,
I was working on other tasks and did not get back to that issue.
I will look in to your suggestions and let you know when it works.
Thanks | | rowter Monday, August 10, 2009 4:39 AM |
|