Reputation: 1681
I cannot understand what is happening here:
void FindRecord(string pRecordID){
dgv_cusData.ClearSelection();
for(int i=0;i < dgv_cusData.Rows.Count;i++){
if (dgv_cusData.Rows[i].Cells["recordid"].Value.ToString()==pRecordID){
dgv_cusData.CurrentCell = dgv_cusData.Rows[i].Cells[1];
dgv_cusData.Rows[i].Selected = true;
}
}
}
pRecordID is a unique column in the DGV. I only have three records in the grid and I know that the selected record is in position 2; however then the SelectionChanged event fires:
void Dgv_cusDataSelectionChanged(object sender, EventArgs e)
{
MessageBox.Show(dgv_cusData.CurrentCell.RowIndex.ToString()); // <---- Returns 0;
lbl_DetailsLabel.Text = "Details For "+dgv_cusData.CurrentRow.Cells[1].Value;
}
The CurrentCell.RowIndex always returns 0. Am I missing something while trying to select a current row in the DGV? Any help would be appreciated. Cheers!
Upvotes: 1
Views: 246
Reputation: 647
I wonder if selecting the row is was causes you to lose the CurrentCell.
Does the row highlight in the datagridview?
Do you allow multiple row selections on the grid. If not I think you would get better results with the following:
MessageBox.Show(dgv_cusData.SelectedRows[0].Index.ToString());
And:
lbl_DetailsLabel.Text = "Details For "+ dgv_cusData.Rows(dgv_cusData.SelectedRows(0).Index).Cells[1].Value;
Upvotes: 1