Click here to Skip to main content
15,891,248 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi Friends..,

I use my code to store all the values in the datagridview to my server but here am geting my error as
"ColumnCount property cannot be set on a data-bound DataGridView control." in my exception pls give me an idea hw will i use dis code..

C#
try
           {


               foreach (DataGridViewRow row in dataGridView1.Rows)
               {

                   foreach (DataGridViewCell cell in row.Cells)
                   {
                       value1 = row.Cells[0].Value.ToString();
                       value2 = row.Cells[1].Value.ToString();
                       value3 = row.Cells[2].Value.ToString();
                       value4 = row.Cells[3].Value.ToString();
                       value5 = row.Cells[4].Value.ToString();
                        dataGridView1.ColumnCount = 5;
                               dataGridView1.Columns[0].Name = "Age";
                               dataGridView1.Columns[1].Name = "workclass";
                               dataGridView1.Columns[2].Name = "fnlwgt";
                               dataGridView1.Columns[3].Name = "education";
                               dataGridView1.Columns[4].Name = "maritalstatus";
                               string[] row1 = new string[] { value1.ToString(), value2.ToString(), value3.ToString(), value4.ToString(), value5.ToString() };
                               dataGridView1.Rows.Add(row1);
                               //...
                               cmd = new SqlCommand("insert into rule1 values ('" + value1.ToString() + "','" + value2.ToString() + "','" + value3.ToString() + "','" + value4.ToString() + "','" + value5.ToString() + "')", cn.con);
                               cmd.ExecuteNonQuery();
                               cmd.Dispose();
                               MessageBox.Show("Execute Process!!");
                           }

                   }
               }
           catch (Exception error)
           {

           }


Thank You...
Posted
Comments
Sushil Mate 12-Jul-13 1:15am    
remove this line dataGridView1.ColumnCount = 5;
usha C 12-Jul-13 1:19am    
Ya i checked by removing tat also
Sushil Mate 12-Jul-13 1:19am    
then whats the problem?
usha C 12-Jul-13 1:22am    
not geting result i got the exception error as "ColumnCount property cannot be set on a data-bound DataGridView control"
Sushil Mate 12-Jul-13 1:28am    
Can you debug the code? I still don't understand after removing the ColumnCount property you still facing the same issue.

1 solution

Try the below coding(edit this as per your project)


protected void Button1_Click1(object sender, EventArgs e)
   {
       string a = Request.QueryString["var"];
       // TextBox1.Text = "";
       foreach (GridViewRow gvr in GridView1.Rows)
       {
           if (gvr.RowType == DataControlRowType.DataRow)
           {
               CheckBox cb = (CheckBox)(gvr.FindControl("CheckBox1"));
               if (cb.Checked == true)
               {   SqlConnection cn = new SqlConnection("Data Source=FABSYSXX-PC; Initial Catalog=rule; User Id=sa; Pwd=saadmin");
           cn.Open();
           SqlCommand cmd = new SqlCommand("Insert into Trans values('" + Session["count"].ToString() + "','" + gvr.Cells[1].Text + "','" + gvr.Cells[2].Text + "','" + gvr.Cells[3].Text + "','" + a + "')", cn); //Write Your Query to insert into database table

                   int iCount = cmd.ExecuteNonQuery();

                   cn.Close();
                   //TextBox1.Text = TextBox1.Text + "," + gvr.Cells[1].Text;
                  // TextBox2.Text = TextBox2.Text + "," + gvr.Cells[2].Text;
               }

           }

       }
       Response.Write("<SCRIPT>alert('Transcation completed Successfully!!!! ')</SCRIPT>");

       //T
 
Share this answer
 
Comments
usha C 12-Jul-13 1:39am    
Thnk u...
Deenuji 12-Jul-13 1:48am    
got ur solution???
usha C 12-Jul-13 1:58am    
s sir i got it...

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