【问题标题】:Manually added column not showing in windows form DataGridView手动添加的列未在 Windows 窗体 DataGridView 中显示
【发布时间】:2025-12-27 01:55:06
【问题描述】:

我有一个带有 DataSet 源表的 DataGridView (CurrentLine)。我添加了一个带有不同数据源的组合框列,效果很好。我尝试添加另一列,但 DataGridView 拒绝显示它! 我尝试通过 DataGridViewColumn 和 DataGridViewTextBoxColumn 添加一个常规列(因为我基本上需要一个只有数字的常规列)。 我尝试将可见性属性设置为 true。 似乎没有任何效果,我不知道为什么。

da1.Fill(Orders, Order);
da2.Fill(Orders, Lines);
da3.Fill(Orders, Products);

CurrentLine.DataSource = Orders.Tables[Lines];
CurrentLine.Columns["fkPrdctnum"].Visible = false;
CurrentLine.Columns["linenum"].Width = 60;
CurrentLine.Columns["linenum"].HeaderText = "Number";
CurrentLine.Columns["linenum"].ReadOnly = true;
CurrentLine.Columns["lineQuantity"].HeaderText = "Quantity";
CurrentLine.Columns["lineQuantity"].Width = 70;
CurrentLine.Columns["linePrdctPrice"].HeaderText = "Unit Price";
CurrentLine.Columns["linePrdctPrice"].Width = 100;

DataGridViewComboBoxColumn prod = new DataGridViewComboBoxColumn();
prod.DataSource = Orders.Tables[Products];
prod.DisplayMember = "prdctName";
prod.ValueMember = "prdctNum";
prod.HeaderText = "Product";
CurrentLine.Columns.Add(prod);
for (int i=0; i<CurrentLine.Rows.Count; i++)
{
int product = Convert.ToInt32(Orders.Tables[Lines].Rows[i][1]); //This loop sets the values of the combobox according to the order lines in the orderline table in the dataset.
CurrentLine.Rows[i].Cells[4].Value = product;
}

DataGridViewColumn LinePrice = new DataGridViewColumn();
LinePrice.HeaderText = "Line Price";
LinePrice.Width = 100;
LinePrice.ReadOnly = true;
CurrentLine.Columns.Add(LinePrice);

要明确一点:组合框工作正常,设置其值的循环也工作正常。问题似乎出在 DataGridViewColumn LinePrice 的某个地方。

知道发生了什么吗?为什么一种类型的列(组合框)可以正常工作,而另一种则不能?

附言我已经削减了关于另一个填充 Order 表的 DataGridView 的代码行,因为它工作正常并且据我所知与问题无关)。

更新:当我将框移到组合框列之前时,它突然出现...虽然不知道为什么,它不是我希望它在 DataGridView 中的位置。

【问题讨论】:

  • 您是否将CurrentLine.AutoGenerateColumns 设置为false?数据网格是否有滚动条并且列不在可见区域中? lineprice 列是在事件还是线程中生成的?
  • DataGridViewColumn 更改为 DataGridViewTextBoxColumn
  • @Pikoh 正如我在帖子中所写,我已经尝试过了。它也不起作用。
  • @SebastianSchulz 这对所有人来说都是不。当前DataGridView列只占一半左右的空间,而且没有滚动条。
  • 它会抛出任何错误还是根本不出现?

标签: c# datagridview datagridviewcolumn


【解决方案1】:

将您的代码更改为:

  DataTable Orders = Orders.Tables[Lines];
CurrentLine.DataSource = Orders;

      for (int i = 0; i < CurrentLine.Rows.Count-1; i++)
              {
                  string product =Orders.Rows[i][1].ToString (); //This loop sets the values of the combobox according to the order lines in the orderline table in the dataset.
                  CurrentLine.Rows[i].Cells[4].Value = product;
              }

              DataColumn LinePrice = new DataColumn("Line Price");
              Orders.Columns.Add(LinePrice);

应该可以的

【讨论】:

  • 恐怕这并没有解决问题。我稍微调整了一下以尝试解决问题,但无论我尝试什么,它都不起作用。如果我将它放在组合框之前,它会破坏组合框循环。如果我把它放在组合框之后,它根本就不会出现。
  • 更改后你能用新代码编辑你的帖子吗
  • 我愿意,但我将代码返回到一切正常的原始版本。我最终使用了从 cmets 到原始帖子的建议想法,尽管我似乎仍然无法让 displayindex 属性工作......
【解决方案2】:

我使用了您的代码,在执行添加“Line Price”column的代码片段时遇到了以下错误。

无法添加列,因为它的 CellType 属性为空。

如果在您已经将行添加到 DataGridView 之后添加列,则可能会产生上述错误,因为无法确定单元格类型。

我设法根据您使用以下代码片段请求的顺序显示该列。请尝试添加新行并确认这是否有助于解决您的问题。

        DataGridViewColumn LinePrice = new DataGridViewColumn();
        LinePrice.HeaderText = "Line Price";
        LinePrice.Width = 100;
        LinePrice.ReadOnly = true;
        LinePrice.CellTemplate = new DataGridViewTextBoxCell();    // Define the cell type of the new column
        CurrentLine.Columns.Add(LinePrice);

注意:for 循环在使用i 获取Orders.Rows[i] 行时会产生错误(@o_weisman 也提到了这一点)。此外,您似乎有一个主 try catch 块正在抑制错误。请检查您的代码以确保您已相应地实施错误陷阱。

【讨论】: