I have 100 checkbox in a winfrom. Their name is sequential like checkbox1,checkbox2 etc. I have a submit button in my winform. After clicking the submitting button, it check
There is LINQ method OfType
. Why not use it to get rid of manual type testing and casting?
foreach (var ctrl in panel.Controls.OfType<CheckBox>().Where(x => x.IsChecked)
{
// ....
}
foreach (Control childc in Page.Controls)
{
if (childc is CheckBox)
{
CheckBox chk = (CheckBox)childc;
//do your operation
}
}
foreach (var box in this.Controls.OfType<CheckBox>())
{
if (box.Checked)
{
//...
}
else
{
//...
}
}
foreach (var ctrl in panel.Controls) {
if (ctrl is CheckBox && ((CheckBox)ctrl).IsChecked) {
//Do Something
}
}
foreach (var control in this.Controls) // I guess this is your form
{
if (control is CheckBox)
{
if (((CheckBox)control).Checked)
{
//update
}
else
{
//update another
}
}
}
This is the write answer for this................
c#
string movie="";
if (checkBox1.Checked == true)
{
movie=movie+checkBox1.Text + ",";
}
if (checkBox2.Checked == true)
{
movie=movie+checkBox2.Text + ",";
}
if (checkBox3.Checked == true)
{
movie=movie+checkBox3.Text + ",";
}
if (checkBox4.Checked == true)
{
movie = movie + checkBox4.Text + ",";
}
if (checkBox5.Checked == true)
{
movie = movie + checkBox5.Text + ",";
}
if (checkBox6.Checked == true)
{
movie = movie + checkBox6.Text + ",";
}
row["EnquiryFor"] = movie.ToString();
where row is a object of DataRow and EnquiryFor is the name of sql table column....