C# 更改Datagridview中的数量值时,价格也会更改。怎么可能?哪个事件将执行此操作。CellEndEdit已使用
我想在Datagridview中更改数量值,它也会更改价格。这怎么可能?使用其他cellproductname已使用的C windows form.CellEndEdit时,它将发生哪个事件C# 更改Datagridview中的数量值时,价格也会更改。怎么可能?哪个事件将执行此操作。CellEndEdit已使用,c#,datagridview,datagridcell,C#,Datagridview,Datagridcell,我想在Datagridview中更改数量值,它也会更改价格。这怎么可能?使用其他cellproductname已使用的C windows form.CellEndEdit时,它将发生哪个事件 private void dataGridViewSalesForm_CellEndEdit(object sender, DataGridViewCellEventArgs e) { try { string MatchingProduct = dataGridViewSa
private void dataGridViewSalesForm_CellEndEdit(object sender, DataGridViewCellEventArgs e)
{
try
{
string MatchingProduct = dataGridViewSalesForm.Rows[0].Cells[1].Value.ToString();
for (int i = 0; i < objArrayList.Count; i++)
{
if (objArrayList[i].ToString().ToLower().StartsWith(MatchingProduct.ToLower()))
{
dataGridViewSalesForm.Rows[0].Cells[1].Value = objArrayList[i].ToString();
break;
}
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
/*connection for loading brandname from brandtable*/
string get_strProductName = dataGridViewSalesForm.Rows[0].Cells[1].Value.ToString();
string quantity = "1";
SqlConnection conn = new SqlConnection(connstr);
try
{
conn.Open();
string qry = "SELECT T_Inv_Brands.brand_name FROM T_Inv_Products INNER JOIN T_Inv_Brands ON T_Inv_Products.brand_id = T_Inv_Brands.brand_id WHERE T_Inv_Products.prod_name ='" + get_strProductName + "'";
SqlCommand cmd = new SqlCommand(qry, conn);
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
da.Fill(ds);
dataGridViewSalesForm.Rows[0].Cells[2].Value = ds.Tables[0].Rows[0]["brand_name"].ToString();
dataGridViewSalesForm.Rows[0].Cells[3].Value = quantity;
conn.Close();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
/*connection for loading retailprice from producttable*/
SqlConnection connection = new SqlConnection(connstr);
try
{
conn.Open();
string qry = "SELECT retailprice FROM T_Inv_Products WHERE prod_name = '" + get_strProductName + "'";
SqlCommand cmd = new SqlCommand(qry, connection);
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataTable dt = new DataTable();
da.Fill(dt);
dataGridViewSalesForm.Rows[0].Cells[4].Value = Convert.ToDouble(dt.Rows[0]["retailprice"].ToString());
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
//double quantityload = Convert.ToDouble(dataGridViewSalesForm.Rows[0].Cells[3].Value.ToString());
//double pricefetch = Convert.ToDouble(dataGridViewSalesForm.Rows[0].Cells[4].Value.ToString());
//double result = quantityload * pricefetch;
//dataGridViewSalesForm.Rows[0].Cells[4].Value = result.ToString();
}
您似乎认为只能对一个单元格或列使用事件。相反,您需要以这样一种方式对事件进行编码:所有必要的单元格和/或列都有自己的逻辑 它有助于将代码划分成更小的部分,并使用有用的名称记录它们所包含的逻辑 下面是一个具有两个分支的示例,一个用于特定单元格,另一个用于特定列。我传递DataGridViewCellEventArgs,以便函数可以像实际事件一样访问DataGridView DGV。。当然,您可以根据需要对其进行扩展:
int quantityColumnIndex = 3; // use your own numbers..
int currencyColumnIndex = 1; // ..and names!!
int currencyRowIndex = 0;
int pricePerUnitColumnIndex = 7;
int totalPriceColumnIndex = 8;
int totalBasePriceColumnIndex = 4;
private void DGV_CellEndEdit(object sender, DataGridViewCellEventArgs e)
{
if (e.ColumnIndex == quantityColumnIndex) doPriceCalc(e);
else if (e.ColumnIndex == currencyColumnIndex && e.RowIndex == currencyRowIndex)
doAllCalc(e);
}
void doPriceCalc(DataGridViewCellEventArgs e)
{
// 1st example
DGV[totalPriceColumnIndex, e.RowIndex].Value =
(int)DGV[quantityColumnIndex, e.RowIndex].Value *
(decimal)DGV[pricePerUnitColumnIndex, e.RowIndex].Value;
}
void doAllCalc(DataGridViewCellEventArgs e)
{
// 2nd example
decimal currency = (decimal) DGV[currencyColumnIndex,currencyRowIndex ].Value;
for (int row = 0; row < DGV.Rows.Count; row++)
DGV[pricePerUnitColumnIndex, e.RowIndex].Value =
(decimal)DGV[totalBasePriceColumnIndex, e.RowIndex].Value * currency;
}
请注意,我已通过列的索引为这些列编制了索引。您也可以按他们的名字为他们编制索引,例如:DGV[PricePerUnit,e.rowIndex]请在这个问题上帮助我Soner gönül