日期:2014-05-17 浏览次数:21170 次
private void Form1_Load(object sender, EventArgs e)
{
DataTable dt = new DataTable();
dt.Columns.Add("name", typeof(string));
dt.Columns.Add("age", typeof(string));
dt.Columns.Add("sex", typeof(string));
//假设这里绑定了3列的datatable
this.dataGridView1.DataSource = dt;
int width = this.dataGridView1.Width;
int avgWidth = width / dt.Columns.Count;//求出每一列的header宽度
for (int i = 0; i < this.dataGridView1.Columns.Count; i++)
{
this.dataGridView1.Columns[i].Width = avgWidth;//设置每一列的宽度
}
}