使用 DataGridViewComboboxColumn 进行输入时出现问题

发布于 2025-01-05 21:49:31 字数 749 浏览 2 评论 0原文

我有一个用于数据输入的 datagridview。我之前已经对所有文本列执行过此操作,效果很好。但现在我希望其中一列成为数据绑定组合框,以便用户可以选择选项。当我这样做时,生成的 gridview 的数据源最终会出现空行(但数量正确)。我缺少什么?

下面是代码:

 DataGridViewComboBoxColumn cboCategory = new DataGridViewComboBoxColumn();
 {
     cboCategory.HeaderText = "Category";
     cboCategory.DataSource = downtimeCategories;
     cboCategory.DisplayMember = "Name";
     cboCategory.ValueMember = "CategoryID";
     cboCategory.DataPropertyName = "CategoryID";

     gridDowntime.Columns.Add(cboCategory);
 }

然后是获取 gridview 数据源的代码:

DataTable dt = (gridDowntime.DataSource as DataTable);

每次我得到一个具有正确行数的表,但所有行都是空的(尽管它们是长行,但数据集可视化工具必须滚动以显示整个单元格)。我怎样才能找到错误并改正呢?

编辑:我应该在这里提供一些具体的附加信息吗?

I have a datagridview which I'm using for data entry. I've done this before with all text columns, and it worked great. But now I want one of the columns to be a databound combobox so the user can select options. When I do this, the resulting gridview's datasource ends up with empty rows (but the right quantity). What am I missing?

Here is code:

 DataGridViewComboBoxColumn cboCategory = new DataGridViewComboBoxColumn();
 {
     cboCategory.HeaderText = "Category";
     cboCategory.DataSource = downtimeCategories;
     cboCategory.DisplayMember = "Name";
     cboCategory.ValueMember = "CategoryID";
     cboCategory.DataPropertyName = "CategoryID";

     gridDowntime.Columns.Add(cboCategory);
 }

Then code to grab gridview's datasource:

DataTable dt = (gridDowntime.DataSource as DataTable);

Everytime I get a table with the correct number of rows, but all the rows are empty (although they are long rows, the dataset visualizer has to scroll to show the entire cell). How can I find the error and correct?

EDIT: Is there some specific additional information I should provide here?

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

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

发布评论

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

评论(1

落墨 2025-01-12 21:49:31

这是我刚刚编写的一个简单的示例项目。

我认为您错误的关键是 DataGridViewComboboxColumnDataPropertyName 属性需要引用 DataGridView 的数据源,而不是柱子。

public Form1()
{
    InitializeComponent();

    DataTable dt = new DataTable("Customers");
    DataColumn dc;

    dc = new DataColumn();
    dc.DataType = typeof(int);
    dc.ColumnName = "CustomerID";

    dt.Columns.Add(dc);
    dt.Columns.Add(new DataColumn("LastName"));
    dt.Columns.Add(new DataColumn("FirstName"));
    // Concatenation of first and last names 
    dt.Columns.Add(new DataColumn("FullName"));
    dt.Columns.Add(new DataColumn("Address"));
    dt.Columns.Add(new DataColumn("City"));
    dt.Columns.Add(new DataColumn("State"));
    dt.Columns.Add(new DataColumn("Zip"));
    dt.Columns.Add(new DataColumn("Phone"));

    dc = new DataColumn();
    dc.DataType = typeof(DateTime);
    dc.ColumnName = "LastPurchaseDate";
    dt.Columns.Add(dc);

    dc = new DataColumn();
    dc.DataType = typeof(int);
    dc.ColumnName = "CustomerType";
    dt.Columns.Add(dc);

    // Populate the table 
    dt.Rows.Add(2, "Baggins", "Bilbo", "Baggins, Bilbo", "Bagshot Row #1", "Hobbiton", "SH", "00001", "555-2222", DateTime.Parse("24/9/2008"), 1);
    dt.Rows.Add(1, "Baggins", "Frodo", "Baggins, Frodo", "Bagshot Row #2", "Hobbiton", "SH", "00001", "555-1111", DateTime.Parse("14/9/2008"), 1);
    dt.Rows.Add(6, "Bolger", "Fatty", "Bolger, Fatty", "ProudFeet Creek", "Hobbiton", "SH", "00001", "555-1111", DateTime.Parse("14/9/2008"), 1); 
    dt.Rows.Add(4, "Elessar", "Aragorn", "Elessar, Aragorn", "Citadel", "Minas Tirith", "Gondor", "00000", "555-0000", DateTime.Parse("14/9/2008"), 4);
    dt.Rows.Add(5, "Evenstar", "Arwin", "Evenstar, Arwin", "Citadel", "Minas Tirith", "Gondor", "00000", "555-0001", DateTime.Parse("23/9/2008"), 4);
    dt.Rows.Add(3, "Greyhame", "Gandalf", "Grayhame, Gandalf", DBNull.Value, DBNull.Value, DBNull.Value, DBNull.Value, DBNull.Value, DBNull.Value, 3);

    DataGridViewComboBoxColumn cboCategory = new DataGridViewComboBoxColumn();

    List<CustomerType> customerTypes = new List<CustomerType> { new CustomerType { Id = 1, Name = "Good" }, new CustomerType { Id = 4, Name = "Bad" }, new CustomerType { Id = 3, Name = "Ugly" } };

    cboCategory.HeaderText = "Customer Type";
    cboCategory.DataSource = customerTypes;
    cboCategory.DisplayMember = "Name";
    cboCategory.ValueMember = "Id";
    cboCategory.DataPropertyName = "CustomerType";

    dataGridView1.Columns.Add(cboCategory);

    dataGridView1.DataSource = dt;

}

那里有一些绒毛——直接从互联网上抓取了数据表代码。但这里的关键部分是组合框列的属性设置。

我的数据源是 customertype 对象的列表:

public class CustomerType
{
    public int Id { get; set; }
    public string Name { get; set; }
}

因此我需要将列上的 DisplayMember 设置为“Name”,将 ValueMember 设置为“Id”,因为这引用了列数据源。 但是 DataPropertyName 的值设置为“CustomerType”,这是我们绑定到 DataGridView 的 DataTable 中的列的名称。


因此,经过聊天中的一点讨论后,发现上述情况是正确的,但您还需要确保 id 列的类型匹配。

用于向组合框列提供数据的数据表是从 sql 查询生成的,并且具有 in 类型的 id 列。您的支持数据表的 id 列如下所示:

dt.Columns.Add(new DataColumn("Category")); 

这默认为字符串类型的列。尝试类似的方法:

downtimeEntries.Columns.Add("Category", typeof(int));

Here is a simple example project that I just cooked up.

The key thing I think you are getting wrong is that the DataPropertyName property of the DataGridViewComboboxColumn needs to refer to the datasource of the DataGridView, not the column.

public Form1()
{
    InitializeComponent();

    DataTable dt = new DataTable("Customers");
    DataColumn dc;

    dc = new DataColumn();
    dc.DataType = typeof(int);
    dc.ColumnName = "CustomerID";

    dt.Columns.Add(dc);
    dt.Columns.Add(new DataColumn("LastName"));
    dt.Columns.Add(new DataColumn("FirstName"));
    // Concatenation of first and last names 
    dt.Columns.Add(new DataColumn("FullName"));
    dt.Columns.Add(new DataColumn("Address"));
    dt.Columns.Add(new DataColumn("City"));
    dt.Columns.Add(new DataColumn("State"));
    dt.Columns.Add(new DataColumn("Zip"));
    dt.Columns.Add(new DataColumn("Phone"));

    dc = new DataColumn();
    dc.DataType = typeof(DateTime);
    dc.ColumnName = "LastPurchaseDate";
    dt.Columns.Add(dc);

    dc = new DataColumn();
    dc.DataType = typeof(int);
    dc.ColumnName = "CustomerType";
    dt.Columns.Add(dc);

    // Populate the table 
    dt.Rows.Add(2, "Baggins", "Bilbo", "Baggins, Bilbo", "Bagshot Row #1", "Hobbiton", "SH", "00001", "555-2222", DateTime.Parse("24/9/2008"), 1);
    dt.Rows.Add(1, "Baggins", "Frodo", "Baggins, Frodo", "Bagshot Row #2", "Hobbiton", "SH", "00001", "555-1111", DateTime.Parse("14/9/2008"), 1);
    dt.Rows.Add(6, "Bolger", "Fatty", "Bolger, Fatty", "ProudFeet Creek", "Hobbiton", "SH", "00001", "555-1111", DateTime.Parse("14/9/2008"), 1); 
    dt.Rows.Add(4, "Elessar", "Aragorn", "Elessar, Aragorn", "Citadel", "Minas Tirith", "Gondor", "00000", "555-0000", DateTime.Parse("14/9/2008"), 4);
    dt.Rows.Add(5, "Evenstar", "Arwin", "Evenstar, Arwin", "Citadel", "Minas Tirith", "Gondor", "00000", "555-0001", DateTime.Parse("23/9/2008"), 4);
    dt.Rows.Add(3, "Greyhame", "Gandalf", "Grayhame, Gandalf", DBNull.Value, DBNull.Value, DBNull.Value, DBNull.Value, DBNull.Value, DBNull.Value, 3);

    DataGridViewComboBoxColumn cboCategory = new DataGridViewComboBoxColumn();

    List<CustomerType> customerTypes = new List<CustomerType> { new CustomerType { Id = 1, Name = "Good" }, new CustomerType { Id = 4, Name = "Bad" }, new CustomerType { Id = 3, Name = "Ugly" } };

    cboCategory.HeaderText = "Customer Type";
    cboCategory.DataSource = customerTypes;
    cboCategory.DisplayMember = "Name";
    cboCategory.ValueMember = "Id";
    cboCategory.DataPropertyName = "CustomerType";

    dataGridView1.Columns.Add(cboCategory);

    dataGridView1.DataSource = dt;

}

Bit of fluff in there - grabbed that datatable code right off the interwebs. But the key part here is the setting of properties for the combobox column.

My datasource is a list of customertype objects:

public class CustomerType
{
    public int Id { get; set; }
    public string Name { get; set; }
}

So I need to set DisplayMember on the column to "Name" and ValueMember to "Id" since this references the columns datasource. However for the DataPropertyName was set the value to "CustomerType" which is the name of a column in the DataTable that we have bound to the DataGridView.


So after a wee bit of discussion in chat it turns out that the above is true but that you also need to ensure that the types of your id columns match.

The datatable used to provide data to the combobox column was generated from a sql query and had the id column of type in. Your backing datatable had the id column made like so:

dt.Columns.Add(new DataColumn("Category")); 

This defaults to a column of type string. Try instead something like:

downtimeEntries.Columns.Add("Category", typeof(int));
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文