生成动态按钮 [英] Generating Dynamic Buttons
问题描述
实际上我从数据库生成2个按钮(汉堡,冰淇淋)如果我点击汉堡按钮只需要汉堡项目如鸡肉汉堡,蔬菜汉堡,如果我们点击冰淇淋按钮只有冰淇淋像香草草莓应该有来吧,但在我的程序中,如果我点击冰淇淋只有冰淇淋项目好了,如果我再次按汉堡除了冰淇淋汉堡项目也来了,请帮助我
actually i generated 2 buttons(Burger,Ice Cream) from database if i Click the Burger button only i need Burger Items like Chicken Burger,Veg Burger,if we click the Ice cream button only Ice creams like vanilla strawberry should have to come, but in my program if i click ice cream only ice cream items coming fine, if i again i pressed burger in addition to ice cream burger items also coming, please help me
private void btnRetreive_Click(object sender, EventArgs e)
{
string str = sender.ToString();
string Category = str.Replace("System.Windows.Forms.Button, Text: ", "");
strCon = GetConnectionString();
OleDbConnection objCon = new OleDbConnection(strCon);
objCon.Open();
string query = "Select count(*) from tblProduct where Category = '" + Category + "'";
OleDbCommand com = new OleDbCommand(query, objCon);
int count = Convert.ToInt32(com.ExecuteScalar());
for (int i = 0; i < count; i++)
{
string Qry = "Select SubCategory from tblProduct where Category = '"+ Category +"' ";
OleDbDataAdapter adpter = new OleDbDataAdapter(Qry, objCon);
DataTable dt = new DataTable();
adpter.Fill(dt);
Button b = new Button();
b.Name = dt.Rows[i]["SubCategory"].ToString();
b.Text = dt.Rows[i]["SubCategory"].ToString();
b.ForeColor = Color.DarkBlue;
b.BackColor = Color.Honeydew;
b.Width = 150;
b.Click += new EventHandler(btnRetreive1_Click);
b.Location = new Point((400+(i*10)), (i * 40));
this.tableLayoutPanel1.Controls.Add(b);
}
}
private void btnRetreive1_Click(object sender, EventArgs e)
{
string strGetProductName;
strGetProductName = sender.ToString();
strGetProductName = strGetProductName.Replace("System.Windows.Forms.Button, Text: ", "");
strCon = GetConnectionString();
OleDbConnection objCon = new OleDbConnection(strCon);
string Query = "Select Category,SubCategory,Rate,Qty from tblProduct where SubCategory ='" + strGetProductName + "' ";
objCon.Open();
OleDbDataAdapter adpter = new OleDbDataAdapter(Query, objCon);
DataTable dt = new DataTable();
dt.Clear();
adpter.Fill(dt);
objCon.Close();
dataGridView1.DataSource = dt;
}
推荐答案
好吧,我并不感到惊讶。您不会从TableLayoutPanel befoer中删除任何添加新按钮的按钮 - 您只需添加新项目。因此,如果他们选择一个类别并添加了一些按钮,那么就永远不要离开......
另外,我建议左右?看看你的btnRetreive1_Click方法:那太可怕了!
试试这个:
Well, I'm not surprised. You don't remove any buttons from your TableLayoutPanel befoer you add new ones - you just add the new items. so if they select a category and you add teh buttons, then never go away...
Plus, cal I make a suggestion or so? Look at your btnRetreive1_Click method: that's pretty horrible!
Try this:
private void btnRetreive1_Click(object sender, EventArgs e)
{
Button b = sender as Button;
if (b != null)
{
string strGetProductName = b.Text;
strCon = GetConnectionString();
OleDbConnection objCon = new OleDbConnection(strCon);
string Query = "Select Category,SubCategory,Rate,Qty from tblProduct where SubCategory ='" + strGetProductName + "' ";
objCon.Open();
OleDbDataAdapter adpter = new OleDbDataAdapter(Query, objCon);
DataTable dt = new DataTable();
adpter.Fill(dt);
objCon.Close();
dataGridView1.DataSource = dt;
}
}
请不要这样做数据库访问!不要连接字符串以构建SQL命令。它让您对意外或故意的SQL注入攻击持开放态度,这可能会破坏您的整个数据库。请改用参数化查询。
And please, don't do DB accesses like that! Do not concatenate strings to build a SQL command. It leaves you wide open to accidental or deliberate SQL Injection attack which can destroy your entire database. Use Parametrized queries instead.
这篇关于生成动态按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!