Controlling a datagridview selection range with Textboxes

J33C316

New member
Joined
Jul 29, 2021
Messages
1
Programming Experience
Beginner
I have two textboxes and a datagridview. One textbox finds and selects the first row of the textbox value in the dataviewgrid and the other textbox selects another row with the value that matches it. What I want is to select the rows with the values in the textboxes and the rows between them selected as well. I appreciate any help. Thanks.
 
Assuming that there are no other rows selected, you simply get the indexes of those two rows and then run a for loop between them to select all the rows in that index range. I'm writing this off the top of my head but it should be pretty close to what you need:
C#:
var selectedRows = dataGridView1.SelectedRows;
var startIndex = selectedRows.Min(dgvr = dgvr.Index);
var endIndex = selectedRows.Max(dgvr = dgvr.Index);

for (var i = startIndex; i <= endIndex; i++)
{
    dataGridView1.Rows[i].Selected = true;
}
That code assumes that you have already selected the two rows you mentioned. If you haven't, get their indexes some other way.
 
Moving to WinForms...
 
Back
Top Bottom