将列添加到datagridview [英] add column to datagridview
问题描述
我正在处理一个现有表单,该表单将填充datagridview,如下所示:
它使用的是bindingsource,因为我在设计器的窗体底部看到BindingSource控件.
Hi,
I am working on an existing form which populates a datagridview as follows:
It is using a bindingsource because I see at the bottom of the form in designer the BindingSource control.
private void PopulateGrid()
{
System.Data.DataTable dt = TFNGet();
fBS.DataSource = dt;
dGV.AutoGenerateColumns = false;
dGV.DataSource = fBS;
int i = 0;
dGV.Columns[i++].DataPropertyName = "BCode";
dGV.Columns[i++].DataPropertyName = "FDCode";
dGV.Columns[i++].DataPropertyName = "Currency";
dGV.Columns[i++].DataPropertyName = "CCcy";
dGV.Columns[i++].DataPropertyName = "VD";
dGV.Columns[i++].DataPropertyName = "BS";
dGV.Columns[i++].DataPropertyName = "A";
dGV.Columns[i++].DataPropertyName = "PCN";
dGV.Refresh();
}
现在,我想添加一个复选框列作为此dGV的第一列,称为失败"
因此,我正在执行以下操作,但错误是:
索引超出范围.必须为非负数,并且小于集合的大小.
Now I would like to add a checkbox column as the first column to this dGV called "Failed"
So I am doing the following to do this but the error is:
"Index was out of range. Must be non-negative and less than the size of the collection.
private void PopulateGrid()
{
System.Data.DataTable dt = TFNGet();
dt.Columns.Add("Failed", typeof(Boolean));
fBS.DataSource = dt;
dGV.AutoGenerateColumns = false;
dGV.DataSource = fBS;
int i = 0;
dGV.Columns[i++].DataPropertyName = "BCode";
dGV.Columns[i++].DataPropertyName = "FDCode";
dGV.Columns[i++].DataPropertyName = "Currency";
dGV.Columns[i++].DataPropertyName = "CCcy";
dGV.Columns[i++].DataPropertyName = "VD";
dGV.Columns[i++].DataPropertyName = "BS";
dGV.Columns[i++].DataPropertyName = "A";
dGV.Columns[i++].DataPropertyName = "PCN";
//checkbox column
dGV.Columns[i++].DataPropertyName = "Failed";
dGV.Refresh();
}
有什么想法吗?
谢谢
Any thoughts please?
Thanks
推荐答案
在哪里定义其他列?
您可能在不存在的列上设置了DataPropertyName.哪一个实际上导致了异常?
dGV.Columns[i++].DataPropertyNane = "Failed"
之一还是dGV.Columns[i++].DataPropertyNane = "FDCode"
我建议第二个,因为我只看到添加了一个列,
Where are the other columns being defined?
It is likely you are setting the DataPropertyName on a column that does not exist. Which one is actually causing the exception?
The one ofdGV.Columns[i++].DataPropertyNane = "Failed"
or is it maybedGV.Columns[i++].DataPropertyNane = "FDCode"
I suggest the second because I am only seeing one column added,
dt.Columns.Add("Failed", typeof(Boolean));
因此,看来您正在将此列设置为"BCode",然后尝试设置不存在的列.
So it seems you are setting this column to be "BCode" and then trying to set columns that do not exist.
这篇关于将列添加到datagridview的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!