1

I have some problems with dynamic button in c#. I have developed an application for Pos and I need to create group of product but when I click on group button I have to show product sorting by name of product group.

This is my code:

private void Pos_Load(object sender, EventArgs e)
{
    MySqlConnection conn = new MySqlConnection("server=localhost;user id=root;database=programmj;allowuservariables=True");
    conn.Open();

    string query = string.Format("SELECT * FROM priduct_group");

    MySqlCommand cmd = new MySqlCommand(query);
    DataTable dt = new DataTable();
    DataSet ds = new DataSet();

    MySqlDataAdapter adapter = new MySqlDataAdapter(query, conn);
    adapter.Fill(dt);

    ds.Tables.Add(dt);

    int top = 0;
    int left = 4;

    foreach (DataRow dr in dt.Rows)
    {
             Button button = new Button();
             button.FlatStyle = FlatStyle.Flat;
             button.BackColor = Color.Gold;
             button.Text = dr[1].ToString();
             button.Font = new Font("Microsoft Sans Serif", 20, FontStyle.Bold);
             button.Size = new Size(170, 85);
             button.Left = left;
             button.Top = top;
             panel8.Controls.Add(button); // here
             top += button.Height + 2;
             button.Click += new System.EventHandler(Button_Click);
    }
}

And this is my another code for displaying product

private void Button_Click(object sender, EventArgs e)
{
    MySqlConnection conn = new MySqlConnection("server=localhost;user id=root;database=programmj;allowuservariables=True");
    conn.Open();

    string query = string.Format("SELECT * FROM tblartikujt ");
    MySqlCommand cmd = new MySqlCommand(query);

    DataTable dt = new DataTable();
    DataSet ds = new DataSet();

    MySqlDataAdapter adapter = new MySqlDataAdapter(query, conn);
    adapter.Fill(dt);

    ds.Tables.Add(dt);

    int top = 0;
    int left = 4;

    foreach (DataRow dr in dt.Rows)
    {
            panel5.Dock = DockStyle.Right;
            Button button = new Button();
            button.Padding = new Padding(20, 3, 20, 3);
            button.FlatStyle = FlatStyle.Flat;
            button.ForeColor = Color.White;
            button.BackColor = Color.Green;
            button.Text = dr[3].ToString();
            button.Text = dr[10].ToString();
            button.Font = new Font("Microsoft Sans Serif", 14, FontStyle.Bold);
            button.Size = new Size(200, 85);
            button.Left = left;
            button.Top = top;
            panel5.Controls.Add(button); // here
            top += button.Height + 2;
    }
}
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Gimm
  • 21
  • 2
  • 2
    You say you have a problem but you didn't explain what problem that is.... – rene Feb 07 '15 at 19:13
  • My problem is that cant display product sort by product name ? i have on table product , and on table product type i need to show product by product type ? do you understand me ? – Gimm Feb 07 '15 at 19:15
  • OK...and what part of your code is trying to do that because I see no sort code anywhere, only adding buttons to, I have to assume, different panels.... – rene Feb 07 '15 at 19:19
  • panel 8 is for displaying the product type and the panel 5 is for product the problem is in panel 5 ? can display product on panel 5 sorting by product type in panel 8 ? – Gimm Feb 07 '15 at 19:22
  • It would be easier I guess if your datatable was bound to a [DataGrid](https://msdn.microsoft.com/en-us/library/system.windows.forms.datagrid(v=vs.110).aspx) because the events are then fired in the context of the grid. By the look of it you try to do the heavy lifting yourself. That is not easy... – rene Feb 07 '15 at 19:27
  • Can we talk with e-mail ? – Gimm Feb 07 '15 at 19:29
  • 1
    No, we can't. You can provide all info here if you [edit] your question. This scales better as more people will read your question. – rene Feb 07 '15 at 21:31
  • Can you update your question with more detail about the problem you are dealing with? I'm not sure if you have difficulty writing a correct SQL query or C# code on Winform. – Dean Seo Feb 11 '15 at 20:39

1 Answers1

1

Your question seems quite vague, but let me try.

Are you asking how to allocate the button in the method Pos_Load in a sorted way?
(By doing so, the panel8 will contain buttons in the way you have sorted.)

If that's what you're asking, you can simply write your SQL query using order by as follows:

SELECT
        *
FROM
        product_group
ORDER BY
        product_name
DESC

With a query like that above, you can control your DataRow to be selected in order, which will cause your button to have the sorted DataRow's information.

Dean Seo
  • 5,486
  • 3
  • 30
  • 49
  • If, for some strange reason, you can't alter the query, you can use [Sorting rows in a data table](http://stackoverflow.com/a/9108171/3191303) to help you sort the results that you've already received, but YayCplusplus's answer should meet what sounds like your needs are. – AWinkle Feb 11 '15 at 20:59