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.
-
possible duplicate of [How do I get the selected row data from a data grid view using SelectedRows?](http://stackoverflow.com/questions/3552497/how-do-i-get-the-selected-row-data-from-a-data-grid-view-using-selectedrows) – CodeSlayer Oct 28 '14 at 03:01
18 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);
}
}

- 17,397
- 27
- 100
- 203
-
-
@pratap K , Does DataGridView1_SelectionChanged event will fire at the time of form load ? but it is happening in my case. Please help me resolve this. If needed more details I will post it as a question. – Sanjeev4evr Apr 09 '14 at 14:33
-
I wanted the opposite behavior and your posted helped me realize what I needed to do. SelectedCells.Count == 1 :) – Mido Dec 23 '14 at 08:22
-
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();

- 13
- 5

- 1,253
- 16
- 17
I know, I'm a little late for the answer. But I would like to contribute.
DataGridView.SelectedRows[0].Cells[0].Value
This code is simple as piece of cake

- 111
- 2
- 12
In the CellClick event you can write following code
string value =
datagridviewID.Rows[e.RowIndex].Cells[e.ColumnIndex].FormattedValue.ToString();
Using the bove code you will get value of the cell you cliked. If you want to get value of paricular column in the clicked row, just replace e.ColumnIndex with the column index you want

- 9,089
- 26
- 73
- 118
-
This is more better answer, just need to change `e.ColumnIndex` with specific string column name or index since question ansked for it. – Ankush Madankar Jan 30 '14 at 05:58
you can get the values of the cell also as the current selection is referenced under CurrentRow
dataGridView1.CurrentRow.Cell[indexorname].FormattedValue
Here you can use index or column name and get the value.

- 37,194
- 9
- 78
- 82
I just want to point out, you can use .selectedindex to make it a little cleaner
string value = gridview.Rows[gridview.SelectedIndex].Cells[1].Text.ToString();

- 558
- 9
- 31
string value = dataGridVeiw1.CurrentRow.Cells[1].Value.ToString();

- 25,519
- 37
- 106
- 129

- 371
- 3
- 9
DataGridView.CurrentRow.Cells[n]
See: http://msdn.microsoft.com/en-us/library/system.windows.forms.datagridview.currentrow.aspx

- 27,674
- 12
- 80
- 107
Just Use: dataGridView1.CurrentCell.Value.ToString()
private void dataGridView1_MouseDoubleClick(object sender, MouseEventArgs e)
{
MessageBox.Show(dataGridView1.CurrentCell.Value.ToString());
}
or
// dataGrid1.Rows[yourRowIndex ].Cells[yourColumnIndex].Value.ToString()
//Example1:yourRowIndex=dataGridView1.CurrentRow.Index (from selectedRow );
dataGrid1.Rows[dataGridView1.CurrentRow.Index].Cells[2].Value.ToString()
//Example2:yourRowIndex=3,yourColumnIndex=2 (select by programmatically )
dataGrid1.Rows[3].Cells[2].Value.ToString()

- 3,219
- 1
- 18
- 27
this get me the text value of exact cell in data grid view
private void dataGridView_SelectionChanged(object sender, EventArgs e)
{
label1.Text = dataGridView.CurrentRow.Cells[#].Value.ToString();
}
you can see it in label and change # wih the index of the exact cell you want

- 21
- 2
- 10
Use Cell Click
as other methods mentioned will fire upon data binding, not useful if you want the selected value, then the form to close.
private void dgvProducts_CellClick(object sender, DataGridViewCellEventArgs e)
{
if (dgvProducts.SelectedCells.Count > 0) // Checking to see if any cell is selected
{
int mSelectedRowIndex = dgvProducts.SelectedCells[0].RowIndex;
DataGridViewRow mSelectedRow = dgvProducts.Rows[mSelectedRowIndex];
string mCatagoryName = Convert.ToString(mSelectedRow.Cells[1].Value);
SomeOtherMethod(mProductName); // Passing the name to where ever you need it
this.close();
}
}
For those who could not fire the click event, they may use following code
public Form1()
{
InitializeComponent();
this.dataGridView1.CellClick += new System.Windows.Forms.DataGridViewCellEventHandler(this.dataGridView1_CellClick);
}
To get one cell value based on entire row selection:
if (dataGridView1.SelectedRows.Count > 0)
{
foreach (DataGridViewRow row in dataGridView1.Rows)
{
TextBox1.Text = row.Cells["ColumnName"].Value.ToString();
}
}
else
{
MessageBox.Show("Please select item!");
}
}

- 4,527
- 2
- 44
- 48
Simplest code is DataGridView1.SelectedCells(column_index).Value
As an example, for the first selected cell:
DataGridView1.SelectedCells(0).Value

- 6,313
- 4
- 53
- 81

- 1
- 3
for vb.net 2013 i use
DataGridView1.SelectedRows.Item(0).Cells(i).Value
where i is the cell number

- 86
- 6
Assigns cell 1 of dgwProduct lines to ProductId.
private void btnUpdate_Click(object sender, EventArgs e)
{
ProductId = Convert.ToInt32(dgwProduct.CurrentRow.Cells[0].Value);
}
Here it puts all the cells in the row into the textbox.
private void dgwProduct_CellClick(object sender, DataGridViewCellEventArgs e)
{
var row=dgwProduct.CurrentRow;
tbxProductName.Text =row.Cells[1].Value.ToString();
tbxUnitPrice.Text = row.Cells[2].Value.ToString();
tbxQuantityPerUnit.Text = row.Cells[3].Value.ToString();
tbxStockUpdate.Text = row.Cells[4].Value.ToString();
}
In this way, we assign all the cells in the dataGridView to the textboxes.

- 89
- 8
-
You can also check here: http://msdn.microsoft.com/en-us/library/system.windows.forms.datagridview.currentrow.aspx – Hakan Bayazıt Oct 04 '21 at 11:44
private void DataGridView_SelectionChanged(object sender, EventArgs e)
{
if (dataGridViewX3.SelectedCells.Count > 0)
{
int selectedrowindex = dataGridViewX3.SelectedCells[0].RowIndex;
DataGridViewRow selectedRow = dataGridViewX3.Rows[selectedrowindex];
string cellValue = Convert.ToString(selectedRow.Cells[0].Value);
Console.WriteLine(cellValue);
}
}

- 11
- 1
- 3