1

I have a DataGridView that is filled using an OleDbDataAdapter. However, I realized when I try to set the current selected row (cell) on my DataGridView, a hidden exception occurs. I have removed the try-catch block so that the exception is thrown. It is most probably a NullReferenceException. But once the program passes this line, the rest of the lines on my method are ignored and my form shows some strange behavior. For example the close box on the form suddenly disappears.

This is the snipped of code when I load my records and try to fill my DataGridView:

dgvCampaigns.DataSource = dsCampaigns;
dgvCampaigns.DataMember = "Campaigns";
dgvCampaigns.Columns["Id"].Visible = false;
dgvCampaigns.Columns["RowNum"].Visible = false;

if (dsCampaigns.Tables["Campaigns"].Rows != null)
    if (dsCampaigns.Tables["Campaigns"].Rows.Count > 0)
    {
        mngSelectedIndex = 0;
        dgvCampaigns.CurrentCell = dgvCampaigns[0, 0];
    }                  
if (dgvCampaigns.CurrentCell != null)
    pnlManageCampaignActivities.Enabled = true;
lblCurrentPage.Text = mngCurrentPage.ToString();
lblTotalPages.Text = mngPagesCount.ToString();
lblCampaignCount.Text = mngCampaignCount.ToString();
lblLastRefreshedValue.Text = DateTime.Now.ToString();
UpdatePaginationLinkValues();

The line that causes this problem is

dgvCampaigns.CurrentCell = dgvCampaigns[0, 0];

After this line, the rest of the lines are ignored and no Exception is thrown.

disasterkid
  • 6,948
  • 25
  • 94
  • 179
  • is this code in form_load? – Hamid Pourjam Nov 03 '14 at 15:13
  • @HPT form_load calls a method that contains this code. So it is not directly form_load but it triggers on it. – disasterkid Nov 03 '14 at 15:18
  • this is a know problem check [this](http://stackoverflow.com/a/4934010/3970411) and also you can read more [here](http://blog.paulbetts.org/index.php/2010/07/20/the-case-of-the-disappearing-onload-exception-user-mode-callback-exceptions-in-x64/) – Hamid Pourjam Nov 03 '14 at 15:27

0 Answers0