This is a migrated thread and some comments may be shown as answers.

[Solved] Grid disappear after filter

2 Answers 169 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Joe
Top achievements
Rank 1
Joe asked on 27 Aug 2013, 11:25 AM

my application will need user to click submit button to retrieve data from code behind and then display the grid,

it works fine that user to select start end, end date and checkbox and then click submit.

but it had error when user using the grid column header filter function, the grid will disappear. user need to click the submit button again to show the result and grid.

how can i fix the problem ? thanks

<%@ Page Language="VB" AutoEventWireup="false" CodeFile="HistoricalStaffs.aspx.vb" Inherits="Reports_HistoricalStaff" %>
<%@ Register Assembly="Telerik.Web.UI" Namespace="Telerik.Web.UI" TagPrefix="telerik" %><%@ Register Assembly="Telerik.Web.UI" Namespace="Telerik.Web.UI" TagPrefix="telerik" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<head runat="server">
  
        <script type="text/javascript">
            function viewpos(sid) {
                wid = 1000
                hei = 900
                x = (screen.availWidth - wid) / 2
                y = (screen.availHeight - hei) / 2
                window.open('../EmployeeDetails/EmployeeDetails.aspx?SID=' + String(sid), 'StaffDetails', 'scrollbars=no,resizable=yes,width=' + String(wid) + ',height=' + String(hei) + ',left=' + x + ',top=' + y);                
            }
    </script>
</head>
  
<body class="pagebackground">
    <form id="form1" runat="server">
    <telerik:RadScriptManager ID="RadScriptManager1" runat="server">
    </telerik:RadScriptManager>
    <asp:Panel ID="Panel2" runat="server" Width="1185px" BackColor="#86A3C3">
        <asp:Label ID="LabelTitle" ForeColor="White" Font-Bold="true" Font-Size="14pt" runat="server"
            Text="Historical Staff"></asp:Label>
    </asp:Panel><div id="tohide">
    <telerik:RadToolBar ID="RadToolBar1" runat="server" Width="1185px">
        <Items>
            <telerik:RadToolBarButton Value="Export" Text="Export" Width="80px">
            </telerik:RadToolBarButton>
            <telerik:RadToolBarButton IsSeparator="true">
            </telerik:RadToolBarButton>
            <telerik:RadToolBarButton Value="Print" Text="Print" Width="80px" NavigateUrl="javascript:PrintRadGrid();">
            </telerik:RadToolBarButton>
            <telerik:RadToolBarButton IsSeparator="true">
            </telerik:RadToolBarButton>
            <telerik:RadToolBarButton Value="Close" Text="Close" NavigateUrl="javascript:window.close()" Width="80px">
            </telerik:RadToolBarButton>
        </Items>
    </telerik:RadToolBar></div>
  <table border="0" style="margin:10px">
  
    <tr>
        <td style="padding-top:5px">From: <telerik:RadDatePicker ID="dfFrom" runat="server" DateInput-DateFormat="dd-MMM-yyyy" Width="120px"></telerik:RadDatePicker>                     To: <telerik:RadDatePicker ID="dfTo" runat="server" DateInput-DateFormat="dd-MMM-yyyy" Width="120px"></telerik:RadDatePicker>   <asp:Button ID="btnSubmit" runat="server" Text="Submit" Visible="true" /></td>
    </tr>
        <tr>
    <td style="padding-top:5px">Staff Type:
        <asp:CheckBoxList ID="CheckBoxList_StaffType" runat="server"  
                                                        DataSourceID="LDS_StaffType" DataTextField="StaffType" AutoPostBack="True" 
                                                        DataValueField="StaffTypeID"  
                                                         OnDataBound="CheckBoxList_StaffType_DataBound" RepeatColumns="4" 
                                                        RepeatDirection="Horizontal" >
                                                    </asp:CheckBoxList>
    </td>
    </tr>
    </table>
    <asp:Table ID="Table4" runat="server">
        <asp:TableRow>
            <asp:TableCell>
                <telerik:RadGrid ID="RadGrid1" runat="server" AutoGenerateColumns="False" 
                    GridLines="None" AllowSorting="True" AllowFilteringByColumn="True" ShowGroupPanel=true 
                    AllowPaging="True" PageSize="300" EnableLinqExpressions="true">
                    <PagerStyle Position="TopAndBottom" />
                                        <GroupingSettings CaseSensitive="false" /> 
                    <ClientSettings EnablePostBackOnRowClick="true" AllowColumnsReorder="false" AllowDragToGroup="false" ReorderColumnsOnClient="false">
                        <Selecting AllowRowSelect="True" />             
                    </ClientSettings
  
                    <MasterTableView  DataKeyNames="SID">
                        <CommandItemSettings ExportToPdfText="Export to Pdf"></CommandItemSettings>
                        <RowIndicatorColumn>
                            <HeaderStyle Width="10px"></HeaderStyle>
                        </RowIndicatorColumn>
                        <ExpandCollapseColumn>
                            <HeaderStyle Width="10px"></HeaderStyle>
                        </ExpandCollapseColumn>
                        <Columns>
                            <telerik:GridBoundColumn DataField="SID" HeaderText="#" ReadOnly="True"
                            SortExpression="SID" UniqueName="SID" visible="false">
                            </telerik:GridBoundColumn>
                              
                            <telerik:GridBoundColumn DataField="DisplayName" HeaderText="Staff Name" ReadOnly="True"
                            SortExpression="DisplayName" UniqueName="DisplayName" ItemStyle-Width="300px">
                            </telerik:GridBoundColumn>
                            <telerik:GridBoundColumn DataField="JobTitle" HeaderText="Job Title" ReadOnly="True"
                                SortExpression="JobTitle" UniqueName="JobTitle" ItemStyle-Width="300px">
                            </telerik:GridBoundColumn>
                            <telerik:GridBoundColumn DataField="StartDate" HeaderText="Start Date" ReadOnly="True"
                                SortExpression="StartDate" UniqueName="StartDate" DataType="System.DateTime" DataFormatString="{0:dd-MMM-yyyy}" ItemStyle-Width="80px">
                        </telerik:GridBoundColumn>
                            <telerik:GridBoundColumn DataField="EndDate" HeaderText="End Date" ReadOnly="True"
                                SortExpression="EndDate" UniqueName="EndDate" DataType="System.DateTime" DataFormatString="{0:dd-MMM-yyyy}" ItemStyle-Width="80px">
                        </telerik:GridBoundColumn>
                          
                        </Columns>
                    </MasterTableView>
                </telerik:RadGrid>
                <br />
                <br />
            </asp:TableCell>
        </asp:TableRow>
    </asp:Table>
  
    <asp:LinqDataSource ID="LDS_StaffType" runat="server" ContextTypeName="dcHRISDataContext"
        OrderBy="StaffType" Select="new (StaffTypeID, StaffType)" TableName="HRIS_Dictionary_StaffTypes" >
    </asp:LinqDataSource>
  
    </form>
</body>
<script type="text/javascript">
    function PrintRadGrid() {
        var previewWnd = window.open('about:blank', '', '', false);
        var sh = '/CompetentPerson/WebResource.axd?d=yI7Ak8XjwPrTb1pWAk6U_13Yzi9iuY7-jabBST0g4uB2ZEwP5lPLe0BUdPNxCLmqIHhJoPKhT0gJMHUmCz-N0w2&t=634183400620000000';
        var styleStr = "<html><head><link href = '" + sh + "' rel='stylesheet' type='text/css'></link></head>";
        styleStr = styleStr + '<style>.rgFilterRow {display:none !important}</style>'
        styleStr = styleStr + '<style>.rgGroupPanel {display:none !important}</style>'
        var htmlcontent = styleStr + "<body>" + $find('RadGrid1').get_element().outerHTML + "</body></html>";
        previewWnd.document.open();
        previewWnd.document.write(htmlcontent);
        previewWnd.document.close();
        previewWnd.print();
        previewWnd.close();
    }
</script>
</html>


Code behind:

Partial Class Reports_HistoricalStaff
    Inherits System.Web.UI.Page
    Dim theRow As Integer = 0
  
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Dim dc As New dcHRISDataContext
  
        Dim rec = (From a In dc.vHRIS_Departments Where a.DID = Request("DID")).firstordefault
  
        If Not isnothing(rec) Then
            LabelTitle.text = "Historical Staff (" & "Department - " & rec.Department & ")"
        End If
  
        If Not Page.IsPostBack Then
            dfFrom.SelectedDate = DateTime.Now
            dfTo.SelectedDate = DateTime.Now
        End If
  
        If Request("SID") <> "" Then
            Session("SID") = Request("SID")
        End If
  
    End Sub
  
  
    Protected Sub CheckBoxList_StaffType_DataBound(ByVal sender As Object, ByVal e As System.EventArgs) Handles CheckBoxList_StaffType.DataBound
        Dim dc As New dcHRISDataContext
        Dim chkboxlb As CheckBoxList
        chkboxlb = DirectCast(FindControl("CheckBoxList_StaffType"), CheckBoxList)
  
        Dim results = (From a In dc.HRIS_Dictionaries Where a.DictTypeID = "70").ToList
        Dim chkstr As String = ""
        For Each item In results
            For Each cb As ListItem In CheckBoxList_StaffType.Items
                If Trim(item.DictData) = (cb.Value) Then
                    'If Trim(item.ToString).Contains("YES") Then
                    cb.Selected = True
                    'End If
                End If
            Next
        Next
  
    End Sub
  
    Protected Sub btnSubmit_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnSubmit.Click
        Dim dc As New dcHRISDataContext
  
        'get Staff Type of checkbox
        Dim strStaffType As String = ""
        For i = 0 To CheckBoxList_StaffType.Items.Count - 1
            If CheckBoxList_StaffType.Items(i).Selected = True Then
                strStaffType += CheckBoxList_StaffType.Items(i).Value.ToString & ","
            End If
        Next
        If Left(strStaffType, 1) = "," Then
            strStaffType = Right(strStaffType, Len(strStaffType) - 1)
        End If
        If Right(strStaffType, 1) = "," Then
            strStaffType = Left(strStaffType, Len(strStaffType) - 1)
        End If
        Dim rList = Split(strStaffType, ",")
  
        Dim rec = From a In dc.HRIS_Staff_Movements _
                  Group Join b In dc.StaffDBs On a.sid _
                  Equals b.sid Into subGrp = Group _
                  From b In subGrp.DefaultIfEmpty _
                  Where rList.Contains(a.StaffType) _
                  And a.EndDate >= CType(dfFrom.SelectedDate, DateTime) And a.EndDate < CType(dfTo.SelectedDate, DateTime).AddDays(1) _
                  And a.ProjectDepartment = Request("DID") _
                  Order By b.DisplayName, a.StartDate, a.EndDate _
                  Select New With {b.DisplayName, a.JobTitle, a.StartDate, a.EndDate, a.SID, b.Lefe}
  
        RadGrid1.DataSource = rec
        RadGrid1.DataBind()
  
    End Sub
  
    Protected Sub RadToolBar1_ButtonClick(ByVal sender As Object, ByVal e As Telerik.Web.UI.RadToolBarEventArgs) Handles RadToolBar1.ButtonClick
        If e.Item.Value = "Export" Then
            RadGrid1.ExportSettings.ExportOnlyData = True
            RadGrid1.ExportSettings.IgnorePaging = True
            RadGrid1.MasterTableView.ExportToExcel()
        End If
    End Sub
  
  
    Protected Sub RadGrid1_ItemDataBound(ByVal sender As Object, ByVal e As Telerik.Web.UI.GridItemEventArgs) Handles RadGrid1.ItemDataBound
        If (TypeOf (e.Item) Is GridDataItem) Then
  
            e.Item.Cells(3).Text = "<font style='cursor:hand;color:blue' onclick='viewpos(" & e.Item.DataItem.SID & ");'><B>" & e.Item.Cells(3).Text & "</B></font>"
        End If
  
    End Sub
  
End Class

2 Answers, 1 is accepted

Sort by
0
Accepted
Jayesh Goyani
Top achievements
Rank 2
answered on 27 Aug 2013, 11:36 AM
Hello,

<telerik:RadGrid ID="RadGrid1" OnNeedDataSource="RadGrid1_NeedDataSource" runat="server" AutoGenerateColumns="False"
                    GridLines="None" AllowSorting="True" AllowFilteringByColumn="True" ShowGroupPanel=true
                    AllowPaging="True" PageSize="300" EnableLinqExpressions="true">
                    <PagerStyle Position="TopAndBottom" />
                                        <GroupingSettings CaseSensitive="false" />
                    <ClientSettings EnablePostBackOnRowClick="true" AllowColumnsReorder="false" AllowDragToGroup="false" ReorderColumnsOnClient="false">
                        <Selecting AllowRowSelect="True" />            
                    </ClientSettings>
   
                    <MasterTableView  DataKeyNames="SID">
                        <CommandItemSettings ExportToPdfText="Export to Pdf"></CommandItemSettings>
                        <RowIndicatorColumn>
                            <HeaderStyle Width="10px"></HeaderStyle>
                        </RowIndicatorColumn>
                        <ExpandCollapseColumn>
                            <HeaderStyle Width="10px"></HeaderStyle>
                        </ExpandCollapseColumn>
                        <Columns>
                            <telerik:GridBoundColumn DataField="SID" HeaderText="#" ReadOnly="True"
                            SortExpression="SID" UniqueName="SID" visible="false">
                            </telerik:GridBoundColumn>
                               
                            <telerik:GridBoundColumn DataField="DisplayName" HeaderText="Staff Name" ReadOnly="True"
                            SortExpression="DisplayName" UniqueName="DisplayName" ItemStyle-Width="300px">
                            </telerik:GridBoundColumn>
                            <telerik:GridBoundColumn DataField="JobTitle" HeaderText="Job Title" ReadOnly="True"
                                SortExpression="JobTitle" UniqueName="JobTitle" ItemStyle-Width="300px">
                            </telerik:GridBoundColumn>
                            <telerik:GridBoundColumn DataField="StartDate" HeaderText="Start Date" ReadOnly="True"
                                SortExpression="StartDate" UniqueName="StartDate" DataType="System.DateTime" DataFormatString="{0:dd-MMM-yyyy}" ItemStyle-Width="80px">
                        </telerik:GridBoundColumn>
                            <telerik:GridBoundColumn DataField="EndDate" HeaderText="End Date" ReadOnly="True"
                                SortExpression="EndDate" UniqueName="EndDate" DataType="System.DateTime" DataFormatString="{0:dd-MMM-yyyy}" ItemStyle-Width="80px">
                        </telerik:GridBoundColumn>
                           
                        </Columns>
                    </MasterTableView>
                </telerik:RadGrid>


Public Property IsNeedtoshowGrid() As Boolean
    Get
        If ViewState("IsNeedtoshowGrid") Is Nothing Then
            Return False
        Else
            Return Convert.ToBoolean(ViewState("IsNeedtoshowGrid"))
        End If
    End Get
    Set
        ViewState("IsNeedtoshowGrid") = value
    End Set
End Property


Protected Sub btnSubmit_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnSubmit.Click
           IsNeedtoshowGrid = true;
        RadGrid1.ReBind()
    
    End Sub
Protected Sub RadGrid1_NeedDataSource(sender As Object, e As GridNeedDataSourceEventArgs
If IsNeedtoshowGrid = True Then
        Dim dc As New dcHRISDataContext
    
        'get Staff Type of checkbox
        Dim strStaffType As String = ""
        For i = 0 To CheckBoxList_StaffType.Items.Count - 1
            If CheckBoxList_StaffType.Items(i).Selected = True Then
                strStaffType += CheckBoxList_StaffType.Items(i).Value.ToString & ","
            End If
        Next
        If Left(strStaffType, 1) = "," Then
            strStaffType = Right(strStaffType, Len(strStaffType) - 1)
        End If
        If Right(strStaffType, 1) = "," Then
            strStaffType = Left(strStaffType, Len(strStaffType) - 1)
        End If
        Dim rList = Split(strStaffType, ",")
    
        Dim rec = From a In dc.HRIS_Staff_Movements _
                  Group Join b In dc.StaffDBs On a.sid _
                  Equals b.sid Into subGrp = Group _
                  From b In subGrp.DefaultIfEmpty _
                  Where rList.Contains(a.StaffType) _
                  And a.EndDate >= CType(dfFrom.SelectedDate, DateTime) And a.EndDate < CType(dfTo.SelectedDate, DateTime).AddDays(1) _
                  And a.ProjectDepartment = Request("DID") _
                  Order By b.DisplayName, a.StartDate, a.EndDate _
                  Select New With {b.DisplayName, a.JobTitle, a.StartDate, a.EndDate, a.SID, b.Lefe}
    
        RadGrid1.DataSource = rec
   End If
    End Sub


Please used Grid - Advanced Data Binding
why filtering worked in advance data binding : Commands that invoke Rebind() implicitly

Thanks,
Jayesh Goyani
0
Joe
Top achievements
Rank 1
answered on 28 Aug 2013, 12:46 AM
thanks a lot.

it solve my problem.
Tags
Grid
Asked by
Joe
Top achievements
Rank 1
Answers by
Jayesh Goyani
Top achievements
Rank 2
Joe
Top achievements
Rank 1
Share this question
or