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

Access DataFormatString from ItemDataBound

4 Answers 318 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Levi
Top achievements
Rank 1
Levi asked on 19 Aug 2010, 03:43 AM
I'm trying to loop through the cell in a row and do conditional formatting based on their column definition's dataformatstring. How do I get a reference to the column they belong to so I can check this?

protected void gridOverview_ItemDataBound(object sender, Telerik.Web.UI.GridItemEventArgs e)
       {
  //Is it a GridDataItem
           if (e.Item is GridDataItem)
           {
               //Get the instance of the right type
               GridDataItem row = e.Item as GridDataItem;
               foreach (GridTableCell cell in row.Cells)
               {                      
                   // Check DataFormatString here and do conditional logic
               }

Thanks!
Levi

4 Answers, 1 is accepted

Sort by
0
Shinu
Top achievements
Rank 2
answered on 19 Aug 2010, 07:05 AM
Hello,

Use the UniqueName to access the cell in a particular column and apply the format from code behind based on the condition. The online help documentation shows how to access cells in grid.
Accessing cells and rows



-Shinu.
0
Levi
Top achievements
Rank 1
answered on 19 Aug 2010, 02:56 PM
I already tried this. The cell does not have the dataformatstring. The column does. I need a way to get a reference to the column itself so I can access the dataformatstring property. I don't how to do this using that link you sent.
0
Accepted
Dimo
Telerik team
answered on 20 Aug 2010, 08:10 AM
Hello Levi,

The table cells do not know which column they belong to, but you can target a row's cell by using the column's UniqueName. If you want to use the ItemDataBound event handler to apply conditional formatting based on DataFormatString, then loop through the columns (not the cells) and use the columns' UniqueNames to target specific cells. For example:

(the DataFormatString in this case is empty, because it is not set)


<%@ Page Language="C#" %>
<%@ Import Namespace="System.Data" %>
<%@ Register Assembly="Telerik.Web.UI" Namespace="Telerik.Web.UI" TagPrefix="telerik" %>
 
<script runat="server">
 
    protected void RadGrid_NeedDataSource(object sender, GridNeedDataSourceEventArgs e)
    {
        DataTable dt = new DataTable();
        DataRow dr;
        int colsNum = 3;
        int rowsNum = 6;
        string colName = "Column";
 
        for (int j = 1; j <= colsNum; j++)
        {
            dt.Columns.Add(String.Format("{0}{1}", colName, j));
        }
 
        for (int i = 1; i <= rowsNum; i++)
        {
            dr = dt.NewRow();
 
            for (int k = 1; k <= colsNum; k++)
            {
                dr[String.Format("{0}{1}", colName, k)] = k;
            }
            dt.Rows.Add(dr);
        }
 
        (sender as RadGrid).DataSource = dt;
    }
 
    protected void RadGrid1_ItemDataBound(object sender, GridItemEventArgs e)
    {
        if (e.Item is GridDataItem)
        {
            GridDataItem row = e.Item as GridDataItem;
            foreach (GridColumn col in (sender as RadGrid).MasterTableView.RenderColumns)
            {
                if (col is GridBoundColumn)
                {
                    row[col.UniqueName].Text += " UniqueName: " + col.UniqueName + " DataFormatString: " + (col as GridBoundColumn).DataFormatString;
                }
            }
        }
    }
     
</script>
 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
 
<head runat="server">
<meta http-equiv="content-type" content="text/html;charset=utf-8" />
<title>RadControls</title>
</head>
<body>
<form id="form1" runat="server">
<asp:ScriptManager ID="ScriptManager1" runat="server" />
 
<telerik:RadGrid
    ID="RadGrid1"
    runat="server"
    Width="800px"
    OnNeedDataSource="RadGrid_NeedDataSource"
    OnItemDataBound="RadGrid1_ItemDataBound">
     
</telerik:RadGrid>
 
</form>
</body>
</html>


Regards,
Dimo
the Telerik team
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
0
Levi
Top achievements
Rank 1
answered on 24 Aug 2010, 02:18 AM
Thanks! This works well.

Levi
Tags
Grid
Asked by
Levi
Top achievements
Rank 1
Answers by
Shinu
Top achievements
Rank 2
Levi
Top achievements
Rank 1
Dimo
Telerik team
Share this question
or