简体   繁体   English

如何添加 IList<ItemStatusClass> 在 DataGridView 的现有列中?

[英]How to add a IList<ItemStatusClass> in an cxisting column of DataGridView?

Script For DataGridView Retrieval is: DataGridView 检索脚本是:

DataSet ds1 = new DataSet();
isSqlString =  "SELECT [Item] AS itemName,Status as itemStatus From Items;"
SqlDataAdapter da = new SqlDataAdapter(isSqlString, isConnStr);
da.Fill(ds1, "items");
datagridview1.DataSource = ds1.Tables["items"].DefaultView;

IList:列表:

IList<ItemStatusClass> itemStatus = 
                    new List<ItemStatusClass>
                    {   
                         new ItemStatusClass { id = 0, name = "AAA" },
                         new ItemStatusClass { id = 1, name = "BBB" },
                         new ItemStatusClass { id = 2, name = "CCC" } 
                    };

I have added this by using below code:我使用以下代码添加了它:

DataGridViewComboBoxColumn statusCol = new DataGridViewComboBoxColumn();
statusCol.DataPropertyName = "itemStatus";
statusCol.Name = "Status";
statusCol.ReadOnly = false;
statusCol.DataSource = itemStatus;
statusCol.DisplayMember = "name";
statusCol.ValueMember = "id";
datagridview1.Columns.Add(statusCol);
datagridview1.Columns[2].DataPropertyName = "itemStatus";

I just needed to add a IList<ItemStatusClass> to an existing column of the DataGridView.我只需要将IList<ItemStatusClass>添加到 DataGridView 的现有列。

One more column is appearing in the last of the DataGridView with the value of the Existing Column.另一个列出现在 DataGridView 的最后一个,其值为 Existing Column。 But there is not change in Existing column, the value of the existing column is remains same.但 Existing column 没有变化,现有列的值保持不变。 Item Code column is in Hide.项目代码列在隐藏中。

在此处输入图片说明

As Shown in the above image there are two Status columns are displayed.如上图所示,显示了两个Status列。 1st status column is from SQL and second status column is which I have added programmatically.第一个状态列来自 SQL,第二个状态列是我以编程方式添加的。

I need the second status column has to be displayed as 2nd status column.我需要第二个状态列必须显示为第二个状态列。 Please let me out from this situation.请让我摆脱这种情况。

Disable the AutoGenerateColumns parameter禁用 AutoGenerateColumns 参数

datagridview1.AutoGenerateColumns = false

then add the column that you want.然后添加所需的列。

EDIT: In your code, there are two columns in datagridview1.Columns before the column that you add.编辑:在您的代码中,在您添加的列之前,datagridview1.Columns 中有两列。 They are created when you add the datasource with AutoGenerateColumns = true.它们是在您使用 AutoGenerateColumns = true 添加数据源时创建的。

Check, before loading the datasource, if AutoGenerateColumns is still false.在加载数据源之前检查 AutoGenerateColumns 是否仍然为 false。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM