Click here to Skip to main content
15,897,334 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
i want to add more than one check boxes dynamically in c#.net
i have three tables
1. tab which contain tabs
2. groups which contain groups with in specified tab
3. items which hold controls like buttons etc within specified groups

the code is

public void CreateCheckBoxes()
{
int TabID, GroupID, ItemID, ControlID;
String TabName, TabCaption, GroupName, GroupCaption, ItemName, ItemCaption, ControlName, ControlCaption;
//System.Windows.Forms.CheckBox check=null;
//System.Windows.Forms.CheckBox check1 = null;
//System.Windows.Forms.CheckBox check2 = null;
OleDbConnection conn = new OleDbConnection(gb.autocreateconmsacess());
conn.Open();
string str = @"Select * from Tabs";
OleDbCommand cmd = new OleDbCommand(str, conn);
OleDbDataReader DataReaderr = cmd.ExecuteReader();
Console.WriteLine(DataReaderr.FieldCount);

while (DataReaderr.Read())
{
CheckBox checkj = new CheckBox();
TabID = Convert.ToInt32(DataReaderr[0]);
TabName = DataReaderr[1].ToString();
TabCaption = DataReaderr[2].ToString();
// CheckBox checkj = new CheckBox();
checkj.Text = TabCaption;

this.groupBox1.Controls.Add(checkj);


// TabHome.Id = TabID;
OleDbDataReader ReadGroup;
string str1 = "select * from Groups where active=yes and Tab_ID=" + TabID;

OleDbDataAdapter da = new OleDbDataAdapter(str1, conn);
DataSet dsGroups = new DataSet();
da.Fill(dsGroups, "Groups");

for (int i = 0; i < dsGroups.Tables[0].Rows.Count; i++)
{
CheckBox check1 = new CheckBox();
GroupID = Convert.ToInt32(dsGroups.Tables[0].Rows[i]["ID"]);
GroupName = Convert.ToString(dsGroups.Tables[0].Rows[i]["Name"]);
GroupCaption = Convert.ToString(dsGroups.Tables[0].Rows[i]["Caption"]);
check1.Text=GroupCaption;
this.groupBox2.Controls.Add(check1);





string str2 = "select * from Items where active=yes and Group_id=" + GroupID;
// OleDbCommand CmdControls = new OleDbCommand(str2, conns);

OleDbDataAdapter daa = new OleDbDataAdapter(str2, conn);
DataSet dsItems = new DataSet();
daa.Fill(dsItems, "Items");
CheckBox check2 = new CheckBox();
for (int j = 0; j < dsItems.Tables[0].Rows.Count; j++)
{

ControlID = Convert.ToInt32(dsItems.Tables[0].Rows[j]["Item_id"]);
ControlName = dsItems.Tables[0].Rows[j]["Caption"].ToString();
ControlCaption = dsItems.Tables[0].Rows[j]["Name"].ToString();
check2.Text = ControlCaption;
this.groupBox3.Controls.Add(new CheckBox());
check2.Contains(new CheckBox());
// check2.Text = ControlCaption;
// check2.Tag = j;
// this.groupBox3.Controls.Add(check2);



}
}

the check boxes created but only 1-1 in three text boxes

means it take last tab
last group
last item

thanks for ur answer mr. christian grause
i think u tell me better now
Posted
Updated 19-Feb-10 20:35pm
v2

1 solution

OK, so you create them and add them to your form's control collection. I assume this is winforms, if it was ASP.NET or WPF, you'd have told us, right ?

This looks messy - you add checkboxes in three places, have you stepped through the code ? Tried removing code to add checkboxes to see which one is working ?
 
Share this answer
 
v2

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