protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
//access_layerDataContext db=new access_layerDataContext();
ques_tbl t1= new ques_tbl();
if (e.Row.DataItem == "qAnswered")
{
Image img = (Image)e.Row.FindControl("image1");
if (t1.qanswered == true)
{
img.ImageUrl = "/images/question_mark.jpg";
}
}
}
Is my code with simple asp.net Grid
now for telerik I wrote the following but its not helping me .
protected void RadGrid1_ItemDataBound(object sender, Telerik.Web.UI.GridItemEventArgs e)
{
if (e.Item.Cells[1].Text== "True")
{
Image img = (Image)e.Item.FindControl("Image1");
img.ImageUrl = "/images/question_mark.jpg";
}
}
and I am getting this error ,at Line 55 the index is out of Range .
Line 53: {
Line 54: ques_tbl t1 = new ques_tbl();
Line 55: if (e.Item.Cells[1].Text== "True")
Line 56: {
Line 57: Image img = (Image)e.Item.FindControl("Image1");
11 Answers, 1 is accepted
0
Accepted

Shinu
Top achievements
Rank 2
answered on 14 Nov 2011, 08:08 AM
Hello Usman,
I suppose you want to access Image based on some conditions. Try the following code snippet to achieve your scenario.
C#:
Also check the following help documentation.
Accessing Cells and Rows
-Shinu.
I suppose you want to access Image based on some conditions. Try the following code snippet to achieve your scenario.
C#:
protected
void
grid_ItemDataBound(
object
sender, Telerik.Web.UI.GridItemEventArgs e)
{
if
(e.Item
is
GridDataItem)
{
GridDataItem dataitem = (GridDataItem)e.Item;
TableCell cell = (TableCell)dataitem[
"Uniquename"
];
if
(cell.Text ==
"qAnswered"
)
{
Image img = (Image)dataitem.FindControl(
"image1"
);
}
}
}
Also check the following help documentation.
Accessing Cells and Rows
-Shinu.
0

usman
Top achievements
Rank 1
answered on 14 Nov 2011, 09:12 AM
thanks for reply
Kindly tell what is GridDataItem ? what is its type ?
Kindly tell what is GridDataItem ? what is its type ?
0
Accepted

Jayesh Goyani
Top achievements
Rank 2
answered on 14 Nov 2011, 09:18 AM
Hello usman,
Each row is represented by a GridDataItem in telerik Radgrid for normal mode.
for more info check this link.
Thanks,
Jayesh Goyani
Each row is represented by a GridDataItem in telerik Radgrid for normal mode.
for more info check this link.
Thanks,
Jayesh Goyani
0

usman
Top achievements
Rank 1
answered on 14 Nov 2011, 09:26 AM
thanks , problem solved
hats off to Telerik Community
hats off to Telerik Community
0

usman
Top achievements
Rank 1
answered on 14 Nov 2011, 09:54 AM
There is one small issue left , my grid was autoupdating by AJAX before including the above code , but now its not working I mean now its not updating the grid after my timer expires
0

Jayesh Goyani
Top achievements
Rank 2
answered on 14 Nov 2011, 10:05 AM
Hello usman,
Please provide your code so i can check it.
Thanks,
Jayesh Goyani
Please provide your code so i can check it.
Thanks,
Jayesh Goyani
0

usman
Top achievements
Rank 1
answered on 14 Nov 2011, 12:46 PM
public partial class questions : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
BindGrid();
}
protected void Timer1_Tick(object sender, EventArgs e)
{
//Label1.Text = "Grid Refreshed at: " + DateTime.Now.ToLongTimeString();
}
public void BindGrid()
{
//NorthWindDataContext db = new NorthWindDataContext();
//var products = from p in db.Products select new { p.CategoryID, p.Discontinued };
//GridView1.DataSource = products;
//GridView1.DataBind();
access_layerDataContext db = new access_layerDataContext();
var query = from row in db.ques_tbls select row;
ques_tbl t1 = new ques_tbl();
if (t1.qanswered == true)
{ t1.qDetail = "answer wala ry"; }
else { t1.qDetail = "question wala ry"; }
db.SubmitChanges();
this.RadGrid1.DataSource = query;
this.RadGrid1.DataBind();
}
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
//access_layerDataContext db=new access_layerDataContext();
ques_tbl t1= new ques_tbl();
if (e.Row.DataItem == "qAnswered")
{
Image img = (Image)e.Row.FindControl("image1");
if (t1.qanswered == true)
{
img.ImageUrl = "/images/question_mark.jpg";
}
}
}
protected void RadGrid1_ItemDataBound(object sender, Telerik.Web.UI.GridItemEventArgs e)
{
//ques_tbl t1 = new ques_tbl();
//if (e.Item.Cells[1].Text== "True")
//{
// Image img = (Image)e.Item.FindControl("Image1");
// img.ImageUrl = "/images/question_mark.jpg";
//}
if (e.Item is GridDataItem)
{
GridDataItem dataitem = (GridDataItem)e.Item;
TableCell cell = (TableCell)dataitem["qAnswered"];
if (cell.Text == "True")
{
Image img = (Image)dataitem.FindControl("Image1");
img.ImageUrl = "~/images/question-mark.jpg";
this.RadGrid1.Rebind();
}
else return;
}
}
}
0

usman
Top achievements
Rank 1
answered on 14 Nov 2011, 12:56 PM
aspx code
<%@ Page Title="" Language="C#" MasterPageFile="~/master.Master" AutoEventWireup="true" CodeFile="questions.aspx.cs" Inherits="questions" %>
<%@ Register Assembly="Telerik.Web.UI" Namespace="Telerik.Web.UI" TagPrefix="telerik" %>
<
asp:Content
ID
=
"Content1"
ContentPlaceHolderID
=
"main"
Runat
=
"Server"
>
<
div
style
=
"width: 1130px; position: relative; top: -1px; left: 96px"
>
<
telerik:RadScriptManager
ID
=
"RadScriptManager1"
Runat
=
"server"
>
</
telerik:RadScriptManager
>
</
div
>
<
div
style
=
"width: 1130px; height: 30px; position: relative; top: -1px; left: 96px; height: 97px;"
>
<
div
>
<
asp:Timer
ID
=
"Timer1"
OnTick
=
"Timer1_Tick"
runat
=
"server"
Interval
=
"10000"
>
</
asp:Timer
>
</
div
>
<
telerik:RadAjaxPanel
ID
=
"RadAjaxPanel1"
runat
=
"server"
Width
=
"1130px"
>
<
telerik:RadGrid
ID
=
"RadGrid1"
runat
=
"server"
CellPadding
=
"0"
ItemStyle-Height
=
"40px"
AlternatingItemStyle-Height
=
"40px"
GridLines
=
"None"
Skin
=
"Web20"
Font-Bold
=
"False"
Font-Italic
=
"False"
Font-Overline
=
"False"
Font-Strikeout
=
"False"
Font-Underline
=
"False"
AllowPaging
=
"True"
onitemdatabound
=
"RadGrid1_ItemDataBound"
>
<
MasterTableView
AutoGenerateColumns
=
"false"
GridLines
=
"None"
>
<
Columns
>
<
telerik:GridTemplateColumn
ItemStyle-Width
=
"60px"
>
<
ItemTemplate
>
<
asp:Image
ID
=
"Image1"
runat
=
"server"
/>
</
ItemTemplate
>
</
telerik:GridTemplateColumn
>
<
telerik:GridBoundColumn
DataField
=
"qTitle"
HeaderText
=
"Title"
></
telerik:GridBoundColumn
>
<
telerik:GridDateTimeColumn
DataField
=
"qDate"
HeaderText
=
"Posted On"
ItemStyle-Width
=
"250px"
></
telerik:GridDateTimeColumn
>
<
telerik:GridBoundColumn
DataField
=
"qAnswered"
></
telerik:GridBoundColumn
>
</
Columns
>
<
RowIndicatorColumn
FooterStyle-Font-Size
=
"Medium"
>
<
HeaderStyle
Width
=
"40px"
></
HeaderStyle
>
</
RowIndicatorColumn
>
<
ExpandCollapseColumn
>
<
HeaderStyle
Width
=
"40px"
></
HeaderStyle
>
</
ExpandCollapseColumn
>
<
HeaderStyle
Font-Bold
=
"True"
Font-Italic
=
"False"
Font-Names
=
"Arial"
Font-Overline
=
"False"
Font-Size
=
"Small"
Font-Strikeout
=
"False"
Font-Underline
=
"False"
Wrap
=
"True"
/>
</
MasterTableView
>
<
ItemStyle
Font-Bold
=
"False"
Font-Italic
=
"False"
Font-Overline
=
"False"
Font-Strikeout
=
"False"
Font-Underline
=
"False"
Wrap
=
"True"
/>
<
HeaderContextMenu
EnableAutoScroll
=
"True"
></
HeaderContextMenu
>
<
ClientSettings
>
<
ClientEvents
OnCommand
=
"function(){ }"
/>
</
ClientSettings
>
</
telerik:RadGrid
>
</
telerik:RadAjaxPanel
>
<
br
/>
</
div
>
<
telerik:RadAjaxManager
runat
=
"server"
>
<
AjaxSettings
>
<
telerik:AjaxSetting
AjaxControlID
=
"Timer1"
>
<
UpdatedControls
>
<
telerik:AjaxUpdatedControl
ControlID
=
"RadGrid1"
/>
</
UpdatedControls
>
</
telerik:AjaxSetting
>
</
AjaxSettings
>
</
telerik:RadAjaxManager
>
</
asp:Content
>
0

Shinu
Top achievements
Rank 2
answered on 14 Nov 2011, 01:36 PM
Hello Usman,
I suppose this is a databinding issue. Make sure that you are populating the grid with advanced data binding using NeedDataSource event which fires each time when it needs to be bound to a data source. In the TimerTick event call
Advanced Data-binding (using NeedDataSource event)
-Shinu.
I suppose this is a databinding issue. Make sure that you are populating the grid with advanced data binding using NeedDataSource event which fires each time when it needs to be bound to a data source. In the TimerTick event call
Rebind()
method. Check the following help documentation which explains more about this.Advanced Data-binding (using NeedDataSource event)
-Shinu.
0

usman
Top achievements
Rank 1
answered on 14 Nov 2011, 04:23 PM
I dont agree , just because I included the conditional image data binding ,the AJAX stopped working
0

Jayesh Goyani
Top achievements
Rank 2
answered on 15 Nov 2011, 06:02 AM
Hello usman,
I agree with shinu, if possible then use Advance Data Binding because it will automatically handle sorting,pagging,filter...etc
or
Also check below code snippet for your code
Let me know it worked or not ??
Thanks,
Jayesh Goyani
I agree with shinu, if possible then use Advance Data Binding because it will automatically handle sorting,pagging,filter...etc
or
Also check below code snippet for your code
protected
void
Page_Load(
object
sender, EventArgs e)
{
if
(!IsPostBack)
{
BindGrid();
}
}
protected
void
Timer1_Tick(
object
sender, EventArgs e)
{
BindGrid();
}
Let me know it worked or not ??
Thanks,
Jayesh Goyani