C# 如何将行添加到DataGridView,然后在C中从数据库加载数据#
对不起,如果我不能确切说明我想要什么,但请理解我的想法 我在面板上有产品,当单击产品时,我想将该产品项代码添加到datagrid视图的第一列,并从数据库中自动获取剩余数据 我想要一个AddToGridView(int productid)函数,当传递产品id时,它应该自动获取剩余的详细信息。datagrid可以很好地处理手动输入,但我无法添加带有产品id的行,也无法在单击产品图像时获取其剩余数据 请帮忙,谢谢 这是我当前的一段代码C# 如何将行添加到DataGridView,然后在C中从数据库加载数据#,c#,datagridview,C#,Datagridview,对不起,如果我不能确切说明我想要什么,但请理解我的想法 我在面板上有产品,当单击产品时,我想将该产品项代码添加到datagrid视图的第一列,并从数据库中自动获取剩余数据 我想要一个AddToGridView(int productid)函数,当传递产品id时,它应该自动获取剩余的详细信息。datagrid可以很好地处理手动输入,但我无法添加带有产品id的行,也无法在单击产品图像时获取其剩余数据 请帮忙,谢谢 这是我当前的一段代码 private void MainGrid_CellEndEd
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;
}
}
}
谢谢你发布这张图片;非常有用。我认为应该这样做
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);
}
}
}
尝试一下并反馈。ColumnIndex用于什么?请解释一下。此外,还可以将DataTable直接绑定到DataGridView。请参阅:您已经提供了CellEndEdit,您说它工作正常。您要修复的函数AddToGridView在哪里?用于的列索引为0,用于商品代码1,用于产品名称2,用于数量3,用于销售价格4,用于折扣,用于总金额5。我无法直接绑定数据表,因为我想在单击产品图像并加载其详细信息时将productid添加到datagridview