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

RadGrid

3 Answers 41 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Nithya Rani
Top achievements
Rank 1
Nithya Rani asked on 02 Mar 2012, 12:32 PM
Hi

While i was binding the value to the rad grid. If particular row column have a value. then bind img1 else bind img2.
anyone can help me in this.

Regards,
Nithya

3 Answers, 1 is accepted

Sort by
0
Princy
Top achievements
Rank 2
answered on 02 Mar 2012, 12:50 PM
Hello,

Try the following code.
C#:
protected void grid_ItemDataBound(object sender, GridItemEventArgs e)
{
 if (e.Item is GridDataItem)
 {
  GridDataItem item = (GridDataItem)e.Item;
  TableCell cell = (TableCell)item["UniqueName"];
  if (cell.Text == "text")
  {
   ImageButton img = (ImageButton)item.FindControl("ImageButton1");
   img.ImageUrl = "~/Images/img.gif";
  }
 }
}

Thanks,
Princy.
0
Nithya Rani
Top achievements
Rank 1
answered on 02 Mar 2012, 02:04 PM
Hi Princy,

I am not getting the value.
Bellow is my code.
.aspx
                            <telerik:RadGrid ID="grdInbound" runat="server" AutoGenerateColumns="false" AllowSorting="True"
                                Width="100%" OnItemCommand="grdInbound_ItemCommand1" AllowAutomaticDeletes="True"
                                OnItemCreated="grdInbound_ItemCreated"
                                onitemdatabound="grdInbound_ItemDataBound">
                                <HeaderStyle BorderStyle="Solid" Font-Bold="true" HorizontalAlign="Center" />
                                <ItemStyle HorizontalAlign="Left" Font-Underline="false" />
                                <AlternatingItemStyle HorizontalAlign="Left" Font-Underline="false" />
                                <MasterTableView CommandItemDisplay="Bottom" TableLayout="Fixed" EditMode="InPlace">
                                    <Columns>
  <telerik:GridTemplateColumn HeaderText="Comment" UniqueName="comment"  DataField="COMMENT">
                                            <ItemStyle HorizontalAlign="Center" Width="8%" />
                                            <HeaderStyle Width="8%" />
                                            <ItemTemplate>
                                                <asp:ImageButton ID="imgComment" runat="server" ImageUrl="~/images/No_Comments.jpg"
                                                    Width="20px" Height="20px" CommandArgument='<%# DataBinder.Eval (Container.DataItem, "COMMENT") %>' />
                                                <telerik:RadToolTip runat="server" ID="RadToolTip1" Width="160px" Height="40px" IsClientID="false"
                                                    Visible="false" SkinID="Forest" TargetControlID="imgComment" Text='<%# DataBinder.Eval (Container.DataItem, "COMMENT") %>'
                                                    BorderWidth="0" Animation="Resize" CssClass="TooltipText" Position="TopLeft"
                                                    ShowDelay="0" HideDelay="0">
                                                </telerik:RadToolTip>
                                            </ItemTemplate>
                                        </telerik:GridTemplateColumn>
 </Columns>
  </MasterTableView>
                            </telerik:RadGrid>

.aspx.cs

            if (e.Item is GridDataItem)
            {
                GridDataItem item = (GridDataItem)e.Item;
                TableCell cell = (TableCell)item["comment"];
                if (string.IsNullOrEmpty(cell.Text))
                {
                    ImageButton img = (ImageButton)item.FindControl("imgComment");
                    img.ImageUrl = "~/images/No_Comments.jpg";
                }
                else
                {
                    ImageButton img = (ImageButton)item.FindControl("imgComment");
                    img.ImageUrl = "~/images/Comments.jpg";                    
                }
            }
0
Andrey
Telerik team
answered on 07 Mar 2012, 11:04 AM
Hello,

Based on the declaration of RadGrid you have post I see that you have one template column. And in the ItemTemplae of this column you have defined the ImageButton control. In this case the cell text is blank, because the ItemTemplate replaces the cell content with the control that is defined in it. In this case you need to use not the GridDataItem object but its corresponding DataItem object, namely:

protected void RadGrid1_ItemDataBound(object sender, Telerik.Web.UI.GridItemEventArgs e)
   {
       if (e.Item is GridDataItem)
       {
           GridDataItem item = (GridDataItem)e.Item;                       
           if (string.IsNullOrEmpty(DataBinder.Eval(item.DataItem, "COMMENT").ToString()))
           {
               ImageButton img = (ImageButton)item.FindControl("ImageButton1");
               img.ImageUrl = "~/Images/img.gif";
           }           
           else
           {
               ImageButton img = (ImageButton)item.FindControl("imgComment");
               img.ImageUrl = "~/images/Comments.jpg";
           }
       }
   }

Give this suggestion a try and check whether this is the desired behavior.

Kind regards,
Andrey
the Telerik team
If you want to get updates on new releases, tips and tricks and sneak peeks at our product labs directly from the developers working on the RadControls for ASP.NET AJAX, subscribe to their blog feed now.
Tags
Grid
Asked by
Nithya Rani
Top achievements
Rank 1
Answers by
Princy
Top achievements
Rank 2
Nithya Rani
Top achievements
Rank 1
Andrey
Telerik team
Share this question
or