如何在数据网格视图的两列上执行乘法并在第三列中分配它? [英] How to perform multiplication on two column for datagrid view and assigning it in third column?
本文介绍了如何在数据网格视图的两列上执行乘法并在第三列中分配它?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
这是我的代码,我试图显示Rate * Supply列值的乘法并将其分配给数据网格视图中的Amount列:
Here is my code, I am trying to display multiplication of Rate * Supply column values and assigning it to the Amount column in data grid view :
try
{
Query = "Select id,Code,Description,Rate,Cust_Id,Supply,Empty,Amount,Received from Items ";
adap = new SQLiteDataAdapter(Query, GlobalVars.conn);
ds = new DataSet();
adap.Fill(ds, "Items");
dtgVOuchers.DataSource = ds.Tables[0];
ds.Tables[0].Columns["Amount"].DefaultValue = (ds.Tables[0].Columns["Rate"].DefaultValue) * (ds.Tables[0].Columns["Supply"].DefaultValue); //error
//dtgVOuchers.Rows.Clear();
ds.Tables[0].Clear();
dtgVOuchers.Refresh();
}
有谁知道如何在数据网格视图中完成此功能? 。 。请帮我纠正这段代码。谢谢
注意:我想在不在查询中的代码中加倍
Does anyone know how I can accomplish this functionality in data grid view ? . .Please help me to correct this code. Thanks
Note : i want to multiply in code not in query
推荐答案
在你的代码中尝试这个。
Try this in your Code.
private void MultiplyMethod(int Rate , int Supply, int Amount )
{
double outVal = 0;
foreach (DataGridViewRow row in dataGridView1.Rows)
{
double localSum = (Convert.ToDouble(row.Cells[Rate].Value) * Convert.ToDouble(row.Cells[Supply].Value);
outVal = outVal + localSum;
row.Cells[Amount].Value = localSum;
}
}
}
DataTable table1 = new DataTable("Registration");
table1.Columns.Add("L1");
table1.Columns.Add("L2");
table1.Columns.Add("L3");
table1.Rows.Add("1",5);
table1.Rows.Add("3", 5);
table1.Rows.Add("4", 9);
table1.Rows.Add("7", 3);
table1.Rows.Add("6", 6);
DataSet Dset = new DataSet("DSET1");
Dset.Tables.Add(table1);
if (Dset.Tables[0].Rows.Count > 0)
{
for (int i = 0; i < Dset.Tables[0].Rows.Count; i++)
{
DataRow dr = Dset.Tables[0].Rows[i];
int a = Convert.ToInt32(dr["L1"]);
int b = Convert.ToInt32(dr["L2"]);
int c = a * b;
dr["L3"] = c.ToString();
}
}
祝你好运。
Good luck.
这篇关于如何在数据网格视图的两列上执行乘法并在第三列中分配它?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文