Click here to Skip to main content
15,891,621 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
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 :
C#
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
Posted

Try this in your Code.

C#
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;
        }        
    }
}
 
Share this answer
 
Comments
Member 8840306 28-Nov-13 6:20am    
private void MultiplyMethod(int Rate , int Supply, int Amount ) How to get the value of these arguments in function?
Karthik_Mahalingam 28-Nov-13 6:38am    
he is mentioning the column index of the gridview...
AnthonyMG 28-Nov-13 6:26am    
See these are all not values, these are your dataGridColumn's, which you use in your dataGrid
Rate - 1st column in dataGrid
Supply - 2nd Column
Amount - 3rd Column

you can Pass corresponding Column's index to this method as per your logic
C#
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.
 
Share this answer
 
Comments
Member 8840306 29-Nov-13 23:17pm    
DataSet Dset = new DataSet("DSET1"); what is "DSET1"?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900