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.