Furkan Ayık Furkan Ayık - 1 year ago 133
ASP.NET (C#) Question

Filtering Data Table as Repeater DataSource (asp. net)

I have nested repaters for group the data. All data in same table and they all have QUESTION_GROUP column. I'm passing group name from parent repeater for child repeater in Parent's Item Data Bound. But i cant set it as child repeater data source. How can i filter multiple data from Data Table for repeater DataSource?

protected void ParentRepeaterDataBound(object sender, RepeaterItemEventArgs e)
Repeater rp = (Repeater)e.Item.FindControl("ChildRepeater");
rp.DataSource = FixedQuestions.Select("QUESTION_GROUP='" + DataBinder.Eval(e.Item.DataItem, "Q_Group").ToString() + "'");

Q_Group is coming from parent repeater. QUESTION_GROUP is a column in fixedquestions(DataTable).

I'm asking, How can i get a part of Data Table which filtered by column(For Repeater Data Source)?

Answer Source

You can use Linq to filter the table before binding it to the nested Repeater.

rp.DataSource = FixedQuestions.AsEnumerable().Where(x => x.Field<string>("columnName") == "myValue").CopyToDataTable();

The above snippet will work as long as there are remaining rows after filtering. Otherwise you will get a "The source contains no DataRows" error. The below snippet will check if there are any rows before calling CopyToDataTable().

        DataTable dtFiltered = new DataTable();
        var dtFilteredTemp = FixedQuestions.AsEnumerable().Where(X => X.Field<string>("columnName") == "myValue");
        if (dtFilteredTemp.AsDataView().Count > 0)
            dtFiltered = dtFilteredTemp.CopyToDataTable();
        rp.DataSource = dtFiltered;
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download