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

OnItemDataBound help with Grid

2 Answers 114 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Sunil
Top achievements
Rank 1
Sunil asked on 27 Jul 2010, 08:21 PM
Hi,

I'm trying to have a literal control change its text when a radgrid is bound. The text basically states whether a terminal is offline or online.

Here is the code of the aspx page.

 

<telerik:RadGrid ID="RadGrid_Terminals" runat="server" AutoGenerateColumns="false" 
            AllowMultiRowSelection="false" AllowPaging="True" PageSize="1"
            GridLines="None" CellPadding="0" AllowSorting="true" Skin="Default" OnItemDataBound="Check_Terminal_Status">
                <PagerStyle Mode="NextPrevAndNumeric"/>
                <MasterTableView Width="100%" CommandItemDisplay="Top" GridLines="None" DataKeyNames="Terminal_Auto_ID">
                <CommandItemTemplate>
                    <telerik:RadToolBar ID="RadToolBar1" runat="server" Skin="Default">
                        <Items>
                            <telerik:RadToolBarButton Text="Control Terminal" CommandName="EditSelected" Visible="true"
                             ImageUrl="Images/Edit.gif">
                            </telerik:RadToolBarButton>
                            <telerik:RadToolBarButton Text="Refresh Terminals" CommandName="EditSelected" Visible="true"
                             ImageUrl="Images/Refresh.gif">
                            </telerik:RadToolBarButton>
                        </Items>
                    </telerik:RadToolBar>
                </CommandItemTemplate>
                    <Columns>
                        <telerik:GridClientSelectColumn UniqueName="Checkbox_Terminal"/>
                        <telerik:GridTemplateColumn UniqueName="Terminal_Image_Column" HeaderText="Status">
                            <ItemTemplate>
                                <asp:Literal ID="Literal_Status" runat="server" Text="Online">
                                </asp:Literal>
                            </ItemTemplate>
                        </telerik:GridTemplateColumn>
                        <telerik:GridBoundColumn UniqueName="Terminal_ID_Ref" DataField="Terminal_ID_Ref" HeaderText="Terminal ID"
                         Visible="false"/>
                        <telerik:GridBoundColumn UniqueName="Terminal_Name" DataField="Terminal_Name" HeaderText="Name"
                         SortExpression="Terminal_Name"/>
                        <telerik:GridBoundColumn UniqueName="Terminal_Type_OnTime_Model" DataField="Terminal_Type_OnTime_Model" 
                        HeaderText="Model" SortExpression="Terminal_Type_OnTime_Model"/>
                    </Columns>
                </MasterTableView>
                <ClientSettings>
                    <Selecting AllowRowSelect="true" />
                </ClientSettings>
            </telerik:RadGrid>


The above works fine but its the code behind with the  onitemdatabound event I'm having a problem with.

protected void Check_Terminal_Status(object sender, GridItemEventArgs e)
        {
  
            if (e.Item is GridDataItem)
            {
//What happens here?
            }

What I want to happen is that when the Terminal_Image_Column" control is bound, I decide whether to change the text in the literal Literal_Status to offline. I need to be able to change the text obviously, but also access the respective values such as Terminal_Type_OnTime_Model, Terminal_ID_Ref and Terminal_Auto_ID for example. I need to access this informaiton to perform other methods which will decide whether of not the text Literal_Status needs to be changed.

I really hope someone can point me in the right direction on this! Thank you in advance,

Regards,

Sunny

2 Answers, 1 is accepted

Sort by
0
Accepted
Princy
Top achievements
Rank 2
answered on 28 Jul 2010, 06:42 AM
Hello Sunil,

You can access the column value by using its 'ColumnUniqueName'. Check out the following code snippet.

C#:
protected void Check_Terminal_Status(object sender, GridItemEventArgs e)
   {
      if (e.Item is GridDataItem)
       {
           GridDataItem item = (GridDataItem)e.Item;
           string value = item["Terminal_ID_Ref"].Text; // access the value using its ColumnUniqueName
           if (//your condition)
           {
               Literal lit = (Literal)item.FindControl("Literal_Status");
               lit.Text=  "offline";
           }
       }
   }

Also refer the following documentation which explains how to access cells and rows in a grid.
Accessing cells and rows

Thanks,
Princy.
0
Sunil
Top achievements
Rank 1
answered on 28 Jul 2010, 09:09 AM
I appreciate your help and thank you very much!! :)

Sunny
Tags
Grid
Asked by
Sunil
Top achievements
Rank 1
Answers by
Princy
Top achievements
Rank 2
Sunil
Top achievements
Rank 1
Share this question
or