I bring through data from a Stored Procedure, this can be displayed in the datagrid fine, however setting column width keeps throwing a null exception:
dataGridView2.Columns[x].Width = 60;
Full Code:
public void Populate_Data_Grid2_All()
{
string sqlQuery = "EXEC sp_CG_GMR_Select_Specific_Data";
using (var connection = new SqlConnection(connectionString))
using (var command = new SqlCommand(sqlQuery, connection))
using (var adapter = new SqlDataAdapter(command))
{
connection.Open();
var myTable = new DataTable();
adapter.Fill(myTable);
dataGridView2.DataSource = myTable;
}
dataGridView2.Columns[0].Width = 60;
dataGridView2.Columns[0].ReadOnly = true;
dataGridView2.Columns[1].Width = 40;
dataGridView2.Columns[1].ReadOnly = true;
dataGridView2.Columns[2].Width = 50;
dataGridView2.Columns[2].ReadOnly = true;
dataGridView2.Columns[3].Width = 250;
dataGridView2.Columns[3].ReadOnly = true;
dataGridView2.Columns[4].Width = 100;
dataGridView2.Columns[4].ReadOnly = true;
dataGridView2.Columns[4].DefaultCellStyle.Format = "N2";
}
Setting ReadOnly and the cell styles don't throw errors, but setting the width does. This code is exactly the same as what I do for another data grid in the program, which brings through more columns from a different procedure. The procedure I'm trying to format now doesn't have any blank values brought through or anything.
Edit: For clarity, here is the code for the other data grid doing the same thing which works perfectly fine. The second datagrid is also a copy of the first, so all setting of it are the same (I've also checked to make sure)
Code:
public void Populate_Data_Grid1(string DataExists, string Scheme)
{
string NoCurrentPeriodData = "EXEC sp_CG_GMR_Scheme_Manual_Entry_Template @Scheme = '"+Scheme+"'";
string YesCurrentPeriodData = "EXEC sp_CG_GMR_Current_Period_Data @Scheme = '"+Scheme+"'";
string sqlQuery;
if(DataExists())
{
sqlQuery = YesCurrentPeriodData;
}
else
{
sqlQuery = NoCurrentPeriodData;
}
using (var connection = new SqlConnection(connectionString))
using (var command = new SqlCommand(sqlQuery, connection))
using (var adapter = new SqlDataAdapter(command))
{
connection.Open();
var myTable = new DataTable();
adapter.Fill(myTable);
dg_Data1.DataSource = myTable;
}
//--Data Grid Column Properties--\\
dg_Data1.Columns[0].Visible = false;
dg_Data1.Columns[2].Visible = false;
dg_Data1.Columns[4].Visible = false;
dg_Data1.Columns[6].Visible = false;
dg_Data1.Columns[1].Width = 60;
dg_Data1.Columns[1].ReadOnly = true;
dg_Data1.Columns[3].Width = 40;
dg_Data1.Columns[3].ReadOnly = true;
dg_Data1.Columns[5].Width = 50;
dg_Data1.Columns[5].ReadOnly = true;
dg_Data1.Columns[7].Width = 250;
dg_Data1.Columns[7].ReadOnly = true;
dg_Data1.Columns[8].Width = 100;
dg_Data1.Columns[8].DefaultCellStyle.Format = "N2";
//dg_Data1.Columns[8].DefaultCellStyle.Format = "D";
btn_SaveValues.Enabled = true;
}
I was having this exact problem and stumbled upon the inconspicuous solution of setting the column's AutoSizeMode before setting Width. Example:
dataGridView2.Columns[0].AutoSizeMode = DataGridViewAutoSizeColumnMode.None;
dataGridView2.Columns[0].Width = 60;
dataGridView2.Columns[0].ReadOnly = true;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With