Link to home
Create AccountLog in
Avatar of akulavi
akulavi

asked on

Gridview Problem

My aspx page is having a grid with three fields(Inst Name,CourseName,Activate Date) and two Buttons ( Modify,Delete). When User clicks on Modify bttn he will shifted to another page with all the Courses data and Clicking on Delete Button it will delete the Course. the gridview is also have the following properties AllowSorting="True" AllowPaging="True".
MY PROBLEM IS ::
IF I OMIT THE ELSE PORTION IN THE PAGE_LOAD EVENT MODIFY AND DELETE IS WORKING FINE, BUT SORTING AND PAGING IS NOT WORKING.
IF I DONT OMIT THE ELSE PORTION THEN SORTING AND PAGING IS WORKING FINE BUT DELETE AND MODIFY IS NOT WORKING.

WHAT TO DO ?????/
I am attaching the aspx.vb file in the AttachCode snippet
In my aspx page GRIDVIEW is defined as below :

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" CellPadding="4" DataKeyNames="CourseId" ForeColor="#333333" OnRowCommand="GridView1_RowCommand"  OnSorting="gridView1_Sorting" OnRowDataBound="GridView1_RowDataBound" OnRowDeleted="GridView1_RowDeleted" OnRowDeleting="GridView1_RowDeleting" Width="872px" AllowPaging="true" AllowSorting="true" >
       
        <FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
        <Columns>
            <asp:BoundField DataField="InstituteId" HeaderText="InstituteId" SortExpression="InstituteId" Visible="False" />
            <asp:BoundField DataField="InstituteName" HeaderText="INSTITUTE NAME" SortExpression="InstituteName" />
            <asp:BoundField DataField="CourseId" HeaderText="CourseId" ReadOnly="True" SortExpression="CourseId" Visible="False" />
            <asp:BoundField DataField="CourseName" HeaderText="COURSE NAME" SortExpression="CourseName" />
            <asp:BoundField DataField="dtActive" HeaderText="ACTIVATED TILL" SortExpression="dtActive" DataFormatString="{0:MMM dd, yy}" HtmlEncode="false"/>
            <asp:TemplateField>
                <ItemTemplate>
                    <asp:Button ID="L3" CommandArgument='<%# Eval("CourseId") %>' CommandName="Modify" runat="server" Text="Modify"/>
                </ItemTemplate>
            </asp:TemplateField>
            <asp:TemplateField>
                <ItemTemplate>
                    <asp:Button id="L1" CommandArgument='<%# Eval("CourseId") %>' CommandName="Delete" runat="server" Text="Delete"/>
                </ItemTemplate>
            </asp:TemplateField>
         </Columns>
        <RowStyle BackColor="#F7F6F3" ForeColor="#333333" />
        <EditRowStyle BackColor="#999999" />
        <SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" />
        <PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" />
        <HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
        <AlternatingRowStyle BackColor="White" ForeColor="#284775" />
    </asp:GridView>
Imports System.Data
Imports System.Data.SqlClient
Partial Class InstituteCourseList
    Inherits System.Web.UI.Page
    Public Sub gridData(ByVal filter As String)
        Dim str As String
        Dim olcmd As SqlCommand
        Dim connstring As String
        connstring = ConfigurationSettings.AppSettings("ConnectionString")
        Dim cn As New SqlConnection(connstring)
        cn.Open()
        If filter = "" Or filter = "ALL" Then
            str = "SELECT Course_Master.InstituteId, Institute_Master.InstituteName, Course_Master.CourseId, Course_Master.CourseName, Course_Master.dtActive FROM Institute_Master INNER JOIN Course_Master ON Institute_Master.InstituteId = Course_Master.InstituteId ORDER BY Institute_Master.InstituteName"
            olcmd = New SqlCommand(str, cn)
            Dim dtr As New SqlDataAdapter(olcmd)
            Dim dst As New DataSet
            dst.Clear()
            dtr.Fill(dst, "POLO")
            GridView1.DataSource = dst.Tables(0)
            GridView1.DataBind()
        ElseIf filter = "ACTIVATED COURSES" Then
            str = "SELECT Course_Master.InstituteId, Institute_Master.InstituteName, Course_Master.CourseId, Course_Master.CourseName, Course_Master.dtActive FROM Institute_Master INNER JOIN Course_Master ON Institute_Master.InstituteId = Course_Master.InstituteId WHERE Course_Master.dtActive >='" & Now & "' ORDER BY Institute_Master.InstituteName"
            olcmd = New SqlCommand(str, cn)
            Dim dtr As New SqlDataAdapter(olcmd)
            Dim dst As New DataSet
            dst.Clear()
            dtr.Fill(dst, "POLO")
            GridView1.DataSource = dst.Tables(0)
            GridView1.DataBind()
        ElseIf filter = "DE-ACTIVATED COURSES" Then
            str = "SELECT Course_Master.InstituteId, Institute_Master.InstituteName, Course_Master.CourseId, Course_Master.CourseName, Course_Master.dtActive FROM Institute_Master INNER JOIN Course_Master ON Institute_Master.InstituteId = Course_Master.InstituteId WHERE Course_Master.dtActive <='" & Now & "' ORDER BY Institute_Master.InstituteName"
            olcmd = New SqlCommand(str, cn)
            Dim dtr As New SqlDataAdapter(olcmd)
            Dim dst As New DataSet
            dst.Clear()
            dtr.Fill(dst, "POLO")
            GridView1.DataSource = dst.Tables(0)
            GridView1.DataBind()
        End If
        cn.Close()
    End Sub
    Private Property GridViewSortDirection() As String
        Get
            Return IIf(ViewState("SortDirection") = Nothing, "ASC", ViewState("SortDirection"))
        End Get
        Set(ByVal value As String)
            ViewState("SortDirection") = value
        End Set
    End Property
 
    Private Property GridViewSortExpression() As String
        Get
            Return IIf(ViewState("SortExpression") = Nothing, String.Empty, ViewState("SortExpression"))
        End Get
        Set(ByVal value As String)
            ViewState("SortExpression") = value
        End Set
    End Property
 
    Private Function GetSortDirection() As String
        Select Case GridViewSortDirection
            Case "ASC"
                GridViewSortDirection = "DESC"
            Case "DESC"
                GridViewSortDirection = "ASC"
        End Select
        Return GridViewSortDirection
    End Function
 
    Protected Function SortDataTable(ByVal dataTable As DataTable, ByVal isPageIndexChanging As Boolean) As DataView
        If Not dataTable Is Nothing Then
            Dim dataView As New DataView(dataTable)
            If GridViewSortExpression <> String.Empty Then
                If isPageIndexChanging Then
                    dataView.Sort = String.Format("{0} {1}", GridViewSortExpression, GridViewSortDirection)
                Else
                    dataView.Sort = String.Format("{0} {1}", GridViewSortExpression, GetSortDirection())
                End If
            End If
            Return dataView
        Else
            Return New DataView()
        End If
    End Function
 
    Protected Sub GridView1_Sorting(ByVal sender As Object, ByVal e As GridViewSortEventArgs)
        GridViewSortExpression = e.SortExpression
        Dim pageIndex As Integer = GridView1.PageIndex
        GridView1.DataSource = SortDataTable(GridView1.DataSource, False)
        'gridData(DropDownList1.SelectedValue)
        GridView1.DataBind()
        GridView1.PageIndex = pageIndex
    End Sub
    Public Sub DeleteRecordByID(ByVal CourId As String)
        Dim str As String
        Dim olcmd As SqlCommand
        Dim connstring As String
        connstring = ConfigurationSettings.AppSettings("ConnectionString")
        Dim cn As New SqlConnection(connstring)
        cn.Open()
        str = "Delete From Course_Master where CourseId='" & CourId & "'"
        olcmd = New SqlCommand(str, cn)
        olcmd.ExecuteScalar()
        cn.Close()
        gridData(DropDownList1.SelectedValue)
    End Sub
    Public Sub modifyRecordbyId(ByVal CourId As String)
        Response.Redirect("Control_PanelModify.aspx?CourseIdReq=" & CourId) ' & "&amp;InstituteIdReq=" & InstId)
    End Sub
 
    Protected Sub GridView1_RowCommand(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewCommandEventArgs) Handles GridView1.RowCommand
        If e.CommandName = "Delete" Then
            Dim Cid As String = e.CommandArgument
            DeleteRecordByID(Cid)
        ElseIf e.CommandName = "Modify" Then
            Dim Cid As String = e.CommandArgument
            modifyRecordbyId(Cid)
        End If
    End Sub
 
    Protected Sub GridView1_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles GridView1.RowDataBound
        If e.Row.RowType = DataControlRowType.DataRow Then
            Dim l As New Button
            l = CType(e.Row.FindControl("L1"), Button)
            l.Attributes.Add("onclick", "javascript:return confirm('Are you sure you want to delete this record " & DataBinder.Eval(e.Row.DataItem, "CourseName") & "')")
        End If
    End Sub
 
    Protected Sub GridView1_RowDeleted(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewDeletedEventArgs) Handles GridView1.RowDeleted
 
    End Sub
 
    Protected Sub GridView1_RowDeleting(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewDeleteEventArgs) Handles GridView1.RowDeleting
 
    End Sub
 
    Protected Sub GridView1_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles GridView1.SelectedIndexChanged
 
    End Sub
 
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Not Page.IsPostBack Then
            DropDownList1.Items.Add("ALL")
            DropDownList1.Items.Add("ACTIVATED COURSES")
            DropDownList1.Items.Add("DE-ACTIVATED COURSES")
            gridData(DropDownList1.SelectedValue)
        Else
            gridData(DropDownList1.SelectedValue)
        End If
    End Sub

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of SystemExpert
SystemExpert
Flag of United States of America image

Link to home
membership
Create an account to see this answer
Signing up is free. No credit card required.
Create Account