Winforms 动态按钮c#
我对c#中的动态按钮有一些问题。我已经为Pos开发了一个应用程序,我需要创建产品组,但当我点击组按钮时,我必须按产品组的名称显示产品排序 这是我的代码:Winforms 动态按钮c#,winforms,c#-4.0,Winforms,C# 4.0,我对c#中的动态按钮有一些问题。我已经为Pos开发了一个应用程序,我需要创建产品组,但当我点击组按钮时,我必须按产品组的名称显示产品排序 这是我的代码: private void Pos_Load(object sender, EventArgs e) { MySqlConnection conn = new MySqlConnection("server=localhost;user id=root;database=programmj;allowuservariables=True"
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);
}
}
这是我展示产品的另一个代码
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;
}
}
你的问题似乎很模糊,但让我试试 您是否询问如何以排序方式分配方法
Pos\u Load
中的按钮
(这样,面板8
将以您排序的方式包含按钮。)
如果这就是您要问的,您可以使用orderby
编写SQL查询,如下所示:
SELECT
*
FROM
product_group
ORDER BY
product_name
DESC
使用上述查询,您可以控制您的
DataRow
按顺序选择,这将导致您的按钮
对数据行的
信息进行排序。您说您有问题,但没有解释问题所在……我的问题是无法按产品名称显示产品排序?我有桌上产品,桌上产品类型我需要按产品类型显示产品?你明白我的意思吗?好的…你的代码的哪一部分在尝试这样做,因为我在任何地方都看不到排序代码,我不得不假设,只是在不同的面板上添加了按钮…面板8用于显示产品类型,面板5用于显示产品问题在面板5中?是否可以在面板5上显示产品?按面板8中的产品类型排序?不,我们不能。如果您有问题,可以在此处提供所有信息。随着越来越多的人阅读你的问题,这种方法的效果会更好。如果出于某种奇怪的原因,你无法更改查询,你可以使用它来帮助你对已经收到的结果进行排序,但是YayCplusplus的答案应该符合你的需要。