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

[Solved] RadGrid can't find items after loading them

3 Answers 197 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Hunter
Top achievements
Rank 1
Hunter asked on 30 Jan 2013, 12:19 AM

This was working and somehow I broke it without knowing it.  I populate a grid from the database.  In the code behind I select some of the  Items in the grid (I have a checkbox in the grid item).  Should be easy.

However when I try to loop through the items in the table the count on the MasterTableView.Items is 0.  But when the page displayed the grid has items.

Here's the code (I removed some non-essential pieces):





Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs)
    If Not IsPostBack Then      
        BindScans(Session("ClientID"))
        Dim ScanGroupID As String = Request("ScanGroupID") & ""
 
        If ScanGroupID <> "" Then LoadScanGroup(ScanGroupID)
    End If
 
End Sub
 
Sub BindScans(ByVal ClientID As String)
 
    Dim sqlConn As SqlConnection = New SqlConnection(Session("SCAN_CONN"))
    Dim SQL As String = "Select *  From nxp_sites SITES  WHERE ....."
    Dim adapter As SqlDataAdapter = New SqlDataAdapter(SQL, sqlConn)
    Dim dt As DataTable = New DataTable
 
    adapter.Fill(dt)
    rgScans.DataSource = dt
 
End Sub
 
Sub LoadScanGroup(ByVal ScanGroupID As String)
 
    Dim oScanGroup As clsScanGroup = New clsScanGroup(ScanGroupID)
    If Not oScanGroup.HasError Then
 
        Dim dt As DataTable = oScanGroup.GetScans()
        SelectScanGroupItems(dt)
 
    End If
 
End Sub
 
Sub SelectScanGroupItems(ByVal dt As DataTable)
 
    If dt.Rows.Count > 0 Then
        For Each dr As DataRow In dt.Rows
 
            'At this point rgscan.MasterTableView.Items count is 0, why?
            For Each gdi As GridDataItem In rgScans.MasterTableView.Items
                If gdi("scan_id").Text = dr("ScanID") And gdi("site_id").Text = dr("SiteID") Then
                    Dim chk As CheckBox = DirectCast(gdi("CheckboxSelectColumn").Controls(0), CheckBox)                    
                    gdi.Selected = True
                End If
            Next
        Next
    End If
 
End Sub
And the ASPX:

<telerik:RadGrid ID="rgScans" runat="server" ShowGroupPanel="False" AllowPaging="true"
    Width="700" AllowMultiRowSelection="true" PageSize="25"
    Skin="Office2010Silver" AllowSorting="true"
    AutoGenerateColumns="false" OnNeedDataSource="rgScans_NeedDataSource" >
 
    <MasterTableView AutoGenerateColumns="false" DataKeyNames="site_id"           
                             InsertItemPageIndexAction="ShowItemOnCurrentPage">
        <Columns>
            <telerik:GridClientSelectColumn UniqueName="CheckboxSelectColumn" HeaderStyle-HorizontalAlign="Center"
                ItemStyle-HorizontalAlign="Center" FooterText="CheckBoxSelect footer" ItemStyle-Width="40px"
                HeaderStyle-Width="40px">
            </telerik:GridClientSelectColumn>
            <telerik:GridTemplateColumn HeaderText="Current" ItemStyle-HorizontalAlign="Center"
                ItemStyle-Width="40px" HeaderStyle-Width="40px">
                <ItemTemplate>
                    <asp:image imageurl='<%# GetCurrentScan(Eval("site_id"))%>' runat="server" />
                </ItemTemplate>
            </telerik:GridTemplateColumn>
            <telerik:GridBoundColumn DataField="site_name" ItemStyle-Width="250px" HeaderStyle-Width="250px"
                FilterControlAltText="Filter ScanGroupName column" HeaderText="Scan Name" AllowFiltering="false"
                SortExpression="ScanGroupName" UniqueName="ScanGroupName" AllowSorting="true" />
            <telerik:GridBoundColumn DataField="start_time" HeaderText="Scan Date/Time"
                DataFormatString="{0:MM/dd/yyyy}"
                FilterControlAltText="Filter start_time column" AllowFiltering="false" SortExpression="start_time"
                UniqueName="start_time" AllowSorting="true" />
            <telerik:GridBoundColumn DataField="live_hosts" ItemStyle-Width="100px" HeaderStyle-Width="100px"
                FilterControlAltText="Filter ScanGroupName column" HeaderText="Live Hosts" AllowFiltering="false"
                SortExpression="live_hosts" UniqueName="live_hosts" AllowSorting="true" />
            <telerik:GridBoundColumn DataField="site_id" ItemStyle-Width="100px" HeaderStyle-Width="100px"
                FilterControlAltText="Filter ScanGroupName column" HeaderText="Site ID" Visible="false"
                AllowFiltering="false" SortExpression="site_id" UniqueName="site_id" AllowSorting="true" />
            <telerik:GridBoundColumn DataField="scan_id" ItemStyle-Width="100px" HeaderStyle-Width="100px"
                FilterControlAltText="Filter scan_id column" HeaderText="Scan ID" Visible="false"
                AllowFiltering="false" SortExpression="scan_id" UniqueName="scan_id" AllowSorting="true" />
        </Columns>
    </MasterTableView>
    <ClientSettings>
        <Selecting AllowRowSelect="true"></Selecting>
    </ClientSettings>
</telerik:RadGrid>

Thank you in advance on this.

3 Answers, 1 is accepted

Sort by
0
Princy
Top achievements
Rank 2
answered on 30 Jan 2013, 05:15 AM
Hi,

I guess you want to select a row in the first time when grid loads. Please try the followng code snippet.

C#:
protected void rgScans_PreRender(object sender, EventArgs e)
{
    if (!IsPostBack)
    {
        if (your condition)
        {    
            foreach (GridDataItem gdi in rgScans.MasterTableView.Items)
            {
               if (condition)
               {
                   CheckBox chk = (CheckBox)gdi["CheckboxSelectColumn"].Controls[0];
                   gdi.Selected = true;
               }
            }
        }
    }
}

One advice is to use NeedDataSource event to bind RadGrid. Since you have bound the RadGrid inside !(IsPostBack) condition, the grid will vanish after a postback.

Please elaborate your scenario if it doesn't help

Regards,
Princy.
0
Jayesh Goyani
Top achievements
Rank 2
answered on 30 Jan 2013, 05:20 AM
Hello,

Please Access Radgrid's Item In Radgrid_Prerender event or in Page_prerender event.

You can able to access Radgrid's Item after Item_cretaed event excuted.

http://www.telerik.com/help/aspnet-ajax/grid-event-sequence.html
http://www.telerik.com/help/aspnet-ajax/events_t_telerik_web_ui_radgrid.html

Thanks,
Jayesh Goyani
0
Hunter
Top achievements
Rank 1
answered on 30 Jan 2013, 03:41 PM

Thanks for the reply.

I made this work by replacing the call to the BindScans routine with a call the the rebind routine and it worked just fine.  Is there a reason why I shouldn't do this?  If so, I'll try the pre-render approach.
Tags
Grid
Asked by
Hunter
Top achievements
Rank 1
Answers by
Princy
Top achievements
Rank 2
Jayesh Goyani
Top achievements
Rank 2
Hunter
Top achievements
Rank 1
Share this question
or