datagrid current column
how to get datagrid view current column value on move the cursor one column to another column
Know the answer? Post it — somebody with the same question will find it here.
Sign in to answer this question
It is the same account you read, post and publish with — and you will come straight back to this page.
Sunny SharmaPosted May 28, 2013, 5:55 AM
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 :)
vadivelPosted May 28, 2013, 5:03 AM
{
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 SharmaPosted May 27, 2013, 5:31 AM
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.