我有一个名为TBL_PRODUCTS
的表
CREATE TABLE TBL_PRODUCTS
(
Products_ID varchar(50) PRIMARY KEY,
Products_Name varchar(100) NOT NULL,
Products_Categorys_ID int,
Products_Qty DECIMAL(16,0),
Products_Sales_Price DECIMAL(16,0),
Products_Cost_Price DECIMAL(16,0)
);
我在Windows应用程序上有一个datagridview,它有6列:
(Products_ID, Products_Name, Products_Categorys_Name, Products_Qty, Products_Sales_Price, Products_Total)
我希望用户在第一列(Products_ID)上写该产品在同一行上显示的代码,在该行上显示该行的其他详细信息,例如(Products_Name, Products_Categorys_Name, Products_Sales_Price)
,依此类推...
我该怎么做?
我尝试了以下代码:
SqlConnection con = new SqlConnection("Data Source=GMCADIOM-PC;Initial Catalog=TEMP;Integrated Security=True;Connect Timeout=15;Encrypt=False;TrustServerCertificate=False;ApplicationIntent=ReadWrite;MultiSubnetFailover=False");
string sqlqry = "SELECT [Products_ID] as 'كود المنتج',[Products_Name] as 'اسم المنتج',Products_Categorys_Name as 'اسم الصنف',[Products_Qty] as 'الكميه المتاحه',[Products_Sales_Price] as 'سعر البيع',[Products_Cost_Price] as 'سعر الشراء' FROM TBL_PRODUCTS INNER JOIN TBL_PRODUCTS_CATEGORYS ON TBL_PRODUCTS_CATEGORYS.Products_Categorys_ID=TBL_PRODUCTS.Products_Categorys_ID WHERE [Products_ID] LIKE '"+ dgvBillsList.CurrentRow.Cells[0].Value.ToString()+ "'";
SqlCommand cmd = new SqlCommand(sqlqry, con);
con.Open();
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataTable dt = new DataTable();
da.Fill(dt);
try
{
if (dt.Rows.Count > 0)
{
for (int i = 0; i < dt.Rows.Count; i++)
{
dgvBillsList.CurrentRow.Cells["Products_ID"].Value = dt.Rows[i]["كود المنتج"];
dgvBillsList.CurrentRow.Cells["Products_Name"].Value = dt.Rows[i]["اسم المنتج"];
dgvBillsList.CurrentRow.Cells["Products_Categorys_Name"].Value = dt.Rows[i]["اسم الصنف"];
dgvBillsList.CurrentRow.Cells["Products_Sales_Price"].Value = dt.Rows[i]["سعر البيع"];
}
}
else
{
var result = MessageBox.Show("هذا المنتج غير موجود بقاعده البيانات ، هل تريد اضافته ؟؟", "", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
if (result == DialogResult.Yes)
{
frmProductsNew frm = new frmProductsNew();
frm.StartPosition = FormStartPosition.CenterScreen;
frm.ShowDialog();
}
else if (result == DialogResult.No)
{
//dgvBillsList.CurrentRow.Cells[0].Value = null;
//dgvBillsList.CurrentRow.Cells[0].Selected = true;
//dgvBillsList.CurrentCell = dgvBillsList.CurrentRow.Cells[0];
//dgvBillsList.CurrentCell.Selected = true;
//dgvBillsList.BeginEdit(true);
//dgvBillsList.CurrentCell = dgvBillsList.Rows[dgvBillsList.Rows.Count].Cells[0];
}
}
CONF_Calculate.Calculate_DGV_Products(dgvBillsList);
CONF_Calculate.Calculate_DGV_Total(dgvBillsList, txtBillsAmount);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
但是它在查询方面给了我错误
对象引用未设置为对象的实例 dgvBillsList.CurrentRow.Cells [0] .Value.ToString()