Why DataColumn.Caption doesn't work?

前端 未结 6 621
夕颜
夕颜 2020-12-10 12:28

I am trying to create a DataTable and bind it to a DataGridView. It works, but I can\'t set columns headers via the Caption property.

6条回答
  •  萌比男神i
    2020-12-10 13:07

    Well, MSDN is right. That is what the Caption property is for. However, that doesn't mean that control makers have to use the caption property. In this case, Microsoft didn't do that (although they really should have). You can modify your code to this though:

    ///snip
    
    dataGridView1.DataSource = dt;
    
    foreach (DataGridViewColumn col in dataGridView1.Columns) {
      col.HeaderText = dt.Columns[col.HeaderText].Caption;
    }
    

提交回复
热议问题