我有一个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);
每当我得到一个具有正确行数的表,但所有行都是空的(尽管它们是长行,数据集可视化工具必须滚动以显示整个单元格)。如何找到错误并更正?
编辑:我在这里应该提供一些具体的补充信息吗?
答案 0 :(得分:1)
这是一个我刚刚编写的简单示例项目。
我认为你出错的关键是DataPropertyName
的{{1}}属性需要引用DataGridViewComboboxColumn
的数据源,而不是列。
DataGridView
那里有一点点毛茸茸 - 抓住了互联网上的数据代码。但这里的关键部分是组合框列的属性设置。
我的数据源是customrtype对象的列表:
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;
}
所以我需要将列上的DisplayMember设置为“Name”,将ValueMember设置为“Id”,因为它引用了列数据源。 DataPropertyName的但是将值设置为“CustomerType”,这是我们绑定到DataGridView的DataTable中列的名称。
所以在聊天中经过一番讨论之后,结果证明上述情况属实,但您还需要确保您的id列的类型匹配。
用于向组合框列提供数据的数据表是从sql查询生成的,并且具有类型为的id列。您的支持数据表具有如下所示的id列:
public class CustomerType
{
public int Id { get; set; }
public string Name { get; set; }
}
默认为string类型的列。尝试改为:
dt.Columns.Add(new DataColumn("Category"));