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 ent
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
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.
for vb.net 2013 i use
DataGridView1.SelectedRows.Item(0).Cells(i).Value
where i is the cell number
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);
}
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();
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!");
}
}