【问题标题】:set value to DataGridViewComboBox Winforms C#将值设置为 DataGridViewComboBox Winforms C#
【发布时间】:2018-10-19 06:25:50
【问题描述】:

我有一个 dataGridView,它的列类型之一是 ComboBox。该组合框与数据源绑定。问题是,当我使用以下行设置此组合框的值时:

for (int i = 0; i < lstData.Count; i++)
{
   grd["ColumnName",i].Value = lstData[i].Data;
}

或使用以下行:

grd.Rows.Add(lstData[i].Data)

它应该显示 Short_Description (0 - Reg) 而不是 Report_Code (0)。但它将值设置为下拉文本而不是设置值。我在这缺少什么? 附上截图供参考。

【问题讨论】:

  • 检查您的 reportCodeLookupBinding 对象值。它可以为 short_description 也设置 0 报告代码
  • 你的问题不清楚,设置组合框值的问题?还是组合框文本显示?
  • @BaskarJohn 问题是将值设置为组合框
  • 'lstData[i].Data' 是什么类型?自定义对象或数组或数据值?
  • 这是数据列表

标签: c# .net winforms datagridviewcolumn datagridviewcomboboxcell


【解决方案1】:
public partial class GridManualBind : Form
{
    //Binding Source for datagridView
    BindingSource datasource = new BindingSource();

    //Binding Source for datagridView Combobox Column
    BindingSource reportLookupBinding = new BindingSource();
    List<ManualData> list = new List<ManualData>();
    public GridManualBind()
    {
        InitializeComponent();
        dataGridView1.AutoGenerateColumns = false;
        datasource.DataSource = typeof(ManualData);
        reportLookupBinding.DataSource = typeof(ManualDataReport);

        dataGridView1.DataSource = datasource;

        // column
        dataGridView1.Columns.Add(new DataGridViewTextBoxColumn
        {
            HeaderText = "Price Type",
            DataPropertyName = "PriceType"
        });

        // As you see in the picture its configured for combo box column
        dataGridView1.Columns.Add(new DataGridViewComboBoxColumn
        {
            HeaderText = "Report Code Lookup",
            DataPropertyName = "ReportCodeLookup",
            DataSource = reportLookupBinding,
            DisplayMember = "Short_Description",
            ValueMember = "Report_Code"
        });

        //Adding Data for Combo Box data Source
        reportLookupBinding.Add(new ManualDataReport { Report_Code = 0, Short_Description = "0-Reg" });
        reportLookupBinding.Add(new ManualDataReport { Report_Code = 1, Short_Description = "1-Post" });

        list.AddRange(new ManualData[]
        {
             new ManualData { PriceType="Reg", ReportCodeLookup=0 },
             new ManualData { PriceType="Post", ReportCodeLookup=1 }
        });
        //Adding Data for grid View data Source
        datasource.Add(list[0]);
        datasource.Add(list[1]);

    }
    private void button1_Click(object sender, EventArgs e)
    {
        // you can Set the combo box value using this;
        list[0].ReportCodeLookup = 1;
        dataGridView1.Invalidate();
    }

    private class ManualData
    {
        public string PriceType { get; set; }
        public int ReportCodeLookup { get; set; }
    }

    private class ManualDataReport
    {
        public int Report_Code { get; set; }
        public string Short_Description { get; set; }
    }
}

设置组合框单元格值的值

    private void button1_Click(object sender, EventArgs e)
    {
        // you can Set the combo box value using this;
        list[0].ReportCodeLookup = 1; // depend on combo box cell value
        dataGridView1.Invalidate();
    }

【讨论】:

    猜你喜欢
    • 2011-08-24
    • 1970-01-01
    • 2016-06-24
    • 2015-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多