0

I'm creating a DataGridView and using a DataTable to populate it.

My C#

DataGridView dgv = new DataGridView();
DataTable dt = new DataTable();

dt.Columns.Add("Col_1");
dt.Columns.Add("Col_2");

for(int i=0; i< myDictionary; i++)
{
  dt.Rows.Add(myDictionary.key, myDictionary.Value);
}

dgv.DataSource = dt;

for(int j=0; j < dgv.Columns.Count; j++)
{...} // this is where Count is always 0...

I'm really just looking to access the columns so that I can do something like

dgv.Columns[j].AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill;

Q: How can have the dataGridView reflect the columns properly?

fifamaniac04
  • 2,343
  • 12
  • 49
  • 72

1 Answers1

0

You have to set the property AutogenerateColumns to true:

dgv.AutogenerateColumns = true;
dgv.DataSource = dt;
jcvegan
  • 3,111
  • 9
  • 43
  • 66