WinForms DataGridView不会在设置DataSource时添加列

时间:2015-04-06 09:17:43

标签: winforms datagridview

我有DataGridView名为dgvInspectionsHistory

第一次运行以下代码行后,dgvInspectionsHistory会添加5列

this.ClearTable(this.dgvTemplatesConfiguration);

this.dgvTemplatesConfiguration.DataSource = templatesData;

this.dgvTemplatesConfiguration.ReadOnly = false;

//3 columns added

this.dgvTemplatesConfiguration.Columns.Add(this.CreateFaultsColumn(faultsData, "OuterFaults", 2));
this.dgvTemplatesConfiguration.Columns.Add(this.CreateFaultsColumn(faultsData, "InnerFaults", 5));

//2 more columns added

for (int i = 0; i <= dgvTemplatesConfiguration.Rows.Count - 1; i++)
{
     dgvTemplatesConfiguration["InnerFaults", i].Value = OkFaultName;
}

但是这段代码第二次运行后确实删除了一些列,dgvInspectionsHistory没有被填充回来:

this.ClearTable(this.dgvInspectionsHistory);
this.dgvInspectionsHistory.DataSource = inspectionsData;

//the desired 3 colums are not getting added

为什么会这样,我怎样才能达到预期的效果?

1 个答案:

答案 0 :(得分:0)

因为您正在使用两个不同的数据网格,即dgvTemplatesConfiguration和dgvInspectionsHistory,这就是为什么没有任何引用,请尝试在第二个代码中将dgvInspectionsHistory重命名为dgvTemplatesConfiguration并检查它是否有效