DataGridView DataSets and Sorting

ZeroEffect

Well-known member
Joined
Oct 24, 2004
Messages
180
Location
Detroit, MI
I know if I use the following line that my data set will be sorted by the column I want.

Code:
dsEvents.Tables("Events").DefaultView.Sort = "RunTime"

Here is how I am getting the data

Code:
                tbEventName.Text = dsEvents.Tables("Events").Rows(DataGridView.CurrentRow.Index).Item("EventName")
                tbCommand.Text = dsEvents.Tables("Events").Rows(DataGridView.CurrentRow.Index).Item("Command")
                dtpTime.Text = dsEvents.Tables("Events").Rows(DataGridView.CurrentRow.Index).Item("RunTime")
                cmbBox.Text = dsEvents.Tables("Events").Rows(DataGridView.CurrentRow.Index).Item("ComPort")

                Days(dsEvents.Tables("Events").Rows(DataGridView.CurrentRow.Index).Item("Days"))

Then the DatagridView Displays the data correctly. If the user changes the sort in the DataGridView the Data I want to retrieve from the DataGridView is messed up. So I have two questions.

1. How can I sort my DataSet when the sort changes on the DataGridView

2. How do I disable the sort on the DataGridView

Thanks

ZeroEffect
 
Last edited by a moderator:
I dont know for the first question, but for the second this might help:

Code:
  For i As Integer = 0 To datagridview.Columns.Count - 1
                datagridview.Columns(i).SortMode = DataGridViewColumnSortMode.NotSortable
   Next
 
Thanks PUIU that helped.

Now I have figured out how to get data from a DataGridView row.

Code:
DataGridView.Rows(DataGridView.CurrentRow.Index).Cells(i).Value
i is the number of the column starting with 0

So this is good I have my data loaded into the DGV and sorted the way I want. Now I have a new issue. When I go to update a row in the Dataset the wrong row is updating. I have tried resorting by ID which should put the DS back into it orignal order. Then sort back after the changes have been made. But if I have the DS sorted by a different column then loaded shouldnt that change the row index to?

example

rows before sort are

4 (index 0)
1
3
2 (index 3)

After sort

1 (index 0)
2
3
4 (index 3)

After my sort the orignal index number seems to follow the rows. Is this how this works or am I missing something?

Thanks for any help you may provide.

ZeroEffect
 
Back
Top