ASP DOT NET

Tuesday, September 17, 2019

How to create new grid select gridview rows using asp.net

****************************ASP.NET CODE*********************************

<form id="form1" runat="server">
        <div class="card">
            <table>
                <tr>
                    <td>
                        
                <asp:Label ID="lblTotalCaption" runat="server" Visible="false">Total Salary:</asp:Label>
              
            
                    </td>
                    <td>
                          <asp:Label ID="lblTotal" runat="server"></asp:Label>
                    </td>
                    <td>
                         <asp:Label ID="lblTotalAllownce" runat="server" Visible="false">Total Allownce:</asp:Label>
                    </td>
                    <td>
                        <asp:Label ID="lblTotaAllownce" runat="server"></asp:Label>
                    </td>
                    <td>

                    </td>
                </tr>

            </table>
           
            <br />
            <asp:GridView ID="gvEmpInfo" AutoGenerateColumns="False" CellPadding="4" runat="server"
                BackColor="White" BorderColor="#3366CC" BorderStyle="None" BorderWidth="1px">
                <Columns>
                    <asp:TemplateField>
                        <ItemTemplate>
                            <asp:CheckBox ID="chkbox" runat="server" AutoPostBack="true" OnCheckedChanged="ChckedChanged"/>
                        </ItemTemplate>
                    </asp:TemplateField>
                    <asp:BoundField HeaderText="EmpId" DataField="EmpId" />
                    <asp:BoundField HeaderText="EmpName" DataField="Emp_Name" />
                    <asp:BoundField HeaderText="EmpAddress" DataField="Emp_Address" />
                    <asp:BoundField HeaderText="EmpDept" DataField="Emp_Department" />
                    <%--<asp:BoundField HeaderText="Salary" DataField="Salary" />--%>
                    <asp:TemplateField HeaderText="Salary">
                        <ItemTemplate>
                            <asp:Label ID="lblListPrice" runat="server" Text='<%#Eval("Salary")%>' />
                        </ItemTemplate>
                    </asp:TemplateField>
                     <asp:TemplateField HeaderText="OtherAllownce">
                        <ItemTemplate>
                            <asp:Label ID="lblOtherAllownce" runat="server" Text='<%#Eval("OtherAllownce")%>' />
                        </ItemTemplate>
                    </asp:TemplateField>
                </Columns>
                <FooterStyle BackColor="#99CCCC" ForeColor="#003399" />
                <HeaderStyle BackColor="#003399" Font-Bold="true" ForeColor="#CCCCFF" />
                <PagerStyle BackColor="#99CCCC" ForeColor="#003399" HorizontalAlign="Left" />
                <RowStyle BackColor="White" ForeColor="#003399" />
                <SelectedRowStyle BackColor="#009999" Font-Bold="True" ForeColor="#CCFF99" />
                <SortedAscendingCellStyle BackColor="#EDF6F6" />
                <SortedAscendingHeaderStyle BackColor="#0D4AC4" />
                <SortedDescendingCellStyle BackColor="#D6DFDF" />
                <SortedDescendingHeaderStyle BackColor="#002876" />
            </asp:GridView>
            <br />
            <b>OnCheckedChanged Event Fired on Gridview</b>
            <asp:GridView ID="gvMovedRows" AutoGenerateColumns="False" CellPadding="4" runat="server" ForeColor="#333333" GridLines="None">
                <AlternatingRowStyle BackColor="White" ForeColor="#284775" />
                <Columns>
                    <asp:BoundField HeaderText="EmpId" DataField="EmpId" />
                    <asp:BoundField HeaderText="EmpName" DataField="Emp_Name" />
                    <asp:BoundField HeaderText="EmpAddress" DataField="Emp_Address" />
                    <asp:BoundField HeaderText="EmpDept" DataField="Emp_Department" />
                    <%--<asp:BoundField HeaderText="Salary" DataField="Salary" />--%>
                    <asp:TemplateField HeaderText="Salary">
                        <ItemTemplate>
                            <asp:Label ID="lblListPrice" runat="server" Text='<%#Eval("Salary", "{0:c}")%>' />
                        </ItemTemplate>
                    </asp:TemplateField>
                </Columns>
                <EditRowStyle BackColor="#999999" />
                <FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
                <HeaderStyle BackColor="#5D7B9D" Font-Bold="true" ForeColor="White" />
                <PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" />
                <RowStyle BackColor="#F7F6F3" ForeColor="#333333" />
                <SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" />
                <SortedAscendingCellStyle BackColor="#E9E7E2" />
                <SortedAscendingHeaderStyle BackColor="#506C8C" />
                <SortedDescendingCellStyle BackColor="#FFFDF8" />
                <SortedDescendingHeaderStyle BackColor="#6F8DAE" />
            </asp:GridView>
        </div>

    </form>

*********************************vb.net code**********************************


Imports System
Imports System.Collections.Generic
Imports System.Linq
Imports System.Web
Imports System.Data
Imports System.Data.SqlClient
Imports System.Web.UI
Imports System.Web.UI.WebControls
Imports System.Configuration
Public Class CheckBoxesCalkulations
    Inherits System.Web.UI.Page
    Private con As SqlConnection
    Private adap As SqlDataAdapter
    Private dt As DataTable
    Dim strConnString As String
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Not IsPostBack Then
            BindGridview()
        End If
    End Sub
    Public Sub New()
        strConnString = ConfigurationManager.ConnectionStrings("ConStr").ConnectionString
    End Sub
    Protected Sub BindGridview()
        adap = New SqlDataAdapter("select * from EmployeeInformation", strConnString)
        dt = New DataTable()
        adap.Fill(dt)
        gvEmpInfo.DataSource = dt
        gvEmpInfo.DataBind()
    End Sub
    Protected Sub ChckedChanged(ByVal sender As Object, ByVal e As EventArgs)
        Try
            Dim count As Integer = 0
            Dim totalSalary As Decimal = 0
            Dim totalOther As Decimal = 0
            lblTotal.Text = ""
            lblTotaAllownce.Text = ""
            lblTotalCaption.Visible = False
            lblTotalAllownce.Visible = False
            For Each rw As GridViewRow In gvEmpInfo.Rows
                Dim chkBx As CheckBox = CType(rw.FindControl("chkbox"), CheckBox)

                If chkBx.Checked Then
                    count += 1
                    Dim Salary As Decimal = Convert.ToDecimal((CType(rw.FindControl("lblListPrice"), Label)).Text)
                    Dim OtherAllownce As Decimal = Convert.ToDecimal((CType(rw.FindControl("lblOtherAllownce"), Label)).Text)
                    totalSalary = Convert.ToDecimal((totalSalary + Convert.ToDecimal(Salary)))
                    totalOther = Convert.ToDecimal((totalOther + Convert.ToDecimal(OtherAllownce)))
                    chkBx.Checked = True
                    lblTotal.Text = totalSalary.ToString()
                    lblTotaAllownce.Text = totalOther.ToString
                    lblTotalCaption.Visible = True
                    lblTotalAllownce.Visible = True

                End If
            Next
        Catch ex As Exception

        End Try


        GetSelectedRows()
        BindSecondGrid()
    End Sub
    Protected Sub BindSecondGrid()
        Dim dt As DataTable = CType(ViewState("EmpDetails"), DataTable)
        gvMovedRows.DataSource = dt
        gvMovedRows.DataBind()
    End Sub
    Private Sub GetSelectedRows()
        Dim dt As DataTable

        If ViewState("EmpDetails") IsNot Nothing Then
            dt = CType(ViewState("EmpDetails"), DataTable)
        Else
            dt = CreateTable()
        End If

        For i As Integer = 0 To gvEmpInfo.Rows.Count - 1
            Dim chk As CheckBox = CType(gvEmpInfo.Rows(i).Cells(0).FindControl("chkbox"), CheckBox)

            If chk.Checked Then
                dt = MoveRows(gvEmpInfo.Rows(i), dt)
            Else
                dt = RemoveRow(gvEmpInfo.Rows(i), dt)
            End If
        Next

        ViewState("EmpDetails") = dt
    End Sub
    Private Function CreateTable() As DataTable
        Dim dt As DataTable = New DataTable()
        dt.Columns.Add("EmpId")
        dt.Columns.Add("Emp_Name")
        dt.Columns.Add("Emp_Address")
        dt.Columns.Add("Emp_Department")
        dt.Columns.Add("Salary")
        dt.AcceptChanges()
        Return dt
    End Function
    Private Function MoveRows(ByVal gvRow As GridViewRow, ByVal dt As DataTable) As DataTable
        Dim dr As DataRow() = dt.[Select]("EmpId = '" & gvRow.Cells(1).Text & "'")

        If dr.Length <= 0 Then
            dt.Rows.Add()
            Dim rowscount As Integer = dt.Rows.Count - 1
            dt.Rows(rowscount)("EmpId") = gvRow.Cells(1).Text
            dt.Rows(rowscount)("Emp_Name") = gvRow.Cells(2).Text
            dt.Rows(rowscount)("Emp_Address") = gvRow.Cells(3).Text
            dt.Rows(rowscount)("Emp_Department") = gvRow.Cells(4).Text
            dt.Rows(rowscount)("Salary") = gvRow.Cells(5).Text
            dt.AcceptChanges()
        End If

        Return dt
    End Function
    Private Function RemoveRow(ByVal gvRow As GridViewRow, ByVal dt As DataTable) As DataTable
        Dim dr As DataRow() = dt.[Select]("EmpId = '" & gvRow.Cells(1).Text & "'")
        If dr.Length > 0 Then
            dt.Rows.Remove(dr(0))
            dt.AcceptChanges()
        End If
        Return dt
    End Function

End Class

*************************Table ***********************************

CREATE TABLE [dbo].[EmployeeInformation](
[EmpId] [int] NULL,
[Emp_Name] [varchar](max) NULL,
[Emp_Address] [nvarchar](max) NULL,
[Emp_Department] [varchar](max) NULL,
[Salary] [money] NULL,
[OtherAllownce] [money] NULL

) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]





No comments:

Post a Comment

How to to select duplicate rows from sql server?

 SELECT * FROM Recruitment WHERE Email IN (SELECT Email FROM Recruitment GROUP BY Email HAVING COUNT(*) > 1); WITH CTE AS (     SELECT   ...