Click here to Skip to main content
15,887,349 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I have a form 'sales'. I have added columns manually to dgv. It works fine.
But when I tried to bind it with my new class ' receipt ' it showed my the error that Rows cannot be programaticaly added to dgv when data control is bound.

I am facing this issue from two days. Anyone who have worked in this scenario plz do help me. Any guidens? <b

What I have tried:

I couldn't properly try out things. I have searched alot but not succeed...

Also I tried this
Datagridview1.datasource=ReceiptbindingSource

But same error rows cannot be added programaticaly to datagridview when data control is bound
Posted
Updated 2-Mar-18 6:12am

1 solution

What has this got to do with the title of your post?
The problem is clear - you can't do both manually add rows and have the control bound.

You can, however, add rows to the dataset ... see the solution on this link
Add Rows To Databound DatagridView[^]
 
Share this answer
 
Comments
Member 11302407 3-Mar-18 11:58am    
this is my code where i am using .

if (dataGridView1.Rows.Count > 0)
{

DataTable dt = dataGridView1.DataSource as DataTable;
//Check if the product Id exists with the same Price
foreach (DataGridViewRow row in dataGridView1.Rows)
{
dataGridView1.AllowUserToAddRows = true ;
if (Convert.ToString(row.Cells[1].Value) == tb_ino.Text )
{
//Update the Quantity of the found row
MessageBox.Show("matched!");
//row.cells[3] is value for qty
//multiply this to row.cells[4]
row.Cells[3].Value = Convert.ToString(Convert.ToInt16(tb_qty.Text) + Convert.ToInt16(row.Cells[3].Value));
row.Cells[5].Value=(Convert.ToInt16(row.Cells[3].Value) * Convert.ToInt16(row.Cells[4].Value)).ToString();
dataGridView1.DataSource = dt;
found = true;
}

}
if (!found)
{
dataGridView1.AllowUserToAddRows = true;


//Add the row to grid view
dt.Rows.Add(dataGridView1.RowCount, r[0], r[1], tb_qty.Text.Trim(), r[4], price);
dt.AcceptChanges();
dataGridView1.DataSource = dt;

}

}
Member 11302407 3-Mar-18 12:05pm    
Also now by this code I am getting exception object reference not set to an instance.
CHill60 4-Mar-18 12:33pm    
On which line?

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