I am new to C# and SQL and I am using windows forms.
As shown in the screenshot I am saving items to SQL
table
and then I read them to create buttons
in flowLayoutPanel
. The following code works fine but the problem is : when I read the items and add them to the flowLayoutPanel
, they are added as they are in DataTable
(unsorted).
Anyone knows how to add the buttons
(Items) to flowLayoutPanel
and re-order them based on their ID
? I mean I want to add them to flowLayoutPanel
as : Item1 ,Item2, Item3, Item4 .
Thank you.
SqlConnection MyConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["MyConnectionString"].ConnectionString);
SqlCommand MyCommand = new SqlCommand();
DataTable DataTable = new DataTable();
SqlDataAdapter Sql_Data_Adapter = new SqlDataAdapter();
DataTable.Rows.Clear();
DataTable.Columns.Clear();
MyConnection.Open();
MyCommand.CommandText = "SELECT * FROM Table_Items";
MyCommand.Connection = MyConnection;
Sql_Data_Adapter.SelectCommand = MyCommand;
Sql_Data_Adapter.Fill(DataTable);
MyCommand.Parameters.Clear();
Sql_Data_Adapter.Dispose();
MyConnection.Close();
int RowIndex = DataTable.Rows.Count - 1;
for (int i = 0; i <= RowIndex; i++)
{
// create buttons
Button btn = new Button();
btn.Text = DataTable.Rows[i]"Item_Name"].ToString();
btn.Name = DataTable.Rows[i]["Item_Name"].ToString();
flowLayoutPanel1.Controls.Add(btn);
}