.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
Sign In
Win Surprise Gifts!!!

Top 5 Contributors of the Month
Melody Anderson

Home >> Code Snippets >> Windows Form Controls >> Post New Resource Bookmark and Share   

 Subscribe to Code Snippets

Calculate the running total in a datagridview

Posted By:Santosh       Posted Date: October 15, 2009    Points: 10    Category: Windows Form Controls    URL: http://www.dotnetspark.com  

Please follow the below code,this will help to calculate the running total in a datagridview
VB Code
  Dim dtRunningTot As New DataTable
    Shared StrConn As String = ""
    Private Function ConnectionString() As String
        StrConn = "Integrated Security=SSPI;Persist Security Info=False;Initial Catalog=Santosh_Practice;Data Source=SANTOSH\SQLEXPRESS" 'Myconnection is Key in App.Config
        Return StrConn
    End Function
    Private Function GetConnectObj() As SqlConnection
        Return New SqlConnection(ConnectionString())
    End Function
    Private Function FillDatatable(ByVal Query As String) As DataTable
        Dim objDT As New DataTable()
        Dim objda As New SqlDataAdapter(Query, ConnectionString())
        Return objDT
    End Function
    Private Sub frmRunningTotal_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        dtRunningTot = FillDatatable("Select salary from RunningTotal")
        DataGridView1.DataSource = dtRunningTot
        For i As Integer = 0 To DataGridView1.Rows.Count - 2
            For j As Integer = 0 To DataGridView1.Columns.Count - 1
                If i = 0 Then
                    DataGridView1.Rows(i).Cells("RunningTotal").Value = Val(DataGridView1.Rows(i).Cells("Salary").Value)
                    DataGridView1.Rows(i).Cells("RunningTotal").Value = Val(DataGridView1.Rows(i - 1).Cells("RunningTotal").Value) + Val(DataGridView1.Rows(i).Cells("Salary").Value)
                End If
    End Sub

C# Code
  private DataTable dtRunningTot = new DataTable();
    public static string StrConn = "";
//TODO: INSTANT C# TODO TASK: Insert the following converted event handler wireups at the end of the 'InitializeComponent' method for forms, 'Page_Init' for web pages, or into a constructor for other classes:
base.Load += frmRunningTotal_Load;

    private string ConnectionString()
        StrConn = "Integrated Security=SSPI;Persist Security Info=False;Initial Catalog=Santosh_Practice;Data Source=SANTOSH\\SQLEXPRESS"; //Myconnection is Key in App.Config
        return StrConn;
    private SqlConnection GetConnectObj()
        return new SqlConnection(ConnectionString());
    private DataTable FillDatatable(string Query)
        DataTable objDT = new DataTable();
        SqlDataAdapter objda = new SqlDataAdapter(Query, ConnectionString());
        return objDT;
    private void frmRunningTotal_Load(object sender, System.EventArgs e)
        dtRunningTot = FillDatatable("Select salary from RunningTotal");
        DataGridView1.DataSource = dtRunningTot;
        for (int i = 0; i <= DataGridView1.Rows.Count - 2; i++)
            for (int j = 0; j < DataGridView1.Columns.Count; j++)
                if (i == 0)
                    DataGridView1.Rows[i].Cells["RunningTotal"].Value = Microsoft.VisualBasic.Conversion.Val(DataGridView1.Rows[i].Cells["Salary"].Value);
                    DataGridView1.Rows[i].Cells["RunningTotal"].Value = Microsoft.VisualBasic.Conversion.Val(DataGridView1.Rows[i - 1].Cells["RunningTotal"].Value) + Microsoft.VisualBasic.Conversion.Val(DataGridView1.Rows[i].Cells["Salary"].Value);

How was the Coding ??
Any Problem Plz feel free to Contact Santosh.mca08@gmail.com
i will reply u soon

Thanx and Regards
More Details Plz visit my blog


Further Readings:


No response found. Be the first to respond this post

Post Comment

You must Sign In To post reply
Find More code samples in C#, ASP.Net, Vb.Net and more Here

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend