I have a DataGridView linked to a ContextMenuStrip. The DataGridView is populated. I select a row (any cell) and when I right click, it opens a menu, where I can choose Edit, Rename or Delete. How can I pass the row number of the selected row to my ContextMenuStrip? So, I left click to select a cell in Row Number 2. I right click anywhere on the DataGridView and I select "Rename". I want to get a message "you want to rename row number 2". It would be ok to right-click directly on row number 2 and get the message (without first selecting a cell on row number 2).Here is my attempt of code:
void RenameToolStripMenuItemClick(object sender, EventArgs e)
{ //this should rename the table
MessageBox.Show("This should rename the selected row number " + dataGridView1.SelectedRows.ToString());
}
You will need to look at the RowIndex
property for the CurrentCell
property for the DataGridView. some thing like
void RenameToolStripMenuItemClick(object sender, EventArgs e)
{ //this should rename the table
MessageBox.Show("This should rename the selected row number " + dataGridView1.CurrentCell.RowIndex);
}
BUT please remember RowIndex count is starting at 0.
So, row one will show row number 0 and row two will show row number 1.
To work around the RowIndex confusion, you could just add a 1 to the rowIndex like below
void RenameToolStripMenuItemClick(object sender, EventArgs e)
{ //this should rename the table
MessageBox.Show("This should rename the selected row number " + (dataGridView1.CurrentCell.RowIndex + 1));
}