Click here to Skip to main content
15,868,141 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hello
i have a form with 50 buttons
I need to create query where i will populate that buttons
at first i will have maybe 10 buttons with some value but further on i will have more
so my question is:
how to check how many ID in table i have and populate that much buttons and to hide rest of them

What I have tried:

I am new in this so i don't have idea how to try it
Posted
Updated 3-Aug-17 4:20am
v2
Comments
Richard MacCutchan 3-Aug-17 4:24am    
50 buttons? Seriously, do you expect your users to try to figure out what each one, or combination of more than one, will do? I suggest you take a good look at your design.
Member 13084733 3-Aug-17 4:29am    
50 buttons is example i will have less and every button has his function so it will not be hard to use
Prifti Constantine 3-Aug-17 4:58am    
What would you like to put inside the button??? What kind of Value?
Member 13084733 3-Aug-17 5:31am    
decimal value and ofc text and image
Michael_Davies 3-Aug-17 5:01am    
What have you tried, it is impossible to help you as we do not even know your table schema nor how you determine which rows to hide.

You only tag your question as C#, presume you are using SQL as well. To determine the number of records in a table you could just SELECT COUNT(*) FROM <tablename>;

1 solution

Hi,

To solve your problem, I suggest to use the dataGridView component of the windows form.
You will dynamically add the button while you populate the component.
You can add or not, depends on the conditional
C#
private void FillData()
        {
            DataTable dt = new DataTable();
            //create column
            dt.Columns.Add("name", typeof(string));
            dt.Columns.Add("number", typeof(string));
            dt.Columns.Add("value", typeof(string));

            //fill data in datatable
            DataRow dr = dt.NewRow();
            dr["name"] = "Peter";
            dr["number"] = "1";
            dr["value"] = "0"; //not show button
            dt.Rows.Add(dr);

            dr = dt.NewRow();
            dr["name"] = "Mary";
            dr["number"] = "2";
            dr["value"] = "1"; //show button
            dt.Rows.Add(dr);

            dr = dt.NewRow();
            dr["name"] = "John";
            dr["number"] = "3";
            dr["value"] = "0"; //not show button
            dt.Rows.Add(dr);

            dr = dt.NewRow();
            dr["name"] = "Sarah";
            dr["number"] = "4";
            dr["value"] = "1"; // show button
            dt.Rows.Add(dr);

            //create column DataGridView
            dataGridView1.Columns.Add("name", "Name");
            dataGridView1.Columns.Add("number", "Number");
            dataGridView1.Columns.Add("button", "View");

            //create array to recive data
            string[] lst_dados = new string[2];

            foreach(DataRow dr1 in dt.Rows)
            {
                lst_dados[0] = dr1["name"].ToString();
                lst_dados[1] = dr1["number"].ToString();
                this.dataGridView1.Rows.Add(lst_dados);

                //Checks if button should be added in component
                if (dr1["value"].ToString() == "1")
                { 
                    //add button
                    DataGridViewButtonCell btn = new DataGridViewButtonCell();
                    btn.Value = "View";
                    btn.UseColumnTextForButtonValue = true;
                    this.dataGridView1.Rows[this.dataGridView1.Rows.Count - 2].Cells["button"] = btn;
                }
            }
        }
.

To fire the button, use CellClick events.
C#
private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
{
   if (e.ColumnIndex == this.dataGridView1.Columns["button"].Index)
   {
     //Do something with your button.
   }
}
 
Share this answer
 
Comments
Member 13084733 3-Aug-17 19:58pm    
This is great but i can not use this for POS especially if my design is based on just buttons for touchscreens

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900