none
Sort field will only show when data grid has data, then sort field will show and sort datagrid RRS feed

  • Question

  • I was wondering how would I achieve this:

    I have the Sort and Datagrid,  the sort is in its own asp panel and the datagrid is in its own panel

    once the datagrid has data, then the sort panel shows. then the end user can sort the Datagrid by up to 5 sort items. After 5 items then entire datagrid should go to all columns being sortable.

    I was wondering about the code behind code. As I got the HTML Visual webpart setup with the key parts, just need o figure out how to get the Dropdowns to sort the datagrid view.

    please let me know if you need more info...

    Thursday, August 29, 2019 11:01 PM

All replies

  • Hi,

    To sow sort panel, I had answered in your another thread.

    To sort data, you need sort by DataView sort function or LINQ and then rebind data to grid.

    You could check below threads for demo.

    https://stackoverflow.com/questions/21582725/c-sharp-how-to-use-orderby-with-multiple-columns-and-decode-column-number-to-co?rq=1

    http://www.codearsenal.net/2012/08/c-sharp-datatable-order-by-column.html#.XWi3aOgzaUk

    Best Regards,

    Lee


    Please remember to mark the replies as answers if they helped. If you have feedback for TechNet Subscriber Support, contact tnmff@microsoft.com.

    SharePoint Server 2019 has been released, you can click here to download it.
    Click here to learn new features. Visit the dedicated forum to share, explore and talk to experts about SharePoint Server 2019.

    Friday, August 30, 2019 6:06 AM
  • Lee or anyone:

    I was wondering what did I do wrong with the below code?

    As there could be a max of five dropdowns and then after five dropdowns the columns should all become sortable.
    each one of the five Dropdown could contain:
    PO Number
    Activity
    Vendor
    Project ID

    those items for the dropdown come from a SharePoint  list

    So DD1 contains PO Number

    and DD2 contains Project ID

    then it should sort by the first one then the second one.

    and if all five DD have items then it would sort by the 5 items

    here is what I have so far.

    here is the code ASPx page code

    <asp:GridView ID="gvSort" AutoGenerateEditButton="false" runat="server" AutoGenerateColumns="false" AlternatingRowStyle-BackColor="#B9DCFF" Width="100%">
    <Columns>
               <asp:TemplateField HeaderText="Sort Order" ItemStyle-Width="8%" HeaderStyle-HorizontalAlign="Left" ItemStyle-HorizontalAlign="Left">
    <ItemTemplate>
                    <asp:Label ID="lblSortOrder" runat="server" />
    </ItemTemplate>
    </asp:TemplateField>
                    <asp:TemplateField HeaderText="Field" ItemStyle-Width="15%" HeaderStyle-HorizontalAlign="Left" ItemStyle-HorizontalAlign="Left">
    <ItemTemplate>
                    <asp:DropDownList ID="ddlField" runat="server" Width="98%" Visible="False" OnSelectedIndexChanged="ddlField_SelectedIndexChanged"></asp:DropDownList>
                     <asp:Label ID="lblField" runat="server" Text='<%#Eval("Field") %>' Visible="true"></asp:Label>
    </ItemTemplate>
    </asp:TemplateField>
    </Columns>
    </asp:GridView>

    Code Behind:

    protected void btnSort_Click(object sender, EventArgs e)
            {

                DataTable dtResult = new DataTable();
                DataView sortOrder = dtResult.DefaultView;
    //sortOrder.Sort = "LINK_CODE, BUDGET_ID, UPLOADED DESC"; -> these parameters to be replaced by values of dropdowns in the gvSort gridview.

    if (ddl_ddlField.SelectedValue == "MenuGroup")
         sortOrder.Sort = "Menu_Group, Item_Name ASC";

     else if (ddl_ddlField.SelectedValue == "BUDGET_ID")
         sortOrder.Sort = "Item_BUDGET_ID, Menu_Group ASC";

     else if (ddl_ddlField.SelectedValue == "UPLOADED DESC")
         sortOrder.Sort = "Item_UPLOADED DESC, Item_Name ASC";

     else if (ddl_ddlField.SelectedValue == "Quantity")
         sortOrder.Sort = "Item_Quantity, Item_Name ASC";

    else if (ddl_ddlField.SelectedValue =="MenuGroup")
    sortOrder.Sort = "Menu_Group, Item_Name ASC";

                gvSort.DataSource = sortOrder;
                gvSort.DataBind();
            }




    • Edited by cowboy2066 Friday, August 30, 2019 8:33 PM
    Friday, August 30, 2019 5:18 PM
  • Lee:

    your two link, kind of point out some info, but it isn't really what I am doing.

    the dropdowns are the key, and there could be 5 of them.

    If you look at the code I posted below. it gives you an idea of what I am up to trying to do.


    • Edited by cowboy2066 Tuesday, September 3, 2019 3:38 PM
    Tuesday, September 3, 2019 3:38 PM
  • I have 5 Dropdown Lists, each of the dropdown lists have their data Supplied from a SharePoint List,
    The Datagrid/Dataview has a Search feature and the Data that is supplied originally needs to be sorted by the Dropdown Lists. 
    I want to have upto 5 of those Dropdown lists to Sort the Datagrid/Dataview.

    Is this Possible?

    Friday, October 4, 2019 7:27 PM
  • Hi,

    Below logic will set sort property only once, not sort by first and then second, then third etc.


    Fake logic to sort by multiple fields.

     DataTable SortedDT;
                OrderedEnumerableRowCollection<DataRow> ordered;
                if (ddl1.SelectedValue == "value" && ddl2.SelectedValue == "value" && ddl3.SelectedValue == "value" && ddl4.SelectedValue == "value")
                {
                    ordered = dt.AsEnumerable()
                   .OrderBy(en => en.Field<string>("Field1"))
                   .ThenBy(en => en.Field<int>("Field2"))
                   .ThenBy(en => en.Field<int>("Field3"))
                   .ThenBy(en => en.Field<int>("Field4"));
                    SortedDT = ordered.CopyToDataTable();
                }
                else
                {
                    if (ddl2.SelectedValue == "value" && ddl3.SelectedValue == "value" && ddl4.SelectedValue == "value")
                    {
                        ordered = dt.AsEnumerable()
                       .OrderBy(en => en.Field<string>("Field2"))
                       .ThenBy(en => en.Field<int>("Field3"))
                       .ThenBy(en => en.Field<int>("Field4"));
                        SortedDT = ordered.CopyToDataTable();
                    }
                    //...
                }
                //...
                gvSort.DataSource = SortedDT;
                gvSort.bind();

    Best Regards,

    Lee


    Please remember to mark the replies as answers if they helped. If you have feedback for TechNet Subscriber Support, contact tnmff@microsoft.com.

    SharePoint Server 2019 has been released, you can click here to download it.
    Click here to learn new features. Visit the dedicated forum to share, explore and talk to experts about SharePoint Server 2019.

    Monday, October 7, 2019 2:43 AM