I
Izzy
I'm writting an app that has two DataGridViews, in the first grid I
load a list on companies. Between the 2 grids are two buttons with
arrows, one points to the right the other to the left.
When the user selects rows from grid 1 (on the left) and clicks the
arrow pointing to the right, it moves the rows to the other
DataGridView.
The user is basically selecting companies to print out invoices for.
The grid on the right is the list of "selected" companies to process.
Here is the issue I'm having.
At first I was using a SqlDataReader object to get a list of companies
and adding the results to the DataGridView like so:
While Reader.Read
Values(0) = Reader(0)
Values(1) = Reader(1)
dgv_Companies.Rows.Add(Values)
End While
The issue here is when the user selects a bunch of rows from grid 1
and I use this code to move them to grid 2:
Private Sub MoveDataGridViewRows(ByVal Source As DataGridView, ByVal
Destination As DataGridView)
If Source.Rows.Count = 0 Then Exit Sub
If Source.SelectedRows.Count = 0 Then Exit Sub
Dim SelectedRow As DataGridViewRow
Dim ColumnCount As Int32 = Source.Columns.Count - 1
Dim Values(ColumnCount) As String
Dim i As Int32
For Each SelectedRow In Source.SelectedRows
For i = 0 To ColumnCount
Values(i) = SelectedRow.Cells(i).Value
Next
Destination.Rows.Add(Values)
Source.Rows.Remove(SelectedRow)
Next
Destination.Sort(Destination.Columns(0),
System.ComponentModel.ListSortDirection.Ascending)
End Sub
It only moves the rows at a rate of about 2 or 3 per second, so when
2200 rows are selected....it takes a while.
So I thought I would bind the DataGridViews to DataTables and use
ADO.NET and hopefully move rows quicker. The issues so far have been:
1) Data needs to be organized in ascending order by column1
(CompanyName), when rows get added to the "selected companies" table
they are not in order, they get appended to the bottom of the
datatable.
2) Using the DefaultView of the DataTable doesn't work too good for
ordering the rows because it only reorders the VIEW of the table and
not the physical order of the table. So when I do
DataGridView.SelectedRows() the index of the selected rows in the
datagridview control is different then the index of the actual rows in
the datatable. That's a little confusing but if you've had experience
with this you'll know what I'm talking about.
So I'd like to get some ideas from anyone on how to accomplish this
task with performance in mind. I really don't mind using different
controls if that is what is necessary, just keep in mind these
requirements:
1) The data in both grids needs to be kept in ascending order by
CompanyName. So when a row get moved from either side it needs to be
placed in the right location in the grid.
2) Users need the ability to select multiple rows and move them all at
once.
Please only people who are serious about sharing ideas need to
respond!
Thanks for your help!
Izzy
load a list on companies. Between the 2 grids are two buttons with
arrows, one points to the right the other to the left.
When the user selects rows from grid 1 (on the left) and clicks the
arrow pointing to the right, it moves the rows to the other
DataGridView.
The user is basically selecting companies to print out invoices for.
The grid on the right is the list of "selected" companies to process.
Here is the issue I'm having.
At first I was using a SqlDataReader object to get a list of companies
and adding the results to the DataGridView like so:
While Reader.Read
Values(0) = Reader(0)
Values(1) = Reader(1)
dgv_Companies.Rows.Add(Values)
End While
The issue here is when the user selects a bunch of rows from grid 1
and I use this code to move them to grid 2:
Private Sub MoveDataGridViewRows(ByVal Source As DataGridView, ByVal
Destination As DataGridView)
If Source.Rows.Count = 0 Then Exit Sub
If Source.SelectedRows.Count = 0 Then Exit Sub
Dim SelectedRow As DataGridViewRow
Dim ColumnCount As Int32 = Source.Columns.Count - 1
Dim Values(ColumnCount) As String
Dim i As Int32
For Each SelectedRow In Source.SelectedRows
For i = 0 To ColumnCount
Values(i) = SelectedRow.Cells(i).Value
Next
Destination.Rows.Add(Values)
Source.Rows.Remove(SelectedRow)
Next
Destination.Sort(Destination.Columns(0),
System.ComponentModel.ListSortDirection.Ascending)
End Sub
It only moves the rows at a rate of about 2 or 3 per second, so when
2200 rows are selected....it takes a while.
So I thought I would bind the DataGridViews to DataTables and use
ADO.NET and hopefully move rows quicker. The issues so far have been:
1) Data needs to be organized in ascending order by column1
(CompanyName), when rows get added to the "selected companies" table
they are not in order, they get appended to the bottom of the
datatable.
2) Using the DefaultView of the DataTable doesn't work too good for
ordering the rows because it only reorders the VIEW of the table and
not the physical order of the table. So when I do
DataGridView.SelectedRows() the index of the selected rows in the
datagridview control is different then the index of the actual rows in
the datatable. That's a little confusing but if you've had experience
with this you'll know what I'm talking about.
So I'd like to get some ideas from anyone on how to accomplish this
task with performance in mind. I really don't mind using different
controls if that is what is necessary, just keep in mind these
requirements:
1) The data in both grids needs to be kept in ascending order by
CompanyName. So when a row get moved from either side it needs to be
placed in the right location in the grid.
2) Users need the ability to select multiple rows and move them all at
once.
Please only people who are serious about sharing ideas need to
respond!
Thanks for your help!
Izzy