如何将行添加到DataGridView,然后在C#中从数据库加载数据

时间:2018-08-10 20:41:30

标签: c# datagridview

对不起,如果我无法确切说明我想要什么,请理解这个想法。

Image of software which i am programming

我在面板上有产品,当单击产品时,我想将该产品项目代码添加到datagrid视图的第一列,并自动从数据库中获取剩余数据。

我希望在传递产品ID时使用AddToGridView(int productid)的功能,它应该自动获取其余详细信息。该datagrid可以正常使用手动输入,但是单击产品图像时,我无法添加带有产品ID的行并获取其剩余数据。

请帮助谢谢

这是我当前的代码

private void MainGrid_CellEndEdit(object sender, DataGridViewCellEventArgs e)
    {
        if (e.ColumnIndex == 0)
        {
            try
            {
                string newvalue;
                newvalue = (MainGrid[e.ColumnIndex, e.RowIndex].Value).ToString();
                con.Open();
                SqlDataAdapter da = new SqlDataAdapter("SELECT name,saleprice from Products where productid='" + newvalue + "'", con);
                DataTable dt = new DataTable();
                da.Fill(dt);
                MainGrid.Rows[e.RowIndex].Cells[1].Value = dt.Rows[0][0].ToString();
                MainGrid.Rows[e.RowIndex].Cells[3].Value = dt.Rows[0][1].ToString();
                MainGrid.Rows[e.RowIndex].Cells[2].Value = 1;
                MainGrid.Rows[e.RowIndex].Cells[4].Value = 0;
                con.Close();
                foreach (DataGridViewRow row in MainGrid.Rows)
                {
                    row.Cells[MainGrid.Columns[5].Index].Value = (Convert.ToDouble(row.Cells[MainGrid.Columns[2].Index].Value) * Convert.ToDouble(row.Cells[MainGrid.Columns[3].Index].Value));
                }
            }
            catch (Exception ex)
            {

                MainGrid.Rows[e.RowIndex].Cells[0].Value = "";
                MainGrid.Rows[e.RowIndex].Cells[1].Value = "";
                MainGrid.Rows[e.RowIndex].Cells[3].Value = "";
                MainGrid.Rows[e.RowIndex].Cells[2].Value = "";
                MainGrid.Rows[e.RowIndex].Cells[4].Value = "";
                MainGrid.Rows[e.RowIndex].Cells[5].Value = "";

                con.Close();

                MessageBox.Show("Product does not exists");
            }
        } else if (e.ColumnIndex == 1)
        {
            try
            {
                string newvalue;
                newvalue = (MainGrid[e.ColumnIndex, e.RowIndex].Value).ToString();
                con.Open();
                SqlDataAdapter da = new SqlDataAdapter("SELECT productid,saleprice from Products where name like'" + newvalue + "%'", con);
                DataTable dt = new DataTable();
                da.Fill(dt);
                MainGrid.Rows[e.RowIndex].Cells[0].Value = dt.Rows[0][0].ToString();
                MainGrid.Rows[e.RowIndex].Cells[3].Value = dt.Rows[0][1].ToString();
                MainGrid.Rows[e.RowIndex].Cells[2].Value = 1;
                MainGrid.Rows[e.RowIndex].Cells[4].Value = 0;
                con.Close();
                foreach (DataGridViewRow row in MainGrid.Rows)
                {
                    row.Cells[MainGrid.Columns[5].Index].Value = (Convert.ToDouble(row.Cells[MainGrid.Columns[2].Index].Value) * Convert.ToDouble(row.Cells[MainGrid.Columns[3].Index].Value));
                }
            }
            catch (Exception ex)
            {

                MainGrid.Rows[e.RowIndex].Cells[0].Value = "";
                MainGrid.Rows[e.RowIndex].Cells[1].Value = "";
                MainGrid.Rows[e.RowIndex].Cells[3].Value = "";
                MainGrid.Rows[e.RowIndex].Cells[2].Value = "";
                MainGrid.Rows[e.RowIndex].Cells[4].Value = "";
                MainGrid.Rows[e.RowIndex].Cells[5].Value = "";

                con.Close();

                MessageBox.Show("Product does not exists");
            }
        }
        if (e.ColumnIndex == 2)
        {
            foreach (DataGridViewRow row in MainGrid.Rows)
            {
                double total = (Convert.ToDouble(row.Cells[MainGrid.Columns[2].Index].Value) * Convert.ToDouble(row.Cells[MainGrid.Columns[3].Index].Value));
                double discount = Convert.ToDouble(row.Cells[MainGrid.Columns[4].Index].Value);
                total = total - discount;
                row.Cells[MainGrid.Columns[5].Index].Value = total;

            }
        }
        if (e.ColumnIndex == 3)
        {


            foreach (DataGridViewRow row in MainGrid.Rows)
            {
                double total = (Convert.ToDouble(row.Cells[MainGrid.Columns[2].Index].Value) * Convert.ToDouble(row.Cells[MainGrid.Columns[3].Index].Value));
                double discount = Convert.ToDouble(row.Cells[MainGrid.Columns[4].Index].Value);
                total = total - discount;
                row.Cells[MainGrid.Columns[5].Index].Value = total;
            }


        }
        if (e.ColumnIndex == 4)
        {

            foreach (DataGridViewRow row in MainGrid.Rows)
            {
                double total = (Convert.ToDouble(row.Cells[MainGrid.Columns[2].Index].Value) * Convert.ToDouble(row.Cells[MainGrid.Columns[3].Index].Value));
                double discount = Convert.ToDouble(row.Cells[MainGrid.Columns[4].Index].Value);
                total = total - discount;
                row.Cells[MainGrid.Columns[5].Index].Value = total;

            }


        }
    }

1 个答案:

答案 0 :(得分:0)

感谢您发布该图片;非常有帮助。我认为应该这样做。

using System;
using System.Data;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace WindowsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            dataGridView1.ColumnCount = 3;
            dataGridView1.Columns[0].Name = "Product ID";
            dataGridView1.Columns[1].Name = "Product Name";
            dataGridView1.Columns[2].Name = "Product Price";

            string[] row = new string[] { "1", "Product 1", "1000" };
            dataGridView1.Rows.Add(row);
            row = new string[] { "2", "Product 2", "2000" };
            dataGridView1.Rows.Add(row);
            row = new string[] { "3", "Product 3", "3000" };
            dataGridView1.Rows.Add(row);
            row = new string[] { "4", "Product 4", "4000" };
            dataGridView1.Rows.Add(row);
        }
    }
}

试一试并反馈。