简体   繁体   English

C#动态LINQ查询

[英]c# dynamic LINQ query

In my winform application, I am adding combobox column in datagridview and selecting the combobox item using the following code: 在我的winform应用程序中,我将在datagridview中添加combobox列,并使用以下代码选择combobox项:

var entityModel= new AdminEntities();
DataGridViewComboBoxColumn cboIsNew = new DataGridViewComboBoxColumn();
var isNew = (from a in entityModel.TOWERs select a.ISNEW).Distinct().OrderBy(x => x);
cboIsNew.Items.AddRange(isNew.ToArray());
int i = dgvLoadTable.Columns["ISNEW"].Index;
dgvLoadTable.Columns.Insert(i, dgvCol);
dgvLoadTable.Columns[i].HeaderText = dgvLoadTable.Columns[i + 1].HeaderText;
dgvLoadTable.Columns[i + 1].Visible = false;

Is there a way to do this by a simple function in which I will pass the tablename and column name/index only? 有没有一种方法可以通过一个简单的函数来实现,即仅传递表名和列名/索引? Also, If I do this: 另外,如果我这样做:

dgvLoadTable.Columns.Insert(i, new DataGridViewComboBoxColumn());

Then how do I add item to this dynamically created combobox? 然后如何将项目添加到此动态创建的组合框? I tried to add items like the following, but it doesn't work: 我试图添加类似以下内容的项目,但是它不起作用:

 cboIsNew.Items.AddRange((from a in entityModel.TOWERs select a.ISNEW).Distinct().OrderBy(x => x))

Any help will be appreciated. 任何帮助将不胜感激。

Try this: 尝试这个:

cboIsNew.Items.AddRange((from a in entityModel.TOWERs select a.ISNEW)
  .Distinct().Select( x => new DataGridViewComboBoxColumn() ).ToList());

You would need to see the DataGridViewComboBoxColumn with specific values using x as the source, but the way I've layed out should create a List of objects that should be able to be added. 您将需要使用x作为源查看具有特定值的DataGridViewComboBoxColumn,但是我布局的方式应该创建一个应该添加的对象列表。

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

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