Wednesday, May 21, 2014

C# Sort generic list by column name


Data in DataTable can be sorted using string column name

DataView dv = new DataView(ds.Tables[0], "","SortExpression" + " " + "SortDirection", DataViewRowState.CurrentRows);

For list we cannot pass sort expression and direction in string format. We have to find the column using reflection and then sort the column as given below

PropertyInfo pi = typeof(Employee).GetProperty("[ColumnName]");
lstData.OrderBy(i => pi.GetValue(i, null)).ToList();


Given below is an example in ASP.NET
-------------------------------------------------------------
HTML (Add inside ‘form’ tags)
    <asp:DropDownList runat="server" ID="ddlSortColumn"></asp:DropDownList>
    <asp:DropDownList runat="server" ID="ddlSortDirection">
        <asp:ListItem Text="asc"></asp:ListItem>
        <asp:ListItem Text="desc"></asp:ListItem>
    </asp:DropDownList>
    <asp:Button runat="server" ID="btnReport" Text="Report"
        onclick="btnReport_Click" />
    <br />
    <asp:GridView runat="server" ID="grvData"></asp:GridView>

C# (CodeBehind)
protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                IList<PropertyInfo> props = new List<PropertyInfo>(typeof(Employee).GetProperties());
                ddlSortColumn.DataSource = props;
                ddlSortColumn.DataTextField = "Name";
                ddlSortColumn.DataValueField = "Name";
                ddlSortColumn.DataBind();

                grvData.DataSource = GetData();
                grvData.DataBind();
            }
        }
        protected void btnReport_Click(object sender, EventArgs e)
        {
            PropertyInfo pi = typeof(Employee).GetProperty(ddlSortColumn.SelectedValue);
            if (ddlSortDirection.SelectedItem.Text == "asc")
            {
                grvData.DataSource = GetData().OrderBy(i => pi.GetValue(i, null)).ToList();
            }
            else
            {
                grvData.DataSource = GetData().OrderByDescending(i => pi.GetValue(i, null)).ToList();
            }
            grvData.DataBind();
        }
        public List<Employee> GetData()
        {
            List<Employee> employees = new List<Employee>();
            employees.Add(new Employee() { Id = 1, Name = "Employee 1", Age = 25 });
            employees.Add(new Employee() { Id = 2, Name = "Employee 2", Age = 35 });
            employees.Add(new Employee() { Id = 3, Name = "Employee 3", Age = 39 });
            return employees;
        }
        public class Employee
        {
            public int Id { get; set; }
            public string Name { get; set; }
            public int Age { get; set; }
        }


No comments: