Click here to Skip to main content
15,917,456 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Guys, I created an app for my friend super market, everythings is work currectlly, but, I want to create a buttons automatically at form depend the rows in SQL table and put the name of that Item. Could anyone can help me.

What I have tried:

I have not tried to do anything because I do not know how to do it. I search on the net, but I have see it yet. Could you help me, please.
Posted
Updated 12-Mar-20 21:14pm
Comments
ZurdoDev 12-Mar-20 10:49am    
You need to be more clear about what you want.

You could use a DataGrid or other data container.
Or you can just write code to display a button or create a button. Lots of ways of doing it.
Karam Ibrahim 12-Mar-20 11:22am    
Dear ZurdoDev, my questions is I have many rows in Sql table such as (name, price).
I want to create form that show the buttons depend of rows in table. For example, if I have 20 rows each row contain (Name, Price) and when i run the form it show me many buttons with each name on it and when i click on one buttons show the price of it.
ZurdoDev 12-Mar-20 11:28am    
A repeater or other data container will be your best bet.
OriginalGriff 12-Mar-20 11:05am    
This is not a good question - we cannot work out from that little what you are trying to do.
Remember that we can't see your screen, access your HDD, or read your mind - we only get exactly what you type to work with.
Use the "Improve question" widget to edit your question and provide better information.

In addition to @phil.o solution, please read this: How to programmatically add controls to Windows forms at run time by using Visual C#[^]

And implementation:
C#
using(var connection = new SqlConnection("conn_string_here"))
using(var command = new SqlCommand("command_text_here", conn))
{
    conn.Open();
    using(var reader = command.ExecuteReader())
        while (reader.Read())
        {
            //add new button here!
            //do not forget to set Location!!! Location = new Point(x,y);
        }
    conn.Close();
}
 
Share this answer
 
Comments
Karam Ibrahim 16-Mar-20 12:02pm    
Dear Maciej, I will do it that you wrote here down. thanks
Maciej Los 16-Mar-20 12:42pm    
Great!
Good luck!
Create and execute the SQL query fetching the rows.
Enumerate the results, and for each row, create a new button, add it to the controls collection of the form/container, and set its location so that successive buttons don't overlap.
 
Share this answer
 
Comments
Maciej Los 13-Mar-20 3:05am    
5ed!

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