SIGN UP MEMBER LOGIN:    
Blog

use of datagrid and sum column in datagrid

Posted by pooja bh Blogs | ASP.NET using VB.NET Apr 01, 2010
This article explains the working of datagrid and how sum of the column code works
//code file
marks.aspx.vb 

Imports System
Imports System.Data
Imports System.Data.SqlClient
Imports System.DateTime 

Partial Class marks
    Inherits System.Web.UI.Page
    Dim Table1 As
DataTable
    Dim i, j As Integer
    Dim x, y As Integer
    Dim com As New SqlCommand
    Dim dr As SqlDataReader
    Dim DSet As New DataTable
    Dim da As New SqlDataAdapter
    Dim str1 As String
    Dim ds As New DataSet 

    'This is the column type which will be added to the Datatable

    Dim row1 As DataRow

    Dim con As New SqlConnection()   

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load 
        Dim internalSum, ints2, ints3, ints4 As
Decimal 

        Dim cmd As SqlCommand = New SqlCommand("select name,physiscs,chemistry,english from marks1,student1 where(student1.sid=marks1.sid)", New SqlConnection            ("Server=localhost;Database=master;Trusted_Connection=True;")) 

        cmd.Connection.Open() 

        'datagrid.DataSource = cmd.ExecuteReader()

        da.SelectCommand = cmd 
        da.Fill(DSet)

        ' datagrid.DataBind() 

         If DSet.Rows.Count >= 1 Then 
            Dim Row1 As
DataRow
            Dim Table1 As DataTable
            Table1 = New DataTable("m"

            Dim sid As DataColumn = New DataColumn("index")
            sid.DataType = System.Type.GetType("System.String")

            Table1.Columns.Add(sid)
            Row1 = Table1.NewRow() 
            Dim name As DataColumn = New DataColumn("name")
            name.DataType = System.Type.GetType("System.String")
            Table1.Columns.Add(name)

            Dim physiscs As DataColumn = New DataColumn("physiscs")
            physiscs.DataType = System.Type.GetType("System.Int32")
            Table1.Columns.Add(physiscs) 
 
            Dim chem As DataColumn = New DataColumn("chemistry")
            chem.DataType = System.Type.GetType("System.Int32")
           Table1.Columns.Add(chem)

            Dim eng As DataColumn = New DataColumn("english")
            eng.DataType = System.Type.GetType("System.Int32")

           Table1.Columns.Add(eng)

            Dim sum As DataColumn = New DataColumn("total")
            sum.DataType = System.Type.GetType("System.Int32"
            Table1.Columns.Add(sum)  
            Row1 = Table1.NewRow()
            Table1.Rows.Add(Row1)
 
            If DSet.Rows.Count <> 1
Then
                For i = 0 To DSet.Rows.Count - 1
                    Row1 = Table1.NewRow()
                    Row1.Item(0) = i + 1
                    Row1.Item(1) = DSet.Rows(i).Item(0).ToString
                    Row1.Item(2) = DSet.Rows(i).Item(1).ToString
                    Row1.Item(3) = DSet.Rows(i).Item(2).ToString
                    Row1.Item(4) = DSet.Rows(i).Item(3).ToString
                    Row1.Item(5) = DSet.Rows(i).Item(1) + DSet.Rows(i).Item(2) + DSet.Rows(i).Item(3)
                    Table1.Rows.Add(Row1)
               
Next
            End If

            internalSum = Table1.Compute("SUM(physiscs)", String.Empty).ToString
            ints2 = Table1.Compute("SUM(chemistry)", String.Empty).ToString
            ints3 = Table1.Compute("SUM(english)", String.Empty).ToString
            ints4 = Table1.Compute("SUM(total)", String.Empty).ToString
            Row1 = Table1.NewRow()

            Row1.Item(0) = ""
 
            Row1.Item(1) =
""

            Row1.Item(2) = internalSum

            Row1.Item(3) = ints2
            Row1.Item(4) = ints3
            Row1.Item(5) = ints4
            Table1.Rows.Add(Row1)

            datagrid.DataSource = Table1
            datagrid.DataBind()
        
Else
            datagrid.Visible = False
        End If 
    End
Sub 
End Class


marks.vb

<%@ Page Language="VB" AutoEventWireup="false" CodeFile="marks.aspx.vb" Inherits="marks" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head id="Head1" runat="server">
    <title>Untitled Page</title>
</head>
<
body>
    <form id="form1" runat="server">
    <div>
        <asp:GridView ID="datagrid" runat="server">
        </asp:GridView>
        </div>
    </form>
</body>
</
html>

share this blog :
post comment