Search code examples
c#asp.netdatatabledatasourcerowfilter

How to retrieve object Column from DataSource RowFilter


I'm currently trying to retrieve a column from a DataTable that contains columns with object inside some certains columns.

Let see :

(this.CohortGrid.DataSource as DataTable).DefaultView.RowFilter =  string.Format("CohortFormation.Name LIKE '*{0}*'", ddlFormation.SelectedItem.Text);

I got these columns inside my DataTable :

[0] : Id

[1] : Name

[2] : Status

[3] : CohortFormation

[4] : RoomCol

[5] : InstructorCol

[6] : EmployeeCol

The column CohortFormation is an object that contains a Id and a Name.

So, I'm trying to retrieve the Name of CohortFormation like this CohortFormation.Name LIKE but it return me :

Unable to find column

In the Grid View TemplateField I can do <%# Eval("CohortFormation.Name") %> and it works pretty well. But in the code behind how can I do this?


Solution

  • I just found a solution and it's working pretty well !

    I loop through the DataTable and remove the rows that is not equals to the value that is selected in the DropDownList -> ddlFormation.

    There is the code :

      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();
    

    So, the trick is to loop on the list and not the DataTable and if it's not equals we remove the object. Finally, we bind the data.

    It works perfectly !