DataGridView ComboBox 永远不会填充

发布于 2024-11-02 13:25:20 字数 2040 浏览 1 评论 0原文

我的 DataGridView 中有一个列是一个 ComboBox,但我无法填充它。我一遍又一遍地查看我的代码,它似乎是正确的,但不可能,因为 ComboBox 从未填充。

这是我的代码。

首先,用于测试的静态数据源:

List<Phone> PhoneList = new List<Phone>();

Phone p1 = new Phone();
p1.PhoneID = 1;
p1.PhoneTypeID = 2;
p1.AreaCode = "333";
p1.Number = "123-1234";
p1.PhoneTypeName = "Primary";
PhoneList.Add( p1 );

Phone p2 = new Phone();
p2.PhoneID = 2;
p2.PhoneTypeID = 2;
p2.AreaCode = "444";
p2.Number = "432-8900";
p2.PhoneTypeName = "Secondary";
PhoneList.Add( p2 );

这只是为了显示 ComboBox 列的 DataPropertyName:

this.dgvClinic.Columns[ "PhoneName" ].DataPropertyName = "PhoneID";

接下来,我将提取数据,然后对于其中的每一行,我将一行添加到 DataGridView 并填充新添加的单元格排。最后是我的 ComboBox 列,如您所见,我正在填充我的 DataSource、DisplayMember 和 ValueMember 属性。请注意最后的三个注释掉的行,因为我什至尝试将静态值添加到单元格的 Item 集合中。这两种方法都不起作用。所发生的情况是前三列有数据,但 ComboBox 单元格从来没有任何数据。

Clinic c = new Clinic();
string CurrentLocation = string.Empty;
foreach ( Clinic i in c.SelfListAll() )
{
    Location_List_ByClinicResult l = i.Locations.FirstOrDefault<Location_List_ByClinicResult>();
    if ( l == null )
    {
        CurrentLocation = "12345 Main Street" + " " + "San Rafael" + ", " + "CA" + " " + "94903";
    }
    else
    {
        CurrentLocation = l.Address1 + " " + l.City + ", " + l.StateAbbrev + " " + l.Zip;
    }

    RowIndex = this.dgvClinic.Rows.Add();

    this.dgvClinic.Rows[ RowIndex ].Cells[ "ClinicID" ].Value = i.ClinicID.ToString();
    this.dgvClinic.Rows[ RowIndex ].Cells[ "ClinicName" ].Value = i.Name;
    this.dgvClinic.Rows[ RowIndex ].Cells[ "LocationName" ].Value = CurrentLocation;

    DataGridViewComboBoxCell cell = this.dgvClinic.Rows[ RowIndex ].Cells[ "PhoneName" ] as DataGridViewComboBoxCell;
    cell.DataSource = PhoneList;
    cell.DisplayMember = "Number";
    cell.ValueMember = "PhoneID";
    //cell.Items.Add( "One" );
    //cell.Items.Add( "Two" );
    //cell.Items.Add( "Three" );
}

我真的希望有人能看到我在这里缺少的东西。顺便说一句,我已经在设计器中创建了列。

谢谢。

I have a column in a DataGridView that is a ComboBox, and I cannot get it to populate. I have looked at my code over and over again, and it seems right, but can't be because the ComboBox is never populated.

Here is my code.

First, a static DataSource for testing:

List<Phone> PhoneList = new List<Phone>();

Phone p1 = new Phone();
p1.PhoneID = 1;
p1.PhoneTypeID = 2;
p1.AreaCode = "333";
p1.Number = "123-1234";
p1.PhoneTypeName = "Primary";
PhoneList.Add( p1 );

Phone p2 = new Phone();
p2.PhoneID = 2;
p2.PhoneTypeID = 2;
p2.AreaCode = "444";
p2.Number = "432-8900";
p2.PhoneTypeName = "Secondary";
PhoneList.Add( p2 );

This just to show the DataPropertyName of the ComboBox column:

this.dgvClinic.Columns[ "PhoneName" ].DataPropertyName = "PhoneID";

Next, I'm pulling data and then for each row of it, I am adding a row to the DataGridView and populating the cell for the newly added row. At the end is my ComboBox column, and as you can see, I'm populating my DataSource, DisplayMember and ValueMember properties. Please notice the three commented out rows at the end as I even tried adding static values to the cell's Item collection. Neither approach worked. All that happens is that the first three columns have data, but the ComboBox cell never has any data.

Clinic c = new Clinic();
string CurrentLocation = string.Empty;
foreach ( Clinic i in c.SelfListAll() )
{
    Location_List_ByClinicResult l = i.Locations.FirstOrDefault<Location_List_ByClinicResult>();
    if ( l == null )
    {
        CurrentLocation = "12345 Main Street" + " " + "San Rafael" + ", " + "CA" + " " + "94903";
    }
    else
    {
        CurrentLocation = l.Address1 + " " + l.City + ", " + l.StateAbbrev + " " + l.Zip;
    }

    RowIndex = this.dgvClinic.Rows.Add();

    this.dgvClinic.Rows[ RowIndex ].Cells[ "ClinicID" ].Value = i.ClinicID.ToString();
    this.dgvClinic.Rows[ RowIndex ].Cells[ "ClinicName" ].Value = i.Name;
    this.dgvClinic.Rows[ RowIndex ].Cells[ "LocationName" ].Value = CurrentLocation;

    DataGridViewComboBoxCell cell = this.dgvClinic.Rows[ RowIndex ].Cells[ "PhoneName" ] as DataGridViewComboBoxCell;
    cell.DataSource = PhoneList;
    cell.DisplayMember = "Number";
    cell.ValueMember = "PhoneID";
    //cell.Items.Add( "One" );
    //cell.Items.Add( "Two" );
    //cell.Items.Add( "Three" );
}

I'm really hoping that someone can see what I am missing here. By the way, I have created the columns in the designer.

Thanks.

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

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

发布评论

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

评论(2

神回复 2024-11-09 13:25:20
cell.DataSource = PhoneList;
cell.DisplayMember = "Number";
cell.ValueMember = "PhoneID";

看看这部分,我可以观察到您正在将电话列表设置为单元格的数据源。现在,DisplayMemberValueMember 属性将在 PhoneList 中查找 "Number""PhoneID"。但实际上 PhoneList 没有任何名为 "Number""PhoneID" 的列,而是 PhoneList 的子 p1 和 p2 有这些列。因此,它将无法在数据源电话列表中找到列("Number""PhoneID")。

尝试将其填充到数据表中并将其分配给单元格。

DataTable dt = new DataTable();
//Add Row p1
//Add Row p2

现在分配它们。

  cell.DataSource = dt;
  cell.DisplayMember = "Number";
  cell.ValueMember = "PhoneID";

另请参阅此链接

cell.DataSource = PhoneList;
cell.DisplayMember = "Number";
cell.ValueMember = "PhoneID";

Taking look at this part, what I can observe is you are setting Phone list as DataSource to cell. Now the DisplayMember and ValueMember properties will look for columns "Number" and "PhoneID" in PhoneList. But actually PhoneList doesn't have any columns called "Number" and "PhoneID", rather PhoneList's child p1 and p2 has these columns. So it will fail to find the columns ("Number" and "PhoneID") in the Datasource Phonelist.

Try populating it in a datatable and assign it to cell.

DataTable dt = new DataTable();
//Add Row p1
//Add Row p2

and now assign them.

  cell.DataSource = dt;
  cell.DisplayMember = "Number";
  cell.ValueMember = "PhoneID";

Also see this link

宣告ˉ结束 2024-11-09 13:25:20

您是否尝试过像这样使用 DataGridViewComboBoxColumn:

DataGridViewComboBoxColumn column = new DataGridViewComboBoxColumn();
column.DataSource = PhoneList;
column.DataPropertyName = "PhoneID";
column.MaxDropDownItems = 4;
column.DisplayStyle = DataGridViewComboBoxDisplayStyle.ComboBox;
yourdatagridview.Columns.Add(column);

也许它的工作方式是组合框应该包含 datagridview 中所有行的相同列表。至少您会知道当您对所有行使用一个 PhoneList 时它是否有效。

Have you tried using DataGridViewComboBoxColumn like this:

DataGridViewComboBoxColumn column = new DataGridViewComboBoxColumn();
column.DataSource = PhoneList;
column.DataPropertyName = "PhoneID";
column.MaxDropDownItems = 4;
column.DisplayStyle = DataGridViewComboBoxDisplayStyle.ComboBox;
yourdatagridview.Columns.Add(column);

Maybe it works in that way that combobox should contain the same list for all rows in a datagridview. At least you will know does it work when you use one PhoneList for all rows.

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