none
Sorted Datatable value differs in UI gridview in c# RRS feed

  • Question

  • I have a datatable with sorted column values and when I bind them to the gridview I'm not able to see the same order in the gridview like I have in the datatable.

    Sorting event is also disable in gridview.

    Can anyone please provide suggestions.

    Code:    

    DataTable dtColumns = FromDataTable();
    GridView1.DataSource = dtColumns;
    GridView1.DataBind();
    dtColumns datatable have sorted data and I when I bind them to the gridview the order changes.

    • Edited by vickyto Monday, January 8, 2018 3:51 PM
    • Moved by CoolDadTx Tuesday, January 9, 2018 2:48 PM ASP.NET related
    Monday, January 8, 2018 3:41 PM

All replies

  • Are you sure datatable is sorted and is in correct order? Most probably datatable is not sorted. So recheck/debug datatable code. Or share your code here. 

    Gaurav Khanna | Microsoft MVP | Microsoft Community Contributor

    Monday, January 8, 2018 4:46 PM
  • Perhaps trying to sort then use the view and sort

    dtColumns.DefaultView.Sort = "SomeFieldToSortOnGoesHere";
    GridView1.DataSource = dtColumns.DefaultView;


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    Tuesday, January 9, 2018 2:31 AM
  • Hello vickyto,

    How do you define the FromDataTable method? There is a simple example that works well on my side.

     private DataTable FromDataTable()
            {
                DataTable dt = new DataTable();
    
               // Data Source = (LocalDB)\MSSQLLocalDB; AttachDbFilename = "xxx\Database1.mdf"; Integrated Security = True
    
                string connStr= @"Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename='C:\Users\v-feih\source\repos\12_20_2017\Windows Form\Database1.mdf';Integrated Security=True";
                try
                {
                    using (SqlConnection conn = new SqlConnection(connStr))
                    {
    
                        conn.Open();
                        SqlDataAdapter da = new SqlDataAdapter("select * from Tb2 order by Mark", conn);
                        da.Fill(dt);
                    }
                }
                catch (Exception e) {
                    MessageBox.Show($"Exception: {e.Message}");
                }
                return dt;
            }

    The result

    Best regards,

    Neil Hu


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Tuesday, January 9, 2018 3:17 AM
  • GridView is an ASP.NET control. Please post questions related to ASP.NET in the ASP.NET forums.

    Michael Taylor http://www.michaeltaylorp3.net

    Tuesday, January 9, 2018 2:48 PM