Rows Cannot Be Programmatically Added to the Datagridview's Row Collection When the Control Is Data-Bound

Rows cannot be programmatically added to the datagridview's row collection when the control is data-bound

It appears as though you are using the DataSource property of the DataGridView. When this property is used to bind to data you cannot explicitly add rows directly to the DataGridView. You must instead add rows directy to your data source.

For example if your data source is a DataTable, using the DataTable that is assigned to the DataSource property (untested):

private void AddARow(DataTable table)
{
// Use the NewRow method to create a DataRow with
// the table's schema.
DataRow newRow = table.NewRow();

// Add the row to the rows collection.
table.Rows.Add(newRow);
}

Error Rows cannot be programatically added to datagridview's row collection when the control is data-bound

Add row in your Datasource and then set the datasource to updated datatable.
Like:

private void BtnAdd_Click(object sender, EventArgs e)
{
if (textBox1.Text == "" | textBox3.Text == "" | textBox4.Text == "" comboBox1.SelectedIndex == 0 | textBox5.Text == "" | textBox6.Text == "" | textBox7.Text == "" | textBox8.Text == "" | textBox9.Text == "" | textBox10.Text == "" | textBox11.Text == "" | textBox12.Text == "")
{
MessageBox.Show("Values Should not Be empty!");
textBox3.Focus();
}
else
{
DataTable dt = dataGridView1.DataSource as DataTable;
dt.Rows.Add(textBox1.Text, textBox2.Text, textBox3.Text, textBox4.Text, richTextBox1.Text, comboBox1.Text, textBox5.Text, comboBox2.Text, textBox6.Text, textBox7.Text, textBox8.Text, textBox9.Text, textBox10.Text, textBox11.Text, textBox12.Text);
dataGridView1.DataSource = dt;
}
}

The Rows cannot be programmatically added to the DataGridView's rows collection when the control is data-bound

If you bind data to the grid then you need to add a new row to the DataTable.

Here is short example:

Dim newRow As DataRow

newRow = dataSet.Tables(0).NewRow
newRow.Item(0) = txtValue1.Text
newRow.Item(1) = txtValue2.Text

dataSet.Tables(0).Rows.Add(newRow)

Rows cannot be programmatically added to the datagridview rows collection when the control is data-bound

Instead of inserting a DataGridViewRow try to insert a DataRow to your DataSource like this

var dataSource = dataGridView1.DataSource as BindingSource;
var dataTable = dataSource.DataSource as DataTable;

dataTable.Rows.Add(value1, value2,...)

this will update your BindingSource and show the new values of the DataTable.

Rows cannot be programmatically added to the DataGridView's rows collection when the control is data-bound error on collection data source

List<String> list = new List<String>();
list.add("val1");
dataGridView1.DataSource = list;
list.add("val2");
dataGridView1.DataSource = null;
dataGridView1.DataSource = list;

In this case you have to set datasource to null, and then again to list;
Or better to use Binding list

BindingList <String> list = new BindingList<String>();
list.Add("val1");
dataGridView1.DataSource = list;
list.Add("val1");

In this case you dont have to "refresh" anything, its done automatically



Related Topics



Leave a reply



Submit