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

RadGrid: Dynamic DataBound column creation

12 Answers 1157 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Harpreet
Top achievements
Rank 1
Harpreet asked on 10 Jul 2008, 11:08 PM
Hi,

I have a radGrid being created dynamically .

Now , the data source is a dataTable that gets created dynamically. I have no control over what is going to be there in the datatable. the only thing that I know about it is that its a dataTable, that's it.

Now based on the number of colulmns I have in the DataTable, I need to have those columns being dynamically generated as dataBoundColumns for my grid.

In other words I need to create all the data columns in my RadGrid dynamically from the DataTable .

Also, can the text cell in the RadGrid row be replaced by an image based on the text. Eg, if text or value inside cell is "ABC", then I should be able to replace it with a clickable image.

Please let me know how can this accomplished.

Thanks

12 Answers, 1 is accepted

Sort by
0
Harpreet
Top achievements
Rank 1
answered on 10 Jul 2008, 11:57 PM
I could generate my grid columns dynamically.
That was just a programing issue. My bad.

However, now the problem is that sometimes I can have too many columns from my datatable. 50 is something that i normally expect.

Displaying 50 columns in one table is going to be very bad looking as far as look and feel on the grid is concerned. Also grid lines and header cover just half of the grid. Doesn't extend to all the rows. Why is this happening?

Can you tell me is there any special way in RadGrids to handle this issue.

Also, please let me know if a cell in the grid can be replaced with a hyperlink image ?

Thanks,
0
Veli
Telerik team
answered on 11 Jul 2008, 09:17 AM
Hi Harpreet,

Have you set Height="100%" for RadGrid's MasterTableView? Also please note that due to the differences in the box model handling in various browsers, it might be required that you set a fixed height in pixels for at least one parent element of RadGrid.

You can replace cell content and controls dynamically in the ItemDataBound event. However, please make sure that RadGrid is a databound object, and as such, the safest way to do this, also to ensure proper viewstate management, is by either modifying your data source, or by defining a GridHyperLinkcolumn or a GridTemplateColumn for this purpose. Still, in the ItemDataBound event, you can access an item cell and modify its content in the following way:

void RadGrid1_ItemDataBound(object sender, GridItemEventArgs e) 
    if (e.Item is GridDataItem) 
    { 
        GridDataItem item = (GridDataItem)e.Item; 
        if (item["GoogleColumn"].Text == "Google"
        { 
            item["GoogleColumn"].Text = "<a href='http://www.google.com'>" + item["GoogleColumn"].Text + "</a>"
        } 
    } 

The above code gets the data cell corresponding to column with a unique name "GoogleColumn" and modifies its text, making it a hyperlink.

Kind regards,
Veli
the Telerik team

Instantly find answers to your questions at the new Telerik Support Center
0
Princy
Top achievements
Rank 2
answered on 11 Jul 2008, 10:34 AM
Hi Harpreet,

You can also refer the following help article which explains how to access cells and rows in RadGrid.
Accessing cells and rows

Thanks
Princy.
0
Harpreet
Top achievements
Rank 1
answered on 18 Jul 2008, 09:13 PM
Hi Veli,

Setting height to 100% doesn't solve my purpose.
The thing is that if I have lets say 50 columns in my grid then all those columns should be included inside the main grid border. But what's happening is that that grid border covers just the screen width and all the other columns go out of the main border. I tried setting width of the master table to 100% but in vein.

please help

Thanks
0
Veli
Telerik team
answered on 21 Jul 2008, 07:33 AM
Hello Harpreet,

When RadGrid's MasterTableView cannot fit all its column in RadGrid's width, it goes beyond the its borders so that the columns are still visible. This behavior can be fixed by enabling scrolling for RadGrid in its Client settings - RadGrid1.ClientSettings.Scrolling.AllowScroll = true; This way, RadGrid will now place a horizontal scroll bar to fit its columns inside the specified width. Please see if this works for you.

Regards,
Veli
the Telerik team

Instantly find answers to your questions at the new Telerik Support Center
0
Harpreet
Top achievements
Rank 1
answered on 22 Jul 2008, 04:49 AM
Hello Veli
I did try the scrolling on the client side and it works.
however i am unable to set the height on the individual grid
height = unit.percentage(100) doesn't work. It just shrink the whole grid and if i don't keep any percentage or pixels set, i renders it  like a big fat box.
any more suggestions ?
thanks
Harpreet
0
Harpreet
Top achievements
Rank 1
answered on 22 Jul 2008, 04:58 AM
Hi Veli,

I have one more problem. I have a hierarchical grid in which I have three levels.
each level has one data bound column.

For skins like outlook2007 lets say, column 2 shows some cells in its row when I expand it and if i have more levels of hierarchy then i keep seeing cells in the previous rows when I go on expanding different levels even though I have only one single data bound column in for each detailtableview.

now if i change the skin to web20, I don't see this behavior.

Again by cells I mean, the databound column row is seen as group of different cells and if i have a tool tip of that column then this is seen in each cell.
cell can also be visualized as <td>'s for a row.

Thanks
0
Veli
Telerik team
answered on 22 Jul 2008, 11:02 AM
Hi Harpreet,

Please find attached a sample project demonstrating how RadGrid can be set to 100% height without specifying a fixed height in pixels for a parent container. Please note that the problem arises due to the box model of the rendering engine of the browser, where at least one parent container needs to have a fixed size in pixels, so that Radgrid can resize accordingly when its size is set as a percentage of the parent.

As for the second problem, could you, please, provide some screenshots and/or sample code so that we can try to reproduce the behavior. We couldn't quite visualize the issue.

All the best,
Veli
the Telerik team

Instantly find answers to your questions at the new Telerik Support Center
0
Harpreet
Top achievements
Rank 1
answered on 24 Jul 2008, 06:44 PM
Actually I have my grid created dynamically in .cs file
I tried to incorporate the client side code that you sent me in my server and it still doesn't work.

I added the following:

 RadGrid3.Height = Unit.Percentage(100);
 RadGrid3.Width = Unit.Percentage(100);

 RadGrid3.MasterTableView.TableLayout = GridTableLayout.Fixed;         

I Have something like this :

-----------------------------------------------------------

  protected void Page_Load(object sender, EventArgs e)
    {
 GenerateGrid();
     }

 private void GenerateGrid()
    {
      
        DataTable dt;
     
        XmlNode myXmlNode = myXmlDoc;

        foreach (XmlNode node in myXmlNode.ChildNodes[0])
        {
           
            RadGrid3 = new RadGrid();

            RadGrid3.ItemDataBound +=new GridItemEventHandler(RadGrid3_ItemDataBound);
           
            RadGrid3.Skin = "Web20";           
            RadGrid3.PageSize = 7;
            RadGrid3.AllowPaging = true;
            RadGrid3.PagerStyle.Mode = GridPagerMode.NextPrevAndNumeric;
            RadGrid3.AutoGenerateColumns = false;
            RadGrid3.GroupingEnabled = true;
            RadGrid3.EnableAJAX = true;
            RadGrid3.ShowGroupPanel = true;
            RadGrid3.ShowGroupPanel = false;  
            RadGrid3.Height = Unit.Percentage(100);
            RadGrid3.Width = Unit.Percentage(100);
                     

            RadGrid3.MasterTableView.HierarchyDefaultExpanded = false;
            RadGrid3.MasterTableView.HierarchyLoadMode = GridChildLoadMode.ServerBind;
            RadGrid3.MasterTableView.PageSize = 7;         
            RadGrid3.MasterTableView.TableLayout = GridTableLayout.Fixed;         
            RadGrid3.ClientSettings.Scrolling.AllowScroll = true;
            RadGrid3.ClientSettings.EnableRowHoverStyle = true;
            RadGrid3.MasterTableView.PageSize = 7;
          
                       
            dt = new DataTable();
            XmlNode nodeDataException = node.ChildNodes[0].ChildNodes[0].ChildNodes[0].ChildNodes[0];
            dt = objCommon.MakeLaunchDataTable(nodeDataException.OuterXml.ToString());
            XmlNode nodeData = nodeDataException.ChildNodes[0];

            int temp = 0;
            if (nodeData.Name.ToString() == "Data")
            {
                XmlNode nodeAttribs = nodeData.ChildNodes[0];
                dataTypes = new string[nodeAttribs.ChildNodes.Count];
                foreach (XmlNode nodeAttribName in nodeAttribs)
                {                
                    dataTypes[temp++] = nodeAttribName.Attributes[0].Value.ToString();
                }
            }

            RadGrid3.MasterTableView.DataSource = dt;

            GridBoundColumn boundColumn;
            for (int i = 0; i < dt.Columns.Count; i++)
            {
               
                boundColumn = new GridBoundColumn();
              
                boundColumn.HeaderText = dt.Columns[i].ToString();
                boundColumn.DataField = dt.Columns[i].ToString();
                RadGrid3.MasterTableView.Columns.Add(boundColumn);
               
            }

            RadGrid3.DataBind();

}

---------------------------------------------------------------------
------------------------------------------------------------------------
0
Veli
Telerik team
answered on 25 Jul 2008, 10:10 AM
Hi Harpreet,

Is the Height="100%" or the hierarchy not working? Please note that for the Height="100%" to work, you need to set some properties in the styles for the <html> <body> and <form> tags, which needs to be done in a css file or on the aspx page:

html
{
    overflow:auto;
}

html,
body,
form
{
    margin:0;
    height:100%;
}

Sincerely yours,
Veli
the Telerik team

Check out Telerik Trainer, the state of the art learning tool for Telerik products.
0
Marium
Top achievements
Rank 1
answered on 27 Nov 2012, 04:29 PM
Please share the code for dynamic columns in radgrid, I urgently need help in this issue
0
Princy
Top achievements
Rank 2
answered on 02 Jan 2013, 09:53 AM
Hi,

Please check the following code snippet for creating columns dynamically.

ASPX:
<telerik:RadGrid ID="RadGrid1" runat="server" />

C#:
protected void Page_Load(object sender, System.EventArgs e)
{
    if (!IsPostBack)
    {
        RadGrid1.DataSourceID = "SqlDataSource1";
        RadGrid1.MasterTableView.DataKeyNames = new string[] { "CustomerID" };
        RadGrid1.Width = Unit.Percentage(98);
        RadGrid1.PageSize = 15;
        RadGrid1.AllowPaging = true;
        RadGrid1.PagerStyle.Mode = GridPagerMode.NextPrevAndNumeric;
        RadGrid1.AutoGenerateColumns = false;
        RadGrid1.Skin = "Web20"; 
        RadGrid1.MasterTableView.Width = Unit.Percentage(100);
        GridBoundColumn boundColumn;
        boundColumn = new GridBoundColumn();
        RadGrid1.MasterTableView.Columns.Add(boundColumn);
        boundColumn.DataField = "CustomerID";
        boundColumn.HeaderText = "CustomerID";
        boundColumn = new GridBoundColumn();
        RadGrid1.MasterTableView.Columns.Add(boundColumn);
        boundColumn.DataField = "ContactName";
        boundColumn.HeaderText = "Contact Name";
    }
}

Please check this help documentation for more help.

Thanks,
Princy.
Tags
Grid
Asked by
Harpreet
Top achievements
Rank 1
Answers by
Harpreet
Top achievements
Rank 1
Veli
Telerik team
Princy
Top achievements
Rank 2
Marium
Top achievements
Rank 1
Share this question
or