DataGridViewComboBoxColumn DataSource?

时间:2011-10-05 19:16:48

标签: c# winforms data-binding datagridview datatable

我正试图在DataGridView中设置一些内容。看起来这应该很简单,但我遇到了麻烦。我想显示三列:

  • 代码ID
  • 代号
  • ComboBox与TypeName的DisplayMember,TypeID的ValueMember

我希望能够从TypeName的所有可能值中进行选择。这是我的困境:

如果我将所有内容加载到一个DataTable并将DataGridView设置为DataSource,我可以显示该记录的现有TypeName,但组合框不包括任何其他值。如果我将DataSource的{​​{1}}设置为包含所有可能DataGridViewComboBoxColumn的单独DataTable,则不会显示现有值。

TypeNames真的很烦人,所以无论是解决方案还是可行的替代方案都将受到赞赏。

修改:此问题似乎是由于我希望为DataGridViewDisplayMember设置单独的项目。如果我不担心将ValueMember设置为ID

,则以下情况有效
ValueMember

如果我执行以下操作,则会选择正确的类型,但我无法更改组合框中的选择:

var typeColumn = new DataGridViewComboBoxColumn
{
    DataSource = typeList,
    DisplayMember = "Type",
    ValueMember = "Type",
    DataPropertyName = "Type"
}

如果我使用以下内容,则会在尝试填充时出现var typeColumn = new DataGridViewComboBoxColumn { DataSource = typeList, DisplayMember = "Type", ValueMember = "TypeID", DataPropertyName = "TypeID" } 错误:

FormatException

修改var typeColumn = new DataGridViewComboBoxColumn { DataSource = typeList, DisplayMember = "Type", ValueMember = "TypeID", DataPropertyName = "Type" } 是一个由以下内容填充的简单typeList

DataTable

2 个答案:

答案 0 :(得分:6)

我有一个类似的(我认为)问题,我的解决方案是在DataSource 设置DataGridViewComboBoxColumn之前为DataSource 设置DataGridView List<T>

在我的情况下,我的DataSource分别是BindingList<T>DataGridViewComboBoxColumn categoryColumn = (DataGridViewComboBoxColumn)_ItemsGrid.Columns["CategoryID"]; categoryColumn.DataSource = categories; _ItemsGrid.DataSource = items; ,但它应该与DataTables一样:

{{1}}

答案 1 :(得分:4)

好的,我想出了一个示例ClientInfoInsuranceDetails,我认为可能会模仿您要做的事情。如果这些细节不太正确,请告诉我。此示例将填充DataGridViewComboBox并根据InsuranceDetails设置值(具体位于:InsurDetailz = all_insurance_types[2]

   public partial class Form1 : Form
   {
      private ClientInfo _myClient;
      private BindingList<InsuranceDetails> all_insurance_types =
         new BindingList<InsuranceDetails>();

      public Form1()
      {
         InitializeComponent();

         DataGridView grid = new DataGridView();
         grid.Dock = DockStyle.Fill;
         grid.AutoGenerateColumns = true;

         all_insurance_types.Add(new InsuranceDetails(1, "Health"));
         all_insurance_types.Add(new InsuranceDetails(2, "Home"));
         all_insurance_types.Add(new InsuranceDetails(3, "Life"));

         var col = new DataGridViewComboBoxColumn
         {
            DataSource = all_insurance_types,
            HeaderText = "Insurance Type",
            DataPropertyName = "InsurDetailz",
            DisplayMember = "ItType",
            ValueMember = "Self",
         };

         _myClient = new ClientInfo { 
            InsurDetailz = all_insurance_types[2], Name = "Jimbo" };
         grid.Columns.Add(col);
         grid.DataSource = new BindingList<ClientInfo> { _myClient };
         this.Controls.Add(grid);

         this.FormClosing += new FormClosingEventHandler(Form1_FormClosing);
      }

      void Form1_FormClosing(object sender, FormClosingEventArgs e)
      {
         // make sure its updated
         InsuranceDetails c = _myClient.InsurDetailz;
         string name = _myClient.Name;
         // Place breakpoint here to see the changes in _myClient 
         throw new NotImplementedException();
      }
   }

   class ClientInfo
   {
      public string Name { get; set; }
      public InsuranceDetails InsurDetailz { get; set; }
   }

   class InsuranceDetails
   {
      public int InsuranceTypeID { get; set; }
      public String ItType { get; set; }
      public InsuranceDetails Self { get { return this; } }

      public InsuranceDetails(int typeId, String itType)
      {
         this.InsuranceTypeID = typeId;
         this.ItType = itType;
      }
   }