Click here to Skip to main content
15,888,321 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i need grand total of col1 in datagridview..

thx in advance

col1
20
10
30
-----
60
Posted
Updated 3-Jan-13 1:08am
v3
Comments
CHill60 3-Jan-13 6:48am    
What have you tried so far - do you have any code to post?
Master Vinu 3-Jan-13 6:52am    
no iam confuse how to do it?? m new to c#
Master Vinu 3-Jan-13 6:53am    
private void findlaksajob_KeyPress(object sender, KeyPressEventArgs e)
{
if (e.KeyChar == (char)13)
{
string sql = "SELECT [Jobcard],[Planqty],[Mouldqty],[Operator],[Supervisor],[Profilename],[Process],[Date],[Remark],[Approvedby],[id],[Division] FROM [SAPProduction].[ProductionData] WHERE JobCard ='" + findlaksajob.Text.ToString() + "'";
SqlDataAdapter da = new SqlDataAdapter(sql, objConn1);
DataSet ds = new DataSet();
objConn1.Open();
da.Fill(ds, "ProductionData");
dataGridView2.DataSource = ds;
dataGridView2.DataMember = "ProductionData";
objConn1.Close();
e.Handled = true;
}

}
Master Vinu 3-Jan-13 6:54am    
using above query i get data in gridview

while you add datasource in gridview
put a line after you assign it
C#
Dgv.AllowUserToAddRows = false;
Dgv.Rows.add();



in datagridview cell value change
C#
if (e.columnindex == 1)//column index of column you want to sum
{
    double amount = 0;
    For (i=0; i<= dgv.rows.count-2;i++)
    {
      amount += Convert.ToDouble(dgv.Row[i].Cells[1].Text.Trim());
    }
    dgv.rows[i].cells[1].value = amount.ToString();
}


Note put same code in row - deleted event also...

Happy Coding!
:)
 
Share this answer
 
v3
Comments
Master Vinu 3-Jan-13 7:21am    
i need col1 row total on end????? pls clarify me
Aarti Meswania 3-Jan-13 7:22am    
end means in gridview's last row?
Master Vinu 3-Jan-13 7:23am    
ya ....dynamically add on last row
Aarti Meswania 3-Jan-13 7:27am    
see updated solution
Master Vinu 3-Jan-13 7:30am    
private void findlaksajob_KeyPress(object sender, KeyPressEventArgs e) { if (e.KeyChar == (char)13) { string sql = "SELECT [Jobcard],[Planqty],[Mouldqty],[Operator],[Supervisor],[Profilename],[Process],[Date],[Remark],[Approvedby],[id],[Division] FROM [SAPProduction].[ProductionData] WHERE JobCard ='" + findlaksajob.Text.ToString() + "'"; SqlDataAdapter da = new SqlDataAdapter(sql, objConn1); DataSet ds = new DataSet(); objConn1.Open(); da.Fill(ds, "ProductionData"); dataGridView2.DataSource = ds; dataGridView2.DataMember = "ProductionData"; objConn1.Close(); e.Handled = true; } }
You can take an object of DataGridView and in a foreach loop you can loop through each row and keep adding them to the total amount.

C#
DataGridView dgv = dataGridView1;
int amount = 0; //maybe you can use double if that is what you need

foreach (DataGridViewRow row in dgv)
{
    amount += Convert.ToInt32(row.Amount);
}

//After the loop ends 
 
Share this answer
 
v2
Comments
Master Vinu 3-Jan-13 7:19am    
i need col1 row total on end????? pls clarify me
shiny13 3-Jan-13 8:23am    
well a column means that it will be be NEXT to the amount column, so you have to add another column. But I think according to your example in the question you are looking for a Row in the end that is the sum of all rows. So after the loop ends add make a DataGridViewRow object and then add the amount value then in the existing dataGridView just add your row!
Try:
C#
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
        double amount = 0;
        //Check the RowType of the specific row
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            amount += Convert.ToDouble(e.Row.Cells[1].Text.Trim());
            GridView1.FooterRow.Cells[1].Text = amount.ToString("#,###.00");
        }
 }
 
Share this answer
 
Comments
Master Vinu 3-Jan-13 7:08am    
need on windows form.

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