错误“当控件受数据绑定时,无法以编程方式将行添加到datagridview行集合”

时间:2015-02-05 18:34:00

标签: c# winforms datagridview

我有一个DataGridView和几个TextBox和组合框控件。当我在文本框控件中输入数据并单击添加按钮时,值将添加到用户视图的DataGridView视图中,单击保存按钮时,值将保存在数据库中。

问题是我有一个TextBox(Invoice_No),在这个TextBox离开事件中,我编写了代码来从DB获取数据到数据网格视图。现在,我无法通过在TextBox控件中输入值来向此网格添加其他行。它给出了以下错误

  

当控件是数据绑定时,无法以编程方式将行添加到datagridview的行集合

private void textBox1_Leave(object sender, EventArgs e)
{
    MySqlConnection connection = new MySqlConnection(myconnectionstring);

    string getinvdtlcnt = "SELECT COUNT(*) FROM invoice_detail WHERE invoice_no = '" + textBox1.Text + "'";
    MySqlCommand cmd = new MySqlCommand(getinvdtlcnt, connection);
    connection.Open();
    var ObjResult = cmd.ExecuteScalar();
    int Result = Convert.ToInt32(ObjResult);
    connection.Close();
    if (Result > 0)
    {                dataGridView1.Columns.Clear();
            string getinvdtl = "SELECT invoice_no Invoice_No,invoice_line_no Invoice_Line_No,barcode Barcode,product_name Product_Name,description Description,vendor_name Vendor_Name,unit_qty Unit_Qty,UOM,total_qty Total_Qty,single_qty_cost Single_Qty_Cost,single_qty_retail Single_Qty_Retail,cost Cost,retail Retail,discount Discount,amount Amount FROM invoice_detail WHERE invoice_no = '" + textBox1.Text + "'";
            connection.Open();
            MySqlDataAdapter adapter = new MySqlDataAdapter(getinvdtl, connection);

            MySqlCommandBuilder cmdbuilder = new MySqlCommandBuilder(adapter);
            DataTable dt = new DataTable();
            adapter.Fill(dt);
            dataGridView1.DataSource = dt;

            connection.Close();
            int r;
            bool isRvalid = int.TryParse(Result.ToString(),out r);
            textBox2.Text = (Result + 1).ToString();
            textBox3.Focus();
    }
}

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
    {                
        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);
    }
}

3 个答案:

答案 0 :(得分:3)

在数据源中添加行,然后将数据源设置为更新的数据表。 像:

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;
    }
}

答案 1 :(得分:1)

使用DataGridView属性将DataSource绑定到数据时,无法直接向DataGridView添加新行。这是预期的错误,记录在MSDN

DataGridView控件本身允许您在AllowUserToAddRows属性设置为true时添加新行。

如果您仍想使用文本框获取输入并添加到DataGridView,则必须操纵已设置为DataTable的基础DataSource

未经测试的示例代码

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);
        }            
    }
}

如果您要同步DataGridViewDataBase,请使用BindingSource。请查看此question的答案以获取更多详细信息。

答案 2 :(得分:0)

在我的DatagridView中已经有数据,然后我想在Windows窗体中单击“添加”按钮时添加新行,但提示“在控件数据绑定中,行无法以编程方式添加到datagridview行集合中。

然后我在google上找到了答案,例如可以使用DataTable添加新行, 下面我使用的代码

然后,我试图跑步

在我的数据网格中显示添加的行,但具有空白值,例如空行

请建议我错在哪里,我是使用C#在Windows应用程序datagridview上新接触的人

 DataTable dataTable=new DataTable();

 foreach (DataGridViewColumn col in dataGridView.Columns)
 {
     dataTable.Column.add(col.Name);
 }
 foreach (dataGridViewRow row in dataGridView.Rows)
 {
     DataRow drow=dataTable.NewRow();
     foreach (DataGridViewCell cell in row.cells)
     {
        drow[cell.ColumnIndes]=cell.value;
        dataTable.Rows.add(drow);
     }
 }
   //then,
   //i assigned dataTable values to dataGridView

   dataGridView.DataSource=dataTable;
 }