Skip to content

Datagrid current column

vadivel

Asked May 26, 2013, 11:06 PM EDT3 answers
Share
LinkedInX
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.

  • Sunny  Sharma
    0
    Hi vadivel,

    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.
    0
  • 0
     private void dataGridView1_CellValueChanged(object sender, DataGridViewCellEventArgs e)
            {
               
                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
    0
  • Sunny  Sharma
    0
    Hi Vadivel,

    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 :)
    0