【问题标题】:Add Combobox in DataGridview在 DataGridview 中添加组合框
【发布时间】:2016-03-21 04:35:10
【问题描述】:

我正在创建一个桌面应用程序,并且我的表单上有一个名为 custCartGrid 的网格。我想通过网格中行中的组合框选择交易类型,如销售或退货。 我是 Grid 中的组合框的新手,所以我不太清楚为什么我没有得到所需的结果。这是我用于获取组合框的代码。

DataGridViewComboBoxColumn dcom = new DataGridViewComboBoxColumn();
        dcom.HeaderText = "Combobox";
        dcom.Items.Add("Sale");
        dcom.Items.Add("Return");
        custCartGrid.Columns.Add(dcom);
  1. 当我插入第二行时,它会在第一行添加另一个组合框,就像我在第一行有 2 个组合框,然后它会创建第二个 有两个组合框的行。第三排也是如此。
  2. 我没有在通过items.add 方法添加的组合框中获取项目。

请帮帮我..

这是我用于在网格中插入值的完整代码

DateTime dt = DateTime.Now;
        string date = dt.ToShortDateString();
        //date
       // determineTransactionType();

        custCartGrid.Rows.Add();
        GridRow = custCartGrid.Rows.Count - 1;

        custCartGrid["CODE", GridRow].Value = productDetails.Tables[0].Rows[0]["ProductID"].ToString();


        custCartGrid["Name", GridRow].Value = productDetails.Tables[0].Rows[0]["ProductName"].ToString();

        custCartGrid["PRICE", GridRow].Value = tb_FP_Price_Single_Product.Text.Trim();
        custCartGrid["CATEGORY", GridRow].Value = productDetails.Tables[0].Rows[0]["CatName"].ToString();

        custCartGrid["MODE", GridRow].Value = "Sale";

        DataGridViewComboBoxColumn dcom = new DataGridViewComboBoxColumn();
        dcom.HeaderText = "Combobox";
        dcom.Items.Add("Sale");
        dcom.Items.Add("Return");
        custCartGrid.Columns.Add(dcom);

这是表单的截图

【问题讨论】:

  • 你改变datagridView的列数了吗?它在哪里被初始化?请发布整个代码。
  • 我没有看到您的代码有任何问题,包括问题中的 custCartGrid 构造和事件。这可能会有所帮助。
  • @UsamaZafar 我对此不太了解,请您将我的代码更改为我的情况好吗?我想要组合框在我的行中
  • 您发布的代码似乎没问题。在看到整个事情之前,我无法确定这个问题。发布用于构造“custCartGrid”的代码部分
  • @UsamaZafar 我已经编辑了这个问题。请检查一下

标签: c# datagridview combobox datagridviewcomboboxcell


【解决方案1】:

尝试以下修改后的代码,将ComboBoxColum 添加到您的 GridView 中:

    DataGridViewComboBoxColumn dcom = new DataGridViewComboBoxColumn();
    dcom.HeaderText = "Combobox";
    dcom.Name = "cmb";
    dcom.MaxDropDownItems = 2;
    dcom.Items.Add("Sale");
    dcom.Items.Add("Return");
    custCartGrid.Columns.Add(dcom);

【讨论】:

  • @IqraQureshi 那么这不是这段代码的问题,因为我已经尝试过了,它工作得很好。
  • @IqraQureshi 我必须承认这很奇怪。
  • 当我通过表单激活事件插入网格值时,我将再次检查所有表单事件。如果问题仍然存在,我会再次发表评论
  • @IqraQureshi 如果没有整个代码,就不可能重现错误。
猜你喜欢
  • 1970-01-01
  • 2014-09-11
  • 1970-01-01
  • 1970-01-01
  • 2017-11-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多