C# 如何通过区分项目来计算某一特定列的总计?
我有一个C# 如何通过区分项目来计算某一特定列的总计?,c#,.net,winforms,ado.net,datagridview,C#,.net,Winforms,Ado.net,Datagridview,我有一个DataGridView,其中包含项目、描述、数量、费率和总额等列。 我有两种类型的项目,一个项目的vapasi==是(vapasi是一种存款金额),另一个项目的vapasi==否。我想通过区分带vapasi的项目和不带vapasi的项目来计算“总计”列的总和,并想将计算出的总计显示在两个相应的文本框中,即“txtboxwithvapasi”,n'txtboxwithout vapasi'在网格后面。我执行了以下代码: private void grdPurchase_CellEndEd
DataGridView
,其中包含项目、描述、数量、费率和总额等列。
我有两种类型的项目,一个项目的vapasi==是(vapasi是一种存款金额),另一个项目的vapasi==否。我想通过区分带vapasi的项目和不带vapasi的项目来计算“总计”列的总和,并想将计算出的总计显示在两个相应的文本框中,即“txtboxwithvapasi”,n'txtboxwithout vapasi'在网格后面。我执行了以下代码:
private void grdPurchase_CellEndEdit_1(object sender, DataGridViewCellEventArgs e)
{
try
{
try
{
string value = grdPurchase.Rows[e.RowIndex].Cells[e.ColumnIndex].Value.ToString();
if (e.ColumnIndex == 2)
{
int val = int.Parse(value);
quantity = val;
}
if (e.ColumnIndex == 4)
{
float val = float.Parse(value);
total = val;
if (vapasi1 == "Yes")
{
vtot += total; //vtot=0+10
txt_forvapasitotal.Text = vtot.ToString(); //10
float vapsitot =float.Parse(txt_forvapasitotal.Text);
float vapsicalculate = (vapsitot * a);
float tax = vapsicalculate / 100;
float with_vapasi = vtot + tax;
txt_withvapasi.Text =Convert.ToString(with_vapasi);
}
else
{
nvtot = 0;
for (int i = 0; i < grdPurchase.Rows.Count; i++)
{
if (vapasi1 == "No")
{
if (grdPurchase.Rows[e.RowIndex].Cells[3].Selected == true)
{
nvtot += float.Parse(grdPurchase[4, i].EditedFormattedValue.ToString());
txt_withoutvapasitot.Text = nvtot.ToString();
}
}
}
}
txt_vapasiincludedtot.Text =(float.Parse(txt_withvapasi.Text) +float.Parse(txt_withoutvapasitot.Text)).ToString();
}
if (e.ColumnIndex == 1)
{
int val = int.Parse(value);
materialid = val;
string vapasi = "Select material_vapasi from tbl_material_master where active_flag=1 AND material_id =" + materialid + "";
SqlCommand cmd = new SqlCommand(vapasi, con);
sdr = cmd.ExecuteReader();
if (sdr.HasRows)
{
while (sdr.Read())
{
vapasi1 = sdr["material_vapasi"].ToString();
}
}
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message.ToString());
}
grdPurchase.Columns[3].ReadOnly = false;
}
catch (Exception ex)
{
MessageBox.Show(ex.Message.ToString());
}
}
private void grdpourchase\u CellEndEdit\u 1(对象发送方,DataGridViewCellEventArgs e)
{
尝试
{
尝试
{
字符串值=grdPurchase.Rows[e.ROWDINDEX]。单元格[e.ColumnIndex]。值。ToString();
如果(e.ColumnIndex==2)
{
int val=int.Parse(值);
数量=val;
}
如果(e.ColumnIndex==4)
{
float val=float.Parse(值);
总计=val;
如果(vapasi1==“是”)
{
vtot+=总计;//vtot=0+10
txt_forvapasitotal.Text=vtot.ToString();//10
float vapsitot=float.Parse(txt_forvapasitotal.Text);
浮点数vapsicalculate=(vapsitot*a);
浮动税=vapsicalculate/100;
浮动汇率=vtot+税收;
txt_with vapasi.Text=Convert.ToString(with_vapasi);
}
其他的
{
nvtot=0;
对于(int i=0;i
问题是:-当我选择第一行有vapasi的项目和第二行没有vapasi的项目时,它工作正常。但是如果我选择第三行的任何项目,那么它会将“没有vapasi的TXTBox”中的所有三个项目相加,而不区分项目。你需要跟踪vapasi是“是”还是“否”对于网格中的每一行,但您使用的是单个变量。不能将此列添加到网格中(如果不想向用户显示此列,则将其作为隐藏列)?然后,每当需要计算总计时,只需遍历网格行并检查vapasi列,而不是vapasi1变量