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

后端 未结 2 694
伪装坚强ぢ
伪装坚强ぢ 2020-12-22 08:26

I have a DataGridView and few TextBox and combo box controls. when i enter data in textbox controls and click on add button the values are added to

相关标签:
2条回答
  • 2020-12-22 09:23

    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;
        }
    }
    
    0 讨论(0)
  • 2020-12-22 09:23

    When the DataGridView is bound to a data using the DataSource property, you cannot add new rows to the DataGridView directly. This is an expected error and documented in MSDN.

    The DataGridView control itself allows to you to add new rows if AllowUserToAddRows the property is set to true.

    If you still want to use the text boxes to get input and add to the DataGridView you have to manipulate the underlying DataTable which you have set as DataSource.

    Untested sample code

    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;
            if(dt != null)
            {
                DataRow row = table.NewRow();
                // set the field values as required                
                dt.Rows.Add(row);
                dataGridView1.DataSource = dt;
            }
            else
            {
                dataGridView1.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);
            }            
        }
    }
    

    If you want to synchronize the DataGridView and the DataBase use the BindingSource. Check the answer of this question for more details.

    0 讨论(0)
提交回复
热议问题