Chriz L

Chriz L

  • NA
  • 220
  • 50.8k

Create checkboxes dynamically

Dec 12 2016 7:35 AM
Hello,
 
I want to create dynamically checkboxes based on sproc result (one checkbox for each row).Here's my code:
 
  1. public void selectChecks(Label appno, Label Cat)    
  2. {    
  3.     SqlConnection connection = getConnection();    
  4.     try    
  5.     {    
  6.         connection.Open();    
  7.         SqlCommand command = new SqlCommand("ChecksSelect", connection);    
  8.         command.CommandType = CommandType.StoredProcedure;    
  9.     
  10.         command.Parameters.Add("@Appno", SqlDbType.Int).Value = Convert.ToInt32(appno.Text);    
  11.         command.Parameters.Add("@Cat", SqlDbType.Int).Value = Convert.ToInt32(Cat.Text);    
  12.     
  13.         SqlDataAdapter adapter = new SqlDataAdapter(command);    
  14.         DataSet ds = new DataSet();    
  15.         adapter.Fill(ds);    
  16.     
  17.         foreach (DataTable table in ds.Tables)    
  18.         {    
  19.             foreach (DataRow dr in table.Rows)    
  20.             {    
  21.                 CheckBox check = new CheckBox();    
  22.                 check.Text = dr["CheckDescr"].ToString();    
  23.                 if (dr["CheckResult"].ToString() == "1")    
  24.                     check.Checked = true;    
  25.                 else    
  26.                     check.Checked = false;   
  27.               check.ID = dr["CheckCode"].ToString() + "Lbl";  
  28.             }    
  29.         }    
  30.     }    
  31.     catch (Exception ex)    
  32.     { myMessageBox(ex.Message); }    
  33. }   
 
How can I place them in my form's table?
 
Thank you in advance.

Answers (6)