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

GridImageColumn DataBinding Code

1 Answer 449 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Karthi
Top achievements
Rank 1
Karthi asked on 04 Mar 2011, 10:26 AM

I am using GridImageColumn for displaying an icon if the value in a table column (HasRead) which is bound to the RadGrid Control is false.

Here is the aspx code..

<Telerik:RadGrid ID="TelerikThread" Width="97%" AllowSorting="True" PageSize="15" 
    OnItemDataBound="TelerikThread_ItemDataBound" AllowPaging="True" 
    AllowMultiRowSelection="True" runat="server" Gridlines="None"
<MasterTableView Width="100%" Summary="RadGrid table"   AutoGenerateColumns="false"
<PagerStyle Mode="NextPrevAndNumeric" /> 
<Columns
<Telerik:GridImageColumn UniqueName="GridImageColumn" SortExpression="HasRead" 
    HeaderText="Unread" DataImageUrlFields="HasRead"
</Telerik:GridImageColumn>

Below is the code behind

      protected void TelerikThread_ItemDataBound(object sender, GridItemEventArgs e)
      {
          if (e.Item is GridDataItem)
          {
              GridDataItem item = (GridDataItem)e.Item;
              TableCell flag = (TableCell)item["HasRead"];
              if (flag.Text == "false")
              {
                  System.Web.UI.WebControls.Image img = (System.Web.UI.WebControls.Image)item["GridImageColumn"].Controls[0];
                  img.ImageUrl = "./web/Themes/default/images/post_status_new_ln.gif";//set image url
              }
              else
              {
                  TableCell cell = (TableCell)item["GridImageColumn"];
                  cell.Text = " ";//clears image column
              }
          }
}

I'm getting exception in the line GridDataItem item = (GridDataItem)e.Item;
"Cannot cast e.item to GridDataItem"

Please help me resolving this..

Thanks in advance.

1 Answer, 1 is accepted

Sort by
0
Princy
Top achievements
Rank 2
answered on 04 Mar 2011, 12:19 PM
Hello Karthi,

I am not quite sure about your issue and I assume that the field HasRead is not there in Grid and you want to get it. If that the case try this approach.
C#:
protected void rad_ItemDataBound(object sender, GridItemEventArgs e)
{
    if (e.Item is GridDataItem)
    {
        GridDataItem item = (GridDataItem)e.Item;
        DataRowView rowview = (DataRowView)item.DataItem;
        string flag =rowview["HasRead"].ToString();
        if (flag == "True")
        {
            System.Web.UI.WebControls.Image img = (System.Web.UI.WebControls.Image)item["GridImageColumn"].Controls[0];
            img.ImageUrl = "./web/Themes/default/images/post_status_new_ln.gif";//set image url
        }


Thanks,
Princy
Tags
Grid
Asked by
Karthi
Top achievements
Rank 1
Answers by
Princy
Top achievements
Rank 2
Share this question
or