0

I created a Windows Forms application in C# and my database in Visual Studio. I want to know how, if it's possible, to sort one of the columns in the table by clicking a button? Or how can I sort this column automatically without using a button?

I've tried to implement this sort in the code below, but it doesn't work :(

private c void button5_Click(object sender, EventArgs e)
{
    SqlConnection sqlConnection = new SqlConnection("Here is my connecting string");

    SqlCommand myCommand = new SqlCommand("SELECT * FROM [Information] ORDER BY (Перевозчик)", sqlConnection);

    sqlConnection.Open();
    myCommand.ExecuteNonQuery();
}
Gerland L
  • 37
  • 1
Anastasia
  • 3
  • 4

2 Answers2

0

You need to do ExecuteDatareader then process the result coming from this call. ExecuteNonQuery cannot be used to retrieve data.

Joost Aarts
  • 673
  • 9
  • 19
0

As found here: https://www.w3schools.com/sql/sql_orderby.asp

Sine we don't know what you want to oder we can only guess here.

But with the query you could try:

SELECT column1, column2, ...
FROM table_name
ORDER BY column1, column2, ... ASC

In your case:

"SELECT * FROM [Information] ORDER BY (Перевозчик) ASC"

Best would be to use the visual query builder where you can query against your database. When you are happy with your result you can at least be sure that the query is correct. How you can do that is explained here:

https://www.c-sharpcorner.com/article/connect-to-a-database-from-visual-studio/

Since the sqlconnection and the sqlcommand is disposable you should consider putting it in a using tag, like in this example. https://learn.microsoft.com/en-us/dotnet/api/system.data.sqlclient.sqlcommand?view=netframework-4.8

private static void ReadOrderData(string connectionString)
{
    string queryString = 
        "SELECT OrderID, CustomerID FROM dbo.Orders;";
    using (SqlConnection connection = new SqlConnection(
               connectionString))
    {
        SqlCommand command = new SqlCommand(
            queryString, connection);
        connection.Open();
        using(SqlDataReader reader = command.ExecuteReader())
        {
            while (reader.Read())
            {
                Console.WriteLine(String.Format("{0}, {1}",
                    reader[0], reader[1]));
            }
        }
    }
}

There is 2 way for sort data

1) sorting just data and fill into grid:

DataGridView datagridview1 = new DataGridView(); // for show data
DataTable dt1 = new DataTable(); // have data
DataTable dt2 = new DataTable(); // temp data table
DataRow[] dra = dt1.Select("", "ID DESC");
if (dra.Length > 0)
    dt2 = dra.CopyToDataTable();
datagridview1.DataSource = dt2;

2) sort default view that is like of sort with grid column header:

DataGridView datagridview1 = new DataGridView(); // for show data
DataTable dt1 = new DataTable(); // have data
dt1.DefaultView.Sort = "ID DESC";
datagridview1.DataSource = dt1;

I found this solution here: Sorting rows in a data table

For a listbox you could give this a shot

ArrayList q = new ArrayList(); 
foreach (object o in listBox4.Items) 
        q.Add(o);
} 
q.Sort(); 
listBox5.Items.Clear();
foreach(object o in q){
    listBox5.Items.Add(o); 
}

I found this solution here: Sorting a list of items in a list box

Julian
  • 886
  • 10
  • 21
  • Thanks for your reply! I made the sql query on my vs, and saved it, but when i started my form application, my column was without sort, i try to sort my column by alphabet – Anastasia Nov 23 '19 at 18:35
  • I just added another solution which might be right what you need – Julian Nov 23 '19 at 18:49
  • I made the sql query in vs query builder, it was: SELECT * FROM [Information] ORDER BY (Перевозчик) in query result i saw what i need, but not on my form and the problem is that for show my table information i use only listbox, without datagriedview – Anastasia Nov 23 '19 at 18:53
  • I added a solution for a listbox – Julian Nov 23 '19 at 19:28
  • Thank you so much for your time :) I solved my problem – Anastasia Nov 23 '19 at 19:46
  • I'm glad it worked out. Can you accept the answer please. Have a nice day. – Julian Nov 24 '19 at 07:59