Sort items in datatable

后端 未结 5 776
臣服心动
臣服心动 2020-12-19 12:01

I have a datatable with one column that contain names.I want to load combobox with datatable such that names should be in alphabetic order for eg:first name starts with a. s

5条回答
  •  没有蜡笔的小新
    2020-12-19 12:46

    Method 1

        // orderby "FistName" column
        EnumerableRowCollection query = 
                        from order in datatable.AsEnumerable()
                        orderby datatable.Field("FirstName")
                        select datatable;
    
        DataView view = query.AsDataView();
    
        // bind to your combobox
        combobox.DataSource = view;
        combobox.DataBind()
    

    Method 2: if using DataSets

        DataTable datatable = dataSet.Tables["yourDataTable"];    
        DataView view = datatable .AsDataView();
    
        view.Sort = "FirstName desc, LastName desc";
    
        combobox.DataSource = view;
        combobox.DataBind();
    

    Reference : Sorting with DataView (LINQ to DataSet)

提交回复
热议问题