C# 如何从数据源行筛选器中检索对象列
我目前正在尝试从DataTable中检索一个列,该DataTable包含在某些特定列中带有object的列 让我们看看:C# 如何从数据源行筛选器中检索对象列,c#,asp.net,datatable,datasource,rowfilter,C#,Asp.net,Datatable,Datasource,Rowfilter,我目前正在尝试从DataTable中检索一个列,该DataTable包含在某些特定列中带有object的列 让我们看看: (this.CohortGrid.DataSource as DataTable).DefaultView.RowFilter = string.Format("CohortFormation.Name LIKE '*{0}*'", ddlFormation.SelectedItem.Text); 我在数据表中得到了以下列: [0]:Id [1] :姓名 [2] :状态
(this.CohortGrid.DataSource as DataTable).DefaultView.RowFilter = string.Format("CohortFormation.Name LIKE '*{0}*'", ddlFormation.SelectedItem.Text);
我在数据表中得到了以下列:
[0]:Id
[1] :姓名
[2] :状态
[3] :共态
[4] :RoomCol
[5] :教官
[6] :EmployeeCol
列组合是一个包含Id和名称的对象
所以,我试着检索像这样的同伙的名字。像这样的名字
但它回报了我:
找不到列
在Grid View TemplateField中,我可以做,而且它工作得很好。但在代码背后,我怎么能做到这一点 我刚刚找到了一个解决方案,效果非常好 我循环遍历DataTable并删除与DropDownList->DDL信息中选择的值不相等的行 代码如下:
CohortCollection cohortCol = (CohortCollection)Session["cohortCol"];
foreach(Cohort cohort in cohortCol.ToList())
{
if(cohort.Status.Id != int.Parse(ddlFormation.SelectedItem.Value))
{
cohortCol.Remove(cohort);
}
}
this.CohortGrid.DataSource = cohortCol;
this.CohortGrid.DataBind();
所以,诀窍是循环列表而不是数据表,如果它不等于,我们就删除对象。最后,我们绑定数据
它工作得很好