1

我想在 Datagridview 中更改数量值,它也会改变价格。怎么可能?哪个事件是使用 C# windows form.CellEndEdit 已被其他单元格(产品名称)使用的。

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();
}
4

1 回答 1

1

您似乎认为您只能将事件用于一个单元格或列。相反,您需要以所有必要的单元格和/或列都有自己的逻辑的方式对事件进行编码。

它有助于将您的代码分成更小的部分,并使用有用的名称记录它们包含的逻辑!

这是一个有两个分支的示例,一个用于某个单元格,一个用于某个列。我传递了,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]

于 2015-06-04T08:13:41.330 回答