使用DataGridViewComboboxColumn输入的麻烦 [英] Trouble using DataGridViewComboboxColumn for input

查看:584
本文介绍了使用DataGridViewComboboxColumn输入的麻烦的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有我使用的数据输入其中一个DataGridView。我与所有文本列前已经做到了这一点,和它的工作太棒了。但现在我想其中一列是一个数据绑定组合框,使用户可以选择的选项。当我这样做,得到的GridView的数据源的空行(但正确的数量)结束。我在想什么?

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?

下面是代码:

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

     gridDowntime.Columns.Add(cboCategory);
 }



然后代码获取GridView的数据源:

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?

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

Is there some specific additional information I should provide here?

推荐答案

下面是我刚熟了一个简单的示例项目。

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

我觉得你越来越错了最关键的事情是,在 DataGridViewComboboxColumn 需要参照 DataGridView的数据源 DataPropertyName 属性。C $ C>,而不是列

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;

}



绒毛在那里位 - 抓住的DataTable代码马上在interwebs。但这里的关键部分是组合框列属性的设置

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.

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

My datasource is a list of customertype objects:

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



所以我需要设置的DisplayMember在列名称和ValueMember以ID,因为这引用的列数据源。 然而作为DataPropertyName是将值设置为CustomerType,这是在数据表中的列,我们已经绑定到DataGridView的名称。

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.

所以之后的聊天讨论一丁点事实证明,上述情况确实存在,但你还需要确保该类型的ID列匹配。

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.

用于提供数据到ComboBox列中的数据表是从SQL查询生成并有型的id列您。依托数据表有id列做出像这样:

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));

这篇关于使用DataGridViewComboboxColumn输入的麻烦的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆