通过代码向Winforms DataGridView添加新列 [英] Adding new columns to a Winforms DataGridView via code

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

问题描述

我试图在给定月份的每一天添加N个列数:

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() });
}

我收到此错误:


至少有一个DataGridView
控件的列没有单元格
模板。

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


推荐答案

当您创建一个新的datagridview列时,它很空白。您将需要设置 celltemplate 的列,以便它知道什么控件显示在网格中的单元格。或者,我想如果你使用一些更强的类型列(DataGridViewTextBoxColumn),那么你可能会没问题。

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.

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

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