问题描述
我有一个 DataGridView
和少量 TextBox
和组合框控件。当我在文本框控件中输入数据并单击添加按钮时,这些值将添加到用户视图的 DataGridView
视图中,然后单击保存按钮,值将保存在DB中。
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 DataGridView
view for user view and on clicking save button the values gets saved in DB.
问题是我有一个 TextBox
(Invoice_No),在这个 TextBox
leave事件我已经编写代码来从DB获取数据到数据网格视图。现在,我不能通过在 TextBox
控件中输入值来向此网格添加额外的行。它给出以下错误
The problem is that I have a TextBox
(Invoice_No), On this TextBox
leave event i have written code to fetch data from DB to data grid view. Now i am not able to add additional row to this grid by entering values in TextBox
controls. It gives the below error
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);
}
}
推荐答案
添加然后将数据源设置为更新的datatable。
喜欢:
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;
}
}
这篇关于错误“当控件是数据绑定的时候,行不能被编程地添加到datagridview的行集合”的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!