【问题标题】:DataGridViewComboBoxColumn cell value returning null valueDataGridViewComboBoxColumn 单元格值返回空值
【发布时间】:2013-11-26 11:17:05
【问题描述】:

我上周发布了一个问题,但并没有真正明白为什么我必须为第一个答案设置显示成员或值成员。

查看其他论坛,我得到的是,如果我使用 List 绑定到 DataGridViewComboBoxColumn,那么我应该很容易获得选定的值。

我发布的问题在这里:

Setting DataGridViewComboBoxColumn.Valuemember to a list<string>

我现在有这个代码:

// set values to combobox column cells in datagridview
GridSellProducts.Rows.Add();
DataGridViewComboBoxColumn cmbItems = (DataGridViewComboBoxColumn)GridSellProducts.Columns["Item"];

cmbItems.DataSource = productNames;

cmbItems.AutoComplete = true;

GridSellProducts.EditingControlShowing += new
            DataGridViewEditingControlShowingEventHandler(GridSellProducts_EditingControlShowing);

private void LastColumnComboSelectionChanged(object sender, EventArgs e)
{
      string itemValue = GridSellProducts.Rows[GridSellProducts.CurrentCell.RowIndex].Cells["Item"].FormattedValue.ToString();
.......code
}

但是上面的字符串itemValue的值始终为null,即string itemValue = "";当我运行我的代码时。这是为什么呢?

添加:

这就是我填充产品名称的方式

 private void AutocompleteItems()
        {
            // get products
            productsURL = "https://eko-app.com/Products/list_products/sessionId:" + sessionID + ".json";

            var products = Products.GetProducts(productsURL);

            List<string> productNames = new List<string>();

            foreach (var p in products)
            {
                var x = p.Product;

                foreach (var pn in x)
                {
                    productNames.Add(pn.name);
                }
            }

            // set values to combobox column cells in datagridview
            GridSellProducts.Rows.Add();
            DataGridViewComboBoxColumn cmbItems = (DataGridViewComboBoxColumn)GridSellProducts.Columns["Item"];

            cmbItems.DataSource = productNames;

            cmbItems.AutoComplete = true;

            GridSellProducts.EditingControlShowing += new
            DataGridViewEditingControlShowingEventHandler(GridSellProducts_EditingControlShowing);
        }      

类产品

//some code
public class Product
{
     public string product_id { get; set; }
     public string name { get; set; }
     public decimal selling { get; set; }
     public decimal buying { get; set; }
     public string default_tax_tier { get; set; }
     public string quantity_available { get; set; }
}

【问题讨论】:

    标签: c# datagridview


    【解决方案1】:

    我也遇到了同样的问题。答案有点晚了,但我希望它对某人有所帮助。我没有设置列的数据源,而是将对象单独添加到列中,如下所示:

    DataGridViewComboBoxColumn column = (DataGridViewComboBoxColumn)GridSellProducts.Columns["Item"];
    foreach (string productname in productNames)
    {
        column.Items.Add(productname);
    }
    this.ValueType = typeof(string);
    // if you use a custom object instead of a string, set these values:
    // this.DisplayMember = "value";
    // this.ValueMember = "hiddenValue";
    

    然后,您可以通过从事件处理程序中的 DataGridViewComboBoxEditingControl 获取选定项来获取对象(字符串或其他内容)的值。该项目将是添加到列项目的对象(在本例中为字符串)。对我有用的代码是:

    private void LastColumnComboSelectionChanged(object sender, EventArgs e)
    {
        var sendingCB = sender as DataGridViewComboBoxEditingControl;
        var currentcell = mappingDataGridView.CurrentCellAddress;
    
        if (currentcell.X == 0)
        {
            object value = sendingCB.SelectedItem;
            if (value != null)
            {
                string itemValue = (string)sendingCB.SelectedItem;
                //
                // or with an other object it looks like this:
                // int intValue = ((ComboBoxItem)sendingCB.SelectedItem).hiddenValue;
            }
        }
    }
    

    我使用this 帖子了解如何为 DataGridViewComboBoxColumn 使用事件处理程序。希望这会对某人有所帮助:)。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-12-21
      • 1970-01-01
      • 2011-01-27
      • 2015-08-24
      • 2020-01-25
      • 1970-01-01
      • 2023-03-13
      • 1970-01-01
      相关资源
      最近更新 更多