通过code添加新列到一个WinForms的DataGridView [英] Adding new columns to a Winforms DataGridView via code

查看:279
本文介绍了通过code添加新列到一个WinForms的DataGridView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想加N多列的给定月份的每一天:

  VAR daysCount = DateTime.DaysInMonth(DateTime.Now.Year,月);

的for(int i = 1; I< = daysCount;我++)
{
    dataGridView1.Columns.Add(新的DataGridViewColumn(){HEADERTEXT = i.ToString()});
}
 

我得到这个错误:

  

目前至少有一个在DataGridView的   控件中列有没有电池   模板。

解决方案

当你创建一个新的DataGridView列是pretty的空白。您将需要设置<一个href="http://msdn.microsoft.com/en-us/library/system.windows.forms.datagridviewcolumn.celltemplate.aspx">celltemplate列的,以便它知道什么控制以显示中的单元格。另外我认为,如果你使用一些强类型的列(DataGridViewTextBoxColumn),那么你可能是好的。

I'm trying to add N number of columns for each days of a given month:

var daysCount = DateTime.DaysInMonth(DateTime.Now.Year, month);

for (int i = 1; i <= daysCount; i++)
{
    dataGridView1.Columns.Add(new DataGridViewColumn() { HeaderText = i.ToString() });
}

I'm getting this error:

At least one of the DataGridView control's columns has no cell template.

解决方案

When you create a new datagridview column it's pretty blank. You will need to set the celltemplate of the column so that it knows what controls to show for the cells in the grid. Alternatively I think if you use some of the stronger typed columns (DataGridViewTextBoxColumn) then you might be ok.

这篇关于通过code添加新列到一个WinForms的DataGridView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆