0

我正在尝试创建一个用于创建发票的应用程序,并且我正在使用 Gridview 动态创建行并添加多个项目

代码:

 try
        {
            if (e.ColumnIndex == 2 || e.ColumnIndex == 3)
            {

                double cell1 = Convert.ToSingle(dataGridView1.CurrentRow.Cells[2].Value);

                double cell2 = Convert.ToSingle(dataGridView1.CurrentRow.Cells[3].Value);

                if (cell1.ToString() != "" && cell2.ToString() != "")
                {
                    dataGridView1.CurrentRow.Cells[4].Value = cell1 * cell2;

                }

            }
            label1.Visible = true;
            decimal tot = 0;
            for (int i = 0; i <= dataGridView1.RowCount - 1; i++)
            {
                tot += Convert.ToDecimal(dataGridView1.Rows[i].Cells[4].Value);
            }
            if (tot == 0)
            {
                //MessageBox.Show("No Records Found");
            }
            txt_netamount.Text = tot.ToString();
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.ToString());
        }
    }

    void Save()
    {

        try
        {

            if (dataGridView1.Rows.Count > 1)
            {

                for (int i = 0; i <= dataGridView1.Rows.Count - 1; i++)
                {

                    string col1 = dataGridView1.Rows[i].Cells[0].Value.ToString();

                    string col2 = dataGridView1.Rows[i].Cells[1].Value.ToString();

                    string col3 = dataGridView1.Rows[i].Cells[2].Value.ToString();

                    string col4 = dataGridView1.Rows[i].Cells[3].Value.ToString();

                    string col5 = dataGridView1.Rows[i].Cells[4].Value.ToString();

                    using (SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["Sample.Properties.Settings.Business_Ultra_1_3_CafeConnectionString"].ConnectionString))
                    {

                        string insert = "INSERT INTO InvoiceItems(SNo,Quantity,Rate,Description,Total) VALUES (@SNo,@Quantity,@Rate,@Description,@Total)";

                        con.Open();

                        SqlCommand cmd = new SqlCommand(insert, con);

                        cmd.Parameters.AddWithValue("@SNo", col1.ToString());

                        cmd.Parameters.AddWithValue("Description", col2.ToString());

                        cmd.Parameters.AddWithValue("@Quantity", col3.ToString());

                        cmd.Parameters.AddWithValue("@Rate", col4.ToString());

                        cmd.Parameters.AddWithValue("Total", col5.ToString());

                        cmd.ExecuteNonQuery();

                        con.Close();

                    }

                }

            }

        }

        catch (Exception ex)
        {

            ex.Message.ToString();

        }

如何在 GridView 的第二列 (ColumnIndex == 1) 上应用自动完成功能,以便我可以从数据库中获取产品名称和速率?

4

0 回答 0