Abhijith
Abhijith

Reputation: 73

Getting exception saying Rows cannot be added programatically

I have a datagridview which displays sale details based on bill number. I have another datagridview which lists products. I have added a cell double click event in Product datagridview and when i double click on an item it should appear in my sale datagridvew. But as i have added data from database to sale datagridview based on bill number i cant add new product to that datagridview. Iam getting exception saying Rows cannot be programatically added to the datagridviews rows collection when the control is data-bound.

here is my code in items datagridview cell double click

var index = dgv_POS.Rows.Add();
        string a = dgv_ItemNamePOS.CurrentRow.Cells[0].Value.ToString();
        dgv_POS.Rows[index].Cells[1].Value = a.ToString();



        string CS = "data source=.; database=BillingSoftware; user id=sa; password=9495640";
        using (SqlConnection con = new SqlConnection(CS))
        {
            con.Open();

            SqlCommand cmd = new SqlCommand("SELECT Item_No from Items where Name=@Name AND Activate='Yes'", con);
            cmd.Parameters.AddWithValue("@Name", a);
            string k = cmd.ExecuteScalar().ToString();
            dgv_POS.Rows[index].Cells[1].Value = k;


            SqlCommand cmd2 = new SqlCommand("SELECT Name from Items where Name=@Name AND Activate='Yes'", con);
            cmd2.Parameters.AddWithValue("@Name", a);
            string k1 = cmd2.ExecuteScalar().ToString();
            dgv_POS.Rows[index].Cells[2].Value = k1;

            SqlCommand cmd5 = new SqlCommand("SELECT Quantity_Type from Items where Name=@Name AND Activate='Yes'", con);
            cmd5.Parameters.AddWithValue("@Name", a);
            string k3 = cmd5.ExecuteScalar().ToString();
            dgv_POS.Rows[index].Cells[3].Value = k3.ToString();
        }

code to databound datagridview

SqlDataAdapter da = new SqlDataAdapter("SELECT Item_no as [Item No.],Item,Qty_Type as [Qty Type], Price, Quantity, Amount FROM POS WHERE Bill_No=@Billno", con);
            da.SelectCommand.Parameters.AddWithValue("@Billno", txt_BillNoPOSUpdate.Text);
            DataTable dt = new DataTable();
            da.Fill(dt);
            dgv_POSUpdate.DataSource = dt;

Upvotes: 0

Views: 41

Answers (1)

Jeremy Thompson
Jeremy Thompson

Reputation: 65534

The error is telling you what you need to do:

Rows cannot be programatically added to the datagridviews rows collection when the control is data-bound.

Since the DataGridView is Data-Bound you dont add a row to it, you add a Row/Item to the datasource.

List<Sale> sales = GetSalesInfo(id);
//or if its a DataTable
DataTable sales  = GetSalesInfo(id);
dgv_POS.DataSource = sales;  //<-- your DataGridView is bound to a data source

To add a new row to the grid you add a new sale item:

sales.Add(new Sale { Name= name, Price = 123 } );
//or if its a DataTable
sales.Rows.Add(new object[] { name,123 } );

Upvotes: 1

Related Questions