none
DataGridView CurrentRow Problem

    Question

  • This code runs very well:

     

        SqlConnection conn = new SqlConnection("Data Source=.;Initial Catalog=project;Persist Security Info=True;User ID=company;Password=company");
        SqlCommand cmd;
        private void company_Load(object sender, EventArgs e)
        {
          da = new SqlDataAdapter("company.getCompanyProduct'"+userName+"'",conn);
          DataSet ds = new DataSet();
          da.Fill(ds);
          bs=new BindingSource();
          bs.DataSource = ds.Tables[0];
          dgvStock.DataSource = bs;
          dgvStock.Columns[0].Visible = false;
          dgvStock.Columns[1].Visible = false;
          bindingNavigator1.BindingSource = bs;
    
    

    dgvStock is obviously a DataGridView and is displayed with no errors. However, when I update the code like this:

        int rowIndex = 0;
        SqlConnection conn = new SqlConnection("Data Source=.;Initial Catalog=project;Persist Security Info=True;User ID=company;Password=company");
        SqlCommand cmd;
        private void company_Load(object sender, EventArgs e)
        {
          da = new SqlDataAdapter("company.getCompanyProduct'"+userName+"'",conn);
          DataSet ds = new DataSet();
          da.Fill(ds);
          bs=new BindingSource();
          bs.DataSource = ds.Tables[0];
          dgvStock.DataSource = bs;
          dgvStock.Columns[0].Visible = false;
          dgvStock.Columns[1].Visible = false;
          bindingNavigator1.BindingSource = bs;
          rowIndex = dgvStock.CurrentRow.Index;
    
    

    it marks the last line and displays a strange error:

    NullReferenceException was unhandled

    Object reference not set to an instance of an object.

    What is the problem

    Wednesday, August 10, 2011 1:31 AM

Answers

  •  Hi Humdy,

    Welcome to MSDN Forum.

    Thanks Zain and Adavesh for your friendly assistance and good suggestions.

    The NullReferenceException occured from dgvStock.CurrentRow.Index, which means no any row currently is selected in the DataGridView control. You can add the following try-catch-finally block into your code sample to fix this issue.

    Int32 rowIndex;
    try
    {
    rowIndex
    = DataGridView1.CurrentRow.Index
    ;
    }
    catch
    (Exception ex) {
    MessageBox.Show(ex.Message)
    ;
    }
    finally
    {
    // Ensure 1 row was selected in DataGridView
    DataGridView1.Rows[1].Selected
    = true;
    DataGridView1.CurrentCell = DataGridView1.Rows[1].Cells[0]
    ;
    MessageBox.Show(DataGridView1.CurrentRow.Index)
    ;
    }

     


    Martin Xie [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.


    • Marked as answer by Humdy Friday, August 12, 2011 12:06 PM
    Friday, August 12, 2011 7:36 AM
    Moderator

All replies

  • Probably because when you make a column 
    Visible =false;

    all row selections get removed. So therefore the CurrentRow is null.

     

    Set the index before changing column property like that.

     

       
    rowIndex = dgvStock.CurrentRow.Index; dgvStock.Columns[0].Visible = false; dgvStock.Columns[1].Visible = false; bindingNavigator1.BindingSource = bs;

    Or try this in CellEnter event

     

    void dataGridView1_CellEnter(object sender, DataGridViewCellEventArgs e)
       
    {
           
    var index = e.RowIndex;
       
    }

    My Blogs


    • Proposed as answer by Hardz T L Wednesday, August 10, 2011 3:46 AM
    Wednesday, August 10, 2011 3:24 AM

  • Hello Humdy,

    That can happen only when the query returns zero rows. That means, your ds.Tables[0] contains no rows at all. So, you must add a null check before that line as,

    if ( ds.Tables[0].Rows.Count > 0)
    {
       rowIndex = dgvStock.CurrentRow.Index;
    }
    

    Hope this helps.


    Please mark this post as answer if it solved your problem. Happy Programming!
    Wednesday, August 10, 2011 3:29 AM
  • Thanks Zain and Adavesh, but neither does the job.
    Wednesday, August 10, 2011 9:55 AM
  •  Hi Humdy,

    Welcome to MSDN Forum.

    Thanks Zain and Adavesh for your friendly assistance and good suggestions.

    The NullReferenceException occured from dgvStock.CurrentRow.Index, which means no any row currently is selected in the DataGridView control. You can add the following try-catch-finally block into your code sample to fix this issue.

    Int32 rowIndex;
    try
    {
    rowIndex
    = DataGridView1.CurrentRow.Index
    ;
    }
    catch
    (Exception ex) {
    MessageBox.Show(ex.Message)
    ;
    }
    finally
    {
    // Ensure 1 row was selected in DataGridView
    DataGridView1.Rows[1].Selected
    = true;
    DataGridView1.CurrentCell = DataGridView1.Rows[1].Cells[0]
    ;
    MessageBox.Show(DataGridView1.CurrentRow.Index)
    ;
    }

     


    Martin Xie [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.


    • Marked as answer by Humdy Friday, August 12, 2011 12:06 PM
    Friday, August 12, 2011 7:36 AM
    Moderator
  • Thanks Martin, it worked well. However, I removed the first two lines in the finally block and it still works fine.

    Friday, August 12, 2011 12:07 PM
  • Thanks Humdy for your feedback.

    I'm glad to hear that you got it working. Cheers!

    If you have any difficulty in future programming, we welcome you to post here again.


    Martin Xie [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Monday, August 15, 2011 3:01 AM
    Moderator