You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何让DataGridViewComboBoxColumn绑定不同数据源并显示CreditUser的Responsible值

WinForms DataGridViewComboBoxColumn 绑定显示对应字段值的实现

需求说明

在WinForms项目的CreditUserLimitsForm类中:

  • 绑定源creditUserBindingSource的数据源为CreditUser类列表
  • DataGridView内有一个名为ResponsibleList的DataGridViewComboBoxColumn,其数据源为字符串列表["Production", "Distribution", "Customer Service", "Sales"]
  • 需要实现该下拉列能为每一行用户显示对应CreditUser对象的Responsible字段值

原代码

CreditUserLimitsForm 类代码

public partial class CreditUserLimitsForm : Form
{
    private List<CreditUser> creditUser;
    private bool SetupCheckStatus = false;
    //private Dictionary<string, string> fbu;

    public CreditUserLimitsForm()
    {
        InitializeComponent();
    }

    private void CreditUserLimitsForm_Load(object sender, EventArgs e)
    {
        //fbu = MainForm.srv.GetFBU();
        //foreach (KeyValuePair<string, string> k in fbu)
        //{
        //    lvwFBU.Items.Add(new ListViewItem(new string[] { k.Key, k.Value }));
        //}
        try
        {
            creditUser = MainForm.srv.GetCreditUser("","").ToList();
            creditUserBindingSource.DataSource = creditUser;
            SetAlternateChoicesUsingDataSource(ResponsibleList);
        }
        catch (Exception ex)
        {
            Cursor = Cursors.Default;
            NutraMsg.DisplayError(this, ex, MainForm.GetMessageDisplayType());
        }
    }
    private void SetAlternateChoicesUsingDataSource(DataGridViewComboBoxColumn comboboxColumn)
    {
        {
            comboboxColumn.DataSource = MainForm.srv.GetResponsibleList();
            comboboxColumn.ValueMember = "Responsible";
            comboboxColumn.DisplayMember = comboboxColumn.ValueMember;
        }
    }
}

CreditUser 类代码

public class CreditUser : INotifyPropertyChanged
{
    public string Responsible { get; set; }
    public int UserId { get; set; }
    public int RoutingId { get; set; }
    public string UserName { get; set; }
    public List<string> AllowedCustomerTypes { get; set; }
    public decimal CreditLimit { get; set; }
    public bool Restricted
    {
        get
        {
            foreach (UserCatalog uc in Catalogs)
            {
                if (uc.Restricted)
                {
                    return true;
                }
            }
            return false;
        }
    }
}

问题分析

原代码中SetAlternateChoicesUsingDataSource方法存在错误:因为comboboxColumn.DataSource是字符串列表,字符串本身没有Responsible属性,所以设置ValueMember = "Responsible"会导致绑定失败,无法正确显示CreditUser对象的Responsible字段值。

修改后的实现代码

完整修改后的CreditUserLimitsForm类

public partial class CreditUserLimitsForm : Form
{
    private List<CreditUser> creditUser;
    private bool SetupCheckStatus = false;
    //private Dictionary<string, string> fbu;

    public CreditUserLimitsForm()
    {
        InitializeComponent();
    }

    private void CreditUserLimitsForm_Load(object sender, EventArgs e)
    {
        //fbu = MainForm.srv.GetFBU();
        //foreach (KeyValuePair<string, string> k in fbu)
        //{
        //    lvwFBU.Items.Add(new ListViewItem(new string[] { k.Key, k.Value }));
        //}
        try
        {
            creditUser = MainForm.srv.GetCreditUser("","").ToList();
            creditUserBindingSource.DataSource = creditUser;
            SetAlternateChoicesUsingDataSource(ResponsibleList);
        }
        catch (Exception ex)
        {
            Cursor = Cursors.Default;
            NutraMsg.DisplayError(this, ex, MainForm.GetMessageDisplayType());
        }
    }

    private void SetAlternateChoicesUsingDataSource(DataGridViewComboBoxColumn comboboxColumn)
    {
        comboboxColumn.DataSource = MainForm.srv.GetResponsibleList();
        // 绑定到CreditUser类的Responsible字段,这是显示对应值的核心设置
        comboboxColumn.DataPropertyName = "Responsible";
    }
}

关键说明

  1. DataPropertyName的作用:该属性指定ComboBox列要绑定到数据源(CreditUser对象)的哪个字段,确保下拉列自动显示对应行CreditUser的Responsible值。
  2. 字符串数据源的处理:当ComboBox列的数据源是字符串列表时,不需要设置ValueMember和DisplayMember,因为字符串本身就是要显示和存储的值,设置反而会因为找不到对应属性导致绑定失败。

内容的提问来源于stack exchange,提问作者Aditya Bhouraskar

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.24 14:09:07