Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Datagridview full row selection but get single cell value

I have a datagridview that is a full row select. How would I grab the data from only a certain cell no matter what cell in the row was clicked on since it highlights the entire row.

like image 927
Tsukasa Avatar asked Oct 05 '11 05:10

Tsukasa


2 Answers

You can do like this:

private void datagridview1_SelectionChanged(object sender, EventArgs e) {   if (datagridview1.SelectedCells.Count > 0)   {     int selectedrowindex = datagridview1.SelectedCells[0].RowIndex;     DataGridViewRow selectedRow = datagridview1.Rows[selectedrowindex];       string cellValue = Convert.ToString(selectedRow.Cells["enter column name"].Value);              } } 
like image 73
Glory Raj Avatar answered Sep 28 '22 18:09

Glory Raj


If you want to get the contents of selected cell; you need the index of row and cell.

int rowindex = dataGridView1.CurrentCell.RowIndex; int columnindex = dataGridView1.CurrentCell.ColumnIndex;   dataGridView1.Rows[rowindex].Cells[columnindex].Value.ToString(); 
like image 29
Özer Özcan Avatar answered Sep 28 '22 19:09

Özer Özcan