简体   繁体   English

从datagridview数据插入数据库,该数据从C#MSSQL中的另一个表检索

[英]Insert to database from datagridview data which is retrieved from another table in C# MSSQL

I have two tables 'Item' and 'Invoice'. 我有两个表“项目”和“发票”。 And I have a 'datagridview' in my form with headers Now application will fill 'itemcode|item desc|item rate' from 'item' table when user enter 'item desc' cell value ('using auto complete property for item desc cell and key down event for fill according to the user entry'). 我的表单中带有标题的“ datagridview”现在,当用户输入“ item desc”单元格值时(“为项目desc单元格使用自动完成属性,按下事件以根据用户输入进行填充”)。 Now user will enter 'Quantity' and 'Item Total'(by manually for now). 现在,用户将输入“数量”和“项目总计”(暂时手动输入)。 And press submit button. 然后按提交按钮。 Here the problem comes. 问题来了。 Its showing me error input string is not correct. 它向我显示错误输入字符串不正确。 But when I enter all cell manually(I mean without using keydown event to fill data) the data is inserting to invoice table. 但是,当我手动输入所有单元格时(我的意思是不使用keydown事件填充数据),数据将插入到发票表中。

This is my submit button to insert in invoice table < 这是我的提交按钮,可插入发票表<

private void btn_submit_Click(object sender, EventArgs e)
        {
            try
            {
                foreach (DataGridViewRow row in dtgrdvw_items.Rows)
                {
                    using (SqlCommand cmd = new SqlCommand("insert into invoice values (@inv_no,@inv_date,@inv_cust_name,@inv_remarks,@inv_type,@inv_item_code,@inv_item_desc,@inv_item_rate," +
                        "@inv_item_qty,@inv_item_total)", con))
                    {
                        cmd.Parameters.AddWithValue("@inv_no", Convert.ToInt32(txt_inv_no.Text));
                        cmd.Parameters.AddWithValue("@inv_date", txt_date.Text);
                        cmd.Parameters.AddWithValue("@inv_cust_name", txt_cust_name.Text);
                        cmd.Parameters.AddWithValue("@inv_remarks", txt_remarks.Text);
                        cmd.Parameters.AddWithValue("@inv_type", cmbx_inv_type.SelectedItem);
                        cmd.Parameters.AddWithValue("@inv_item_code",Convert.ToInt32(row.Cells[0].Value));
                        cmd.Parameters.AddWithValue("@inv_item_desc",Convert.ToString(row.Cells[1].Value));
                        cmd.Parameters.AddWithValue("@inv_item_rate",Convert.ToDecimal(row.Cells[2].Value));
                        cmd.Parameters.AddWithValue("@inv_item_qty",Convert.ToInt32(row.Cells[3].Value));
                        decimal total = Convert.ToDecimal(row.Cells[2].Value) * Convert.ToDecimal(row.Cells[3].Value);
                        cmd.Parameters.AddWithValue("@inv_item_total",total);
                        if (con.State == ConnectionState.Closed)
                        {
                            con.Open();
                        }
                        cmd.ExecuteNonQuery();
                        con.Close();

                    }

                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            MessageBox.Show("invoice submitted");
        }

> >

this is my keydown event 这是我的按键事件

< <

 private void dtgrdvw_items_KeyDown(object sender, KeyEventArgs e)
          {
            if (e.KeyCode == Keys.Tab)
            {

                int rowindex = dtgrdvw_items.CurrentCell.RowIndex;
                int columnindex = dtgrdvw_items.CurrentCell.ColumnIndex;
                if (columnindex == 1)
                {
                    SqlCommand cmd = new SqlCommand("Select * from item WHERE item_desc=@inv_item_desc", con);
                    cmd.Parameters.Add("@inv_item_desc", SqlDbType.VarChar).Value = dtgrdvw_items.Rows[rowindex].Cells[columnindex].Value.ToString();
                    if (con.State == ConnectionState.Closed)
                    {
                        con.Open();
                    }
                    SqlDataReader dr = cmd.ExecuteReader();
                    while (dr.Read())
                    {
                      dtgrdvw_items.Rows[rowindex].Cells[0].Value = dr[0].ToString();
                       dtgrdvw_items.Rows[rowindex].Cells[1].Value = dr[1].ToString();
                       dtgrdvw_items.Rows[rowindex].Cells[3].Value = dr[2].ToString();
                    }
                      dr.Close();
                }
           }
       } 

> >

This is my gridview 这是我的gridview

Please help. 请帮忙。

If I understand correctly, the Rate column accepts an integer type. 如果我理解正确,则“费率”列将接受整数类型。 You may want to use Convert.ToInt32 on 您可能要在上使用Convert.ToInt32

dtgrdvw_items.Rows[rowindex].Cells[3].Value = dr[2].ToString();

so that it will cast the string to an integer before it will be assigned to Cells[3] which is the Rate column. 这样,在将字符串分配给Cells [3](即“费率”列)之前,它将字符串转换为整数。

dtgrdvw_items.Rows[rowindex].Cells[3].Value = Convert.ToInt32(dr[2].ToString());

Cells in grid 0 code 1 description 2 quantity 3 rate 4 total 网格中的像元0代码1描述2数量3比率4总计

cmd.Parameters.AddWithValue("@inv_item_code",Convert.ToInt32(row.Cells[0].Value));
cmd.Parameters.AddWithValue("@inv_item_desc",Convert.ToString(row.Cells[1].Value));
cmd.Parameters.AddWithValue("@inv_item_rate",Convert.ToDecimal(row.Cells[2].Value)); //Change

Should be: 应该:

cmd.Parameters.AddWithValue("@inv_item_rate",Convert.ToDecimal(row.Cells[3].Value));

cmd.Parameters.AddWithValue("@inv_item_qty",Convert.ToInt32(row.Cells[3].Value)); //Change

Should be: 应该:

cmd.Parameters.AddWithValue("@inv_item_qty",Convert.ToInt32(row.Cells[2].Value)); //Change

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM