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

1 つのセルまたは列に対してのみイベントを使用できると考えているようです。代わりに、必要なすべてのセルや列が独自のロジックを取得するように、イベントをコーディングする必要があります。

コードに含まれるロジックを文書化する便利な名前を使用して、コードを小さな断片に分割するのに役立ちます!

これは、特定のセル用と特定の列用の2 つのブランチの例です。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 に答える