简体   繁体   中英

Sorting DataTable column is not working in C#

I am new to C# and I am using windows forms.

I have Form1 which has button , Datagridview and flowLayoutPanel . I have large address table in SQL (400,000 records) I am trying to import house numbers based on a specific given Postcode and then assign those numbers to button text as shown below:

     SqlConnection MyConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["MyConnectionString"].ConnectionString);
     SqlCommand MySQLCommand = new SqlCommand();
     DataTable Data_Table1 = new DataTable();
     SqlDataAdapter SQLDataAdapter = new SqlDataAdapter();


  private void button1_Click(object sender, EventArgs e)
    {

        string PostCode = "L8 8HN";

        Data_Table1.Rows.Clear();
        Data_Table1.Columns.Clear();  
        MyConnection.Open();          
        MySQLCommand.CommandText = "SELECT * FROM PC where Postcode= '" + PostCode + "'";

        MySQLCommand.Connection = MyConnection;
        SQLDataAdapter.SelectCommand = MySQLCommand;
        SQLDataAdapter.Fill(Data_Table1);
        MySQLCommand.Parameters.Clear();
        SQLDataAdapter.Dispose();
        MyConnection.Close();


        // Sort the "Building Number" column. 
        DataTable dt2 = Data_Table1.Clone();
        dt2.Columns["Building Number"].DataType =    Type.GetType("System.Int32");
        foreach (DataRow dr in Data_Table1.Rows)
        {
            dt2.ImportRow(dr);
        }
        dt2.AcceptChanges();
        DataView dv = dt2.DefaultView;
        dv.Sort = "Building Number ASC";


        //Create buttons and make Building Number as text.
       int RowIndex = dt2.Rows.Count - 1;
        for (int i = 0; i <= RowIndex; i++)
        {
            Button btn = new Button();
            btn.Text = dt2.Rows[i]["Building Number"].ToString();
            btn.Width = 50;
            btn.Height = 50;        
            flowLayoutPanel1.Controls.Add(btn);
        }

        // check if the column "Building Number" is sorted or not.
        dataGridView1.DataSource = dt2;

}

When I click on button1 , the SQL query brings all the Building Numbers based on a given Postcode, then sort the Building Numbers and after that create new buttons and make their text based on Building Numbers and at same time show the dt2 in datagridview .

  • The issue:

屏幕截图

As shown in the screen shot, the column "Building Number" in datagridview is sorted as I wanted but when I loop throw dt2 and assign buttons text from dt2 then in this case the buttons are not sorted in the correct way. The buttons should be sorted in the right way as in the datagridview

Note that the column "Building Number" in SQL table is type of string. Also I am using datagridview just to see if the column is sorted or not.

I had a look here , here and here but none of them helped me.

Please if anyone knows how can I get those buttons sorted in the correct way starting from 1, 2, 3, 4, 5, 6, 7...

Thank you

You are creating an ordered DataView , but in the loop where buttons are being created, you are using dt2 which is unordered. You should change that loop to something like:

for (int i = 0; i <= RowIndex; i++)
{
    Button btn = new Button();
    btn.Text = dv[i]["Building Number"].ToString();
    btn.Width = 50;
    btn.Height = 50;        
    flowLayoutPanel1.Controls.Add(btn);
}

As a better alternative, instead of all the steps you go through to order your data, why don't you get it already ordered in your sql query?

MySQLCommand.CommandText = "SELECT * FROM PC where Postcode= '" + PostCode + "' order by Building Number";

I also recommend you to use parametrized queries instead of concatenating strings to avoid SQL Injection

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM