foreach (DataGridViewRow row in dtgVOuchers.Rows)
{
row.Cells[dtgVOuchers.Columns["Amount"].Index].Value = (Convert.ToDouble(row.Cells[dtgVOuchers.Columns["Rate"].Index].Value) * Convert.ToDouble(row.Cells[dtgVOuchers.Columns["Supply"].Index].Value));
}
How to perform multiplication on two column for datagrid view and assigning it in third column?
-
05-08-2022 - |
Question
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
La solution 3
Autres conseils
just use the query
Select id,Code,Description,Rate,Cust_Id,Supply,Empty,isnull(Rate,0)*isnull(Supply,0) as [Amount],Received from Items
DataTable myDataTable = new DataTable();
DataColumn quantityColumn = new DataColumn("Quantity");
DataColumn rateColumn = new DataColumn("Rate");
DataColumn priceColumn = new DataColumn ("Price");
quantityColumn.DataType = typeof(int);
rateColumn.DataType = typeof(int);
myDataTable.Columns.Add(quantityColumn);
myDataTable.Columns.Add(rateColumn);
myDataTable.Columns.Add(priceColumn);
//Set the Expression here
priceColumn.Expression = "Quantity * Rate";
Use the below code..run a loop to update the Amount column in all rows
Note : Though this method is not preferred as it effects performance.So do this calculation in the sql query.I dont understand the reason behind not doing the munltiplication in SQL query..
foreach (DataRow row in ds.Tables[0].Rows)
{
row["Amount"]= Convert.ToDecimal(row["Rate"])* Convert.ToDecimal(row["Supply"]);
ds.Tables[0].AcceptChanges();
}
dtgVOuchers.DataSource = ds.Tables[0];
dtgVOuchers.Refresh();
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow