C# 更改 DataGridView 中的列名
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13395321/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Change Column name(s) in DataGridView
提问by
I use the following code to change the Column Name but unfortunately it won't let me do that. Please help me to solve this problem:
我使用以下代码来更改列名称,但不幸的是它不会让我这样做。请帮我解决这个问题:
DateTime dt = DateTime.Now;
string s = dt.DayOfWeek.ToString();
for (int i = 0; i < 10; i++)
{
dataGridView1.Columns.Add(string.Format("col{0}", i), s);
}
for (int i = 0; i < dataGridView1.Columns.Count; i++)
{
int c = dataGridView1.CurrentCell.ColumnIndex;
string str = dataGridView1.Columns[c].HeaderText;
if (str == "Wednesday")
{
str = "fifth day of week";
}
}
Also is there any way so that I can get all day of week after each other between specific datetimes.
还有什么方法可以让我在特定日期时间之间接连获得一周中的每一天。
Any help will be appreciated
任何帮助将不胜感激
采纳答案by Danilo Vulovi?
You need to set DataGridView.Column[index].HeaderText:
您需要设置 DataGridView.Column[index].HeaderText:
DateTime dt = DateTime.Now;
string s = dt.DayOfWeek.ToString();
for (int i = 0; i < 10; i++)
{
dataGridView1.Columns.Add(string.Format("col{0}", i), s);
}
for (int i = 0; i < dataGridView1.Columns.Count; i++)
{
string str = dataGridView1.Columns[i].HeaderText;
if (str == "Wednesday")
{
dataGridView1.Columns[i].HeaderText = "fifth day of week";
}
}
回答by 03Usr
The below code will get all days of week after each other between specific datetimes and print the names of the days as column headers:
下面的代码将在特定日期时间之间依次获取一周中的所有天数,并将这些天的名称打印为列标题:
DateTime dtStart = new DateTime(2012, 11, 1);
DateTime dtEnd = new DateTime(2012, 11, 7);
for (int i = 0; i < dtEnd.Subtract(dtStart).Days; i++)
{
TimeSpan counter = new TimeSpan(i, 0, 0, 0);
dataGridView1.Columns.Add(string.Format("col{0}", i), (dtStart + counter).DayOfWeek.ToString());
}
回答by sameera
Add this line to Datagridview DataBindingComplete event handler
将此行添加到 Datagridview DataBindingComplete 事件处理程序
this.dataGridView1.Columns["your database column name"].HeaderText = " preferred name";

