在datagridview中生成一个包含多行的数字 [英] Generating one number with multiple rows in datagridview

查看:77
本文介绍了在datagridview中生成一个包含多行的数字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试创建结算表单。我在sql-server中有两个表。文本框应该将日期插入表1,将datagridview插入表2.

I am trying to create a billing form. I have two tables in sql-server. The text boxes should insert the date to table 1 and datagridview to table 2.

我没有收到任何错误,但我有一个生成自动编号的文本框,并且每个数字在datagridview中应该有多行。

I am not getting any errors but I have a text box which generates auto number, and under each number there should be multiple rows in datagridview.

下面的代码只保存表格中的最后一行。代码是

The code below is saving only the last row in the table.here is the code

con = new SqlConnection("data source=localhost;initial catalog=testDb;integrated security=true"); con.Open(); cmd = new SqlCommand(@"select CashirReport.ReportId,CashirReport.ReportNumber,CashirReport.ReportDate , CashirReportDetails.SerialNumber, CashirReportDetails.RefDetails,CashirReportDetails.Amount from CashirReport inner join CashirReportDetails on CashirReport.ReportId=CashirReportDetails.ReportDetailsNumber", con); da = new SqlDataAdapter(); da.SelectCommand = cmd; dt = new DataTable(); da.Fill(dt); } private void btnsave_Click(object sender, EventArgs e) { for (int i = 0; i < dgvDetails.Rows.Count - 1; i++) { cmd = new SqlCommand(@"insert into CashirReport(ReportDate,Total)values(@ReportDate,@Total); insert into CashirReportDetails(SerialNumber,RefDetails,Amount)values(@SerialNumber,@RefDetails,@Amount)",con); cmd.Parameters.AddWithValue("@ReportDate", SqlDbType.Date).Value = dtDate.Value.Date; cmd.Parameters.AddWithValue("@Total", SqlDbType.Money).Value = txtAmount.Text; cmd.Parameters.AddWithValue("@SerialNumber", dgvDetails.Rows[i].Cells[0].Value); cmd.Parameters.AddWithValue("@RefDetails", dgvDetails.Rows[i].Cells[1].Value); cmd.Parameters.AddWithValue("@Amount", dgvDetails.Rows[i].Cells[2].Value); } cmd.ExecuteNonQuery();







推荐答案

你好,

我建议一次只执行一个插入,检查错误,如果没有则移动到下一个插入。例如,在

以下帖子
几乎同样的问题被问到但是没有在for / next但是没有什么改变,这将适用于for / next。

I would recommend only executing one insert at a time, check for errors, if none then move to the next insert. For example in the following post pretty much the same question is asked but not in a for/next yet with little change this will work with a for/next.

通过添加一个事务可以更好,所以如果有任何插入失败一切都回滚了,例如如果您希望在添加15个记录后添加20个记录而其中一个记录失败则会撤消15个记录。另外,在ExecuteNonQuery上你可以分配一个变量,检查
结果,它确实有效,它是否失败而没有错误?

It can be better by adding a transaction so if any insert fails everything is rolled back e.g. if you expected to add 20 records and one failed after adding 15 then the 15 would be undone. Also, on the ExecuteNonQuery you can assign a variable, check the result, it did work, did it fail without error?

关于异常处理,这里没有但是将持有者留在两个捕获中供您决定。

In regards to exception handling, here there is none but place holders in the two catchs for you to decide.

private void button2_Click(object sender, EventArgs e)
{
    string Qry1 = "INSERT INTO tbladdbook(fBookTitle,fAuthor,fBookYr,fEdition,fPublication,fAccNo,fCallNo,fCategory,fBarCodeNo,fCurrentCopies) " + 
                    "VALUES (@Title, @Author, @BookYr, @Edition, @Publication, @AccNo, @CallNo, @Category, @BarCode, @Copies)";

    string Qry2 = "INSERT INTO tbltruecopies(fBookTitle, fAuthor, fBarCodeNo, fTrueCopies) " + 
                    "VALUES (@Title, @Author, @Barcode, @Copies)";

    using (var cn = new SqlConnection("TODO"))
    {
        using (SqlCommand cmd = new SqlCommand())
        {
            cmd.Connection = cn;

            for (var index = 0; index < dgvDetails.Rows.Count - 1; index++)
            {
                cmd.CommandText = Qry2;
                cmd.Parameters.AddWithValue("@Title", txtTITLE.Text);
                cmd.Parameters.AddWithValue("@Author", txTAUTHOR.Text);
                cmd.Parameters.AddWithValue("@Barcode", txtBARCODE.Text);
                cmd.Parameters.AddWithValue("@Copies", txtCOPIES.Text);
                try
                {
                    cmd.ExecuteNonQuery();
                }
                catch (Exception)
                {
                        /* your error handling */
                }

                cmd.CommandText = Qry1;
                cmd.Parameters.AddWithValue("@BookYr", txtBOOKYR.Text);
                cmd.Parameters.AddWithValue("@Edition", txtEDITION.Text);
                cmd.Parameters.AddWithValue("@Publication", txtPUBLICATION.Text);
                cmd.Parameters.AddWithValue("@AccNo", txtACCESSNO.Text);
                cmd.Parameters.AddWithValue("@CallNo", txtCALLNO.Text);
                cmd.Parameters.AddWithValue("@Category", txtCATEGORY.SelectedItem);
                try
                {
                    cmd.ExecuteNonQuery();
                }
                catch (Exception)
                {
                        /* your error handling */
                }
            }
        }
    }
}


这篇关于在datagridview中生成一个包含多行的数字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆