Hi, new to RadGrid and Telerik.
I have created a RadGrid made of GridTemplateColumns and populated it with data.
Now, for each row in the grid I'd like to iterate over every value in each column in that row and if the value is numeric and is negative, I'd like to make it red.
I suspect it would be nested foreach, but I'm not seeing a Row object to iterate over and I can't see how to get the value out of each column(field) so I can test for negative.
Also, how would I set the style color to red on that value?
Which even would be best for this, Prerender or Databound?
Thanks!
I have created a RadGrid made of GridTemplateColumns and populated it with data.
Now, for each row in the grid I'd like to iterate over every value in each column in that row and if the value is numeric and is negative, I'd like to make it red.
I suspect it would be nested foreach, but I'm not seeing a Row object to iterate over and I can't see how to get the value out of each column(field) so I can test for negative.
Also, how would I set the style color to red on that value?
Which even would be best for this, Prerender or Databound?
Thanks!
5 Answers, 1 is accepted
0

Jayesh Goyani
Top achievements
Rank 2
answered on 10 Aug 2011, 06:11 AM
Hello,
Let me know if any concern.
Thanks,
Jayesh Goyani
protected
void
RadGrid1_ItemDataBound(
object
sender, GridItemEventArgs e)
{
if
(e.Item
is
GridDataItem)
//if (e.Item is GridDataItem && e.Item.OwnerTableView.Name == "ParentTable")
// you
can also identify details table/Item
{
GridDataItem item = (GridDataItem)e.Item;
if
(Convert.ToInt32(((DataRowView)item.DataItem)[
"ColumnUniqueName"
]) < value)
{
TableCell cell = item[
"ColumnUniqueName"
];
cell.BackColor = Color.Red;
}
}
}
Let me know if any concern.
Thanks,
Jayesh Goyani
0

Princy
Top achievements
Rank 2
answered on 10 Aug 2011, 06:42 AM
Hello Ken,
You can try the following code snippet to achieve your scenario.
C#:
Thanks,
Princy.
You can try the following code snippet to achieve your scenario.
C#:
protected
void
RadGrid1_ItemDataBound(
object
sender, GridItemEventArgs e)
{
if
(e.Item
is
GridDataItem)
{
GridDataItem dataItem = (GridDataItem)e.Item;
TextBox txt = (TextBox)dataItem.FindControl(
"txtMin"
);
string
value = txt.Text;
//check for condition here
}
}
Thanks,
Princy.
0

Ken
Top achievements
Rank 1
answered on 10 Aug 2011, 02:56 PM
Thanks.
This code: (DataRowView)item.DataItem)[
"ProductName"
],
issues an exception: "ProductName is neither a DataColumn nor a DataRelation for table DefaultView."
As you can see, I'm passing in the correct column name: <telerik:GridTemplateColumn UniqueName="ProductName"
Please advise,
Thank you.
0

Ken
Top achievements
Rank 1
answered on 10 Aug 2011, 05:16 PM
Thanks Princy.
The confusing thing about this is that e.Item is not a discrete item of data as implied, rather it's the full row of data. So one has to reference every single column inside this ItemDataBound event to make the changes necessary. The examples don't suggest this.
Further, it is not the UniqueName that is used to reference the value of the column, rather it is the column name of the underlying data table, this is why I was getting an exception earlier. The examples always say UniqueName which points one to the UniqueName attribute of the GridTemplateColumn, and if you are using a different UniqueName than the actual column name from the table then it's a problem.
I have renamed the UniqueName to use the exact table column names. And I have come up with this code to do what i need:
This does exactly what I need.
Thanks!
Hopes this helps someone else
The confusing thing about this is that e.Item is not a discrete item of data as implied, rather it's the full row of data. So one has to reference every single column inside this ItemDataBound event to make the changes necessary. The examples don't suggest this.
Further, it is not the UniqueName that is used to reference the value of the column, rather it is the column name of the underlying data table, this is why I was getting an exception earlier. The examples always say UniqueName which points one to the UniqueName attribute of the GridTemplateColumn, and if you are using a different UniqueName than the actual column name from the table then it's a problem.
I have renamed the UniqueName to use the exact table column names. And I have come up with this code to do what i need:
protected
void
grdCustomerComparison_ItemDataBound(
object
sender, GridItemEventArgs e)
{
if
(e.Item
is
GridDataItem)
{
GridDataItem item = e.Item
as
GridDataItem;
var columNames = ((DataRowView)item.DataItem).Row.Table.Columns.Cast<DataColumn>().Select(c => c.ColumnName);
//GridDataItem is not one piece(item) of data, it's a full row of data
//so you have to iterate over that row accessing each column
foreach
(
string
colName
in
columNames)
{
var value = ((DataRowView)item.DataItem)[colName];
if
(IsNumeric(value) && Convert.ToInt32(value) < 0)
{
//if UniqueName is different than the table
//column name then using it here will not work here and you must use some other method to get the UniqueName OR
//as I have done, use the same column names as are in your table name for the UniqueName attribute.
TableCell cell = item[colName];
cell.ForeColor = Color.Red;
}
}
}
}
This does exactly what I need.
Thanks!
Hopes this helps someone else
0
Hi Ken,
It is good to hear that you got this working. However, there are a couple of clarifications to make:
1) When you use the DataItem object, you are indeed accessing the row of data from the grid datasource and you can index it only using the DataField name. However, when using GridDataItem, when you want to access the cells, you use the column UniqueName.
2) ItemDataBound is equivalent to the GridView's RowDataBound event. It is fired for each databound item (row, header, footer) inside RadGrid. Could you please point us to any sources which give ambiguous definitions for when this event is supposed to be fired, so that we could fix them? Thank you in advance.
All the best,
Tsvetina
the Telerik team
It is good to hear that you got this working. However, there are a couple of clarifications to make:
1) When you use the DataItem object, you are indeed accessing the row of data from the grid datasource and you can index it only using the DataField name. However, when using GridDataItem, when you want to access the cells, you use the column UniqueName.
2) ItemDataBound is equivalent to the GridView's RowDataBound event. It is fired for each databound item (row, header, footer) inside RadGrid. Could you please point us to any sources which give ambiguous definitions for when this event is supposed to be fired, so that we could fix them? Thank you in advance.
All the best,
Tsvetina
the Telerik team
Browse the vast support resources we have to jump start your development with RadControls for ASP.NET AJAX. See how to integrate our AJAX controls seamlessly in SharePoint 2007/2010 visiting our common SharePoint portal.