Datagrid current column
how to get datagrid view current column value on move the cursor one column to another column
3 Answers
Know the answer? Post it. Somebody with the same question will find it here.
Know the answer? Post it. Somebody with the same question will find it here.
Sunny SharmaRank #223 · 11.1K pointsPosted May 27, 2013, 1:31 AM EDT
You can use CellClick event of DataGridView to capture the current column and row Index. Below is a demonstration:
private void DGV_CellClick(object sender, DataGridViewCellEventArgs e)
{
MessageBox.Show("Mouse Entered- Col:" + e.ColumnIndex.ToString() + ", Row:" + e.RowIndex.ToString());
}
This code will popup a message each time a cell is clicked/selected to edit with Column & Row Index.
Happy Coding :)
Please don't forget to accept this as answer if it helps!
Thanks.
vadivel4 pointsPosted May 28, 2013, 1:03 AM EDT
{
if (e.ColumnIndex == 0)
{
if ((string)dataGridView1.CurrentRow.Cells[0].Value.ToString() != string.Empty)
MessageBox.Show("ok");
}
}
Error : NullReferenceException was unhandled Object reference not set to an instance of an object.
How to solved this error
Sunny SharmaRank #223 · 11.1K pointsPosted May 28, 2013, 1:55 AM EDT
Definitely it will give you an error if you use it like this. The moment a DataGridView starts initializing it fires a CellValue_Changes event every time it initializes a new cell or change the value of any cell whether it is a header cell or it's a row cell. Well, that's not a problem in your code. The problem is you're trying to access "DGV.CurrentRow.Cells[0].Value" which is null. Ther's no CurrentRow the moment a DataGridView Gets Initialized. Hope you got the reason why the error is there.
If you want to track if the cell value is changed, you can try CellEndEdit event:
private void DGV_CellEndEdit(object sender, DataGridViewCellEventArgs e)
{
if (e.ColumnIndex == 0)
{
if ((string)DGV.CurrentRow.Cells[0].Value.ToString() != string.Empty)
MessageBox.Show("ok");
}
}
Hope it helps :)