Click here to Skip to main content
15,885,757 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I'm trying to add multiple controls at the runtime in datagridview, Like as follows
If one datatable's count will return 2 then there will be two row in the datagridview, and in each row there will be one combo, one textbox and one checkbox. I've tried this following code, but it works for only one row. Getting error from the 2nd onwards.

if (dtRec.Rows.Count > 0)
                {
                    DataTable dt = new DataTable();
                    dt.Columns.Add("Combo");
                    dt.Columns.Add("Textbox");
                    dt.Columns.Add("Checkbox");
                    dt.Rows.Add("");
                    
                    this.gvConstituency.DataSource = dt;
                    this.gvConstituency.Columns[0].Width = 200;
                    this.gvConstituency.Columns[1].Width = 200;
                    this.gvConstituency.Columns[2].Width = 200;
                    for (int i = 0; i < dtRec.Rows.Count - 1; i++)
			{
				DataGridViewComboBoxCell ComboBoxCell = new DataGridViewComboBoxCell();
                            ComboBoxCell.Items.AddRange(new string[] { "0-10%", "11-20%", "21-30%", "31-40%", "41-50%", "51-60%", "61-70%", "71-80%", "81-90%", "91-100%" });
                            this.gvConstituency[i, 0] = ComboBoxCell;
                            this.gvConstituency[i, 0].Value = "0-10%";

                            DataGridViewTextBoxCell TextBoxCell = new DataGridViewTextBoxCell();
                            this.gvConstituency[i+1, 0] = TextBoxCell;
                            this.gvConstituency[i+1, 0].Value = "Write some thing";

                            DataGridViewCheckBoxCell CheckBoxCell = new DataGridViewCheckBoxCell();
                            CheckBoxCell.Style.Alignment = DataGridViewContentAlignment.MiddleCenter;
                            this.gvConstituency[i + 2, 0] = CheckBoxCell;
                            this.gvConstituency[i + 2, 0].Value = true;

                            dt.Rows.Add("");
                            this.gvConstituency.DataSource = dt;
			}
		}


Here in
dtRec
some records are stored, as per the record count i want to create multiple rows with the controls in datagridview.
Hope i can describe the question, if any confusion please ask.

Please help me to solve this.
Thanks in advance.
Posted

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