c# - Using "row.Selected = true" changes appearence but not CurrentRow index -
I have a snippet of code that searches for grids and records for the desired record:
foreach (grddActiveJobs.Rows in DataGridViewRow) {if (line.kel ["part number"]. Value.ToString () == lvPartNum & row.Cells ["serial number"]. Value .toString () == lvSerialNum) row.Selected = true; }
works as the above code - The row is highlighted, however, if I run an investigation on currentrow.index
If (grdActiveJobs.ContentRow.Index == 0) {// dots}
This will show the current index as 0, even if the highlight of the highlighted by the previous snippet is indicative of row 5 .
My question is, when "row.selected = true" is set, how can I ensure that the existing line index for the grid is set correctly and not known as 0 is?
Thank you in advance.
After some excavation I found this explanation:
GridName. CurrentRow.Index - Returns the indicator of the current cell defined by the system, which is not always the same as the selected cell. An example of this would be if you have a grid that allows you to select multiple rows, if you have selected 10 and then unselect one of the rows, then your current row The index will index the last line you clicked - now unchecked one.
To return a pointer to the currently selected row (the user gets highlighted), you get the gridname The selected line must use [0]. Index , if you have only one row selected, it will return the indicator of the selected row (Increasing the [0] in the previous snippet will clearly return the index to the second selected row, if multi-select is enabled ).
My code now reads:
if (GrdActiveJobs.SelectedRows [0]. Index == 0) {// Content correctly}
< / Pre>If you can improve your explanation comment and tell me :)
Comments
Post a Comment