DataGridViewComboBoxColumn 数据源?

发布于 2024-12-08 01:00:50 字数 1714 浏览 0 评论 0原文

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

  • CodeID
  • CodeName
  • ComboBox,其中 DisplayMember of TypeName、ValueMember of TypeID

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

如果我将所有这些加载到一个 DataTable 中并将 DataGridView 设置为 DataSource,我可以显示现有的 该记录的 TypeName,但组合框将不包含任何其他值。如果我将 DataGridViewComboBoxColumnDataSource 设置为包含所有可能的 TypeNames 的单独 DataTable,则现有值为不显示。

DataGridView 使用起来确实很烦人,因此无论是解决方案还是可行的替代方案都将受到赞赏。

编辑:看来问题是由于我想要为 DisplayMemberValueMember 提供一个单独的项目引起的。如果我不担心将 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"
}

edit: typeList 是一个简单的DataTable 由以下内容填充:

SELECT DISTINCT IT.InsuranceTypeID, IT.[Type]
FROM InsuranceType IT
WHERE IT.ClientID = @ClientID
ORDER BY [Type]

I'm trying to get something set up in a DataGridView. It seems like this should be pretty straightforward but I'm having trouble. I want to display three columns:

  • CodeID
  • CodeName
  • ComboBox with DisplayMember of TypeName, ValueMember of TypeID

I want to be able to select from all possible values of TypeName. Here's my dilemma:

If I load all of this into one DataTable and set the DataGridView as the DataSource, I can display the existing TypeName for that record, but the combo box will not include any other values. If I set the DataSource for the DataGridViewComboBoxColumn to a separate DataTable that includes all possible TypeNames, the existing value is not displayed.

DataGridView is really annoying to work with so either a solution for this or a viable alternative would be appreciated.

Edit: it appears the issue is caused by my wanting to have a separate item for DisplayMember and ValueMember. The following works, if I don't worry about setting the ID as the ValueMember:

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

If I do the following, the right types are selected, but I can't change the selection in the combo box:

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

If I use the following I get a FormatException error as it's trying to populate:

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

edit: typeList is a simple DataTable populated by the following:

SELECT DISTINCT IT.InsuranceTypeID, IT.[Type]
FROM InsuranceType IT
WHERE IT.ClientID = @ClientID
ORDER BY [Type]

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

画▽骨i 2024-12-15 01:00:50

我有一个类似的(我认为)问题,我的解决方案是为 DataGridViewComboBoxColumn 设置 DataSource before 设置 DataGridView 的数据源

就我而言,我的 DataSource 分别是 ListBindingList 但它应该与 DataTables 相同:

DataGridViewComboBoxColumn categoryColumn = (DataGridViewComboBoxColumn)_ItemsGrid.Columns["CategoryID"];
categoryColumn.DataSource = categories;

_ItemsGrid.DataSource = items;

I had a similar (I think) issue, and the solution for me was to set the DataSource for the DataGridViewComboBoxColumn before setting the DataSource for the DataGridView.

In my case, my DataSources are a List<T> and a BindingList<T> respectively but it should work the same with DataTables:

DataGridViewComboBoxColumn categoryColumn = (DataGridViewComboBoxColumn)_ItemsGrid.Columns["CategoryID"];
categoryColumn.DataSource = categories;

_ItemsGrid.DataSource = items;
千纸鹤 2024-12-15 01:00:50

好的,我想出了一个示例 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;
      }
   }

Ok, I came up with an example ClientInfo and InsuranceDetails that I think might mimic what you are trying to do. Let me know if these details arent quite right. This example will populate the DataGridViewComboBox and set the value based on the InsuranceDetails (specifically at: 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;
      }
   }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文