Microsoft Communities

Welcome to WindowsClient.net | Sign in | Join

Here are some frequently asked questions about Windows Forms and their answers.

Windows Forms FAQs

How do I set the column HeaderText in a DataGridView?

I wrote this to set the HeaderText in a DataGridView:

dataGridView1.AutoGenerateColumns = true;
dataGridView1.DataSource = ds;
dataGridView1.DataMember = ds.Tables[0].TableName;

// change the HeaderText of columns.
dataGridView1.Columns[0].HeaderText="aa";
dataGridView1.Columns[1].HeaderText="bb";
dataGridView1.Columns[2].HeaderText="cc";

But only Columns[0]'s HeaderText is changed.

Because of the way data binding works, the columns will be recreated before the form is shown. Move the lines to set the header text to either in your Form_Load method or handle the DataBindingCompleted event on the DataGridview and set the column header text there.



Page view counter