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

Dynamics Columns

1 Answer 54 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Developer
Top achievements
Rank 1
Developer asked on 26 Feb 2015, 03:32 PM


Hi,



I’m after some advice please.  My aim is to setup a dynamic sort of roadmap.  Visually this would need to be a grid comprising of twelve columns (represents the next 12 months) and then each activity would exists as a row.

So far I’ve got the grid displaying with the dynamic columns using this simple code:

   DataTable table = new
DataTable();

 

            var startDate = new DateTime(2015, 3, 1);

            var months = Enumerable.Range(0,
11)

                      
.Select(startDate.AddMonths)

                       .Select(m =>
m.ToString("yyyy MMMM"))

                       .ToList();

 

            foreach (string month
in months)

            {

               
table.Columns.Add(month);

            }

 

The next chahlenge was get the rows to appear correctly.  To enable this I created a simple test database table.

I then loop through each row of the tasking table and attempt to match the target_date value with the column date value, if I get a match I insert the data at the correct position

Here is the code I use:

     foreach (DataRow
row in dtroadmapdata.Rows)

            {

               
foreach (DataColumn
col in table.Columns)

               
{

                   
if ((string)row["target_date"] == col.ColumnName)

                   
{

                        DataRow dr = table.NewRow();

                        dr[col.Ordinal] = row["task"];

                        table.Rows.Add(dr);

                   
}

               
}

            }

 

So far so good. However instead of just displaying text I need to insert an image (ideally with an embedded hyperlink)

How can I accomplish this please, tricky because I have no template fields because the columns are creating on the fly.

Any suggestions greatly appreciated.

1 Answer, 1 is accepted

Sort by
0
Konstantin Dikov
Telerik team
answered on 03 Mar 2015, 12:21 PM
Hello Software,

RadGrid is not designed to be used with different controls for each cell in a particular column, depending on the underlying data, so it could prove difficult to achieve such result.

The main problem that you will face with this is the fact that in order to get the value from a particular cell you will need to handle the OnItemDataBound event of the grid, but that event will not preserve changes to Controls collection of the TableCell, due to the fact that event is too late in the page's life cycle.

Nevertheless, you could try to add your custom controls to the TableCell within the OnitemDataBound event in similar manner:
protected void RadGrid1_ItemDataBound(object sender, GridItemEventArgs e)
{
    if (e.Item is GridDataItem)
    {
        GridDataItem dataItem = (GridDataItem)e.Item;
        dataItem["SomeColumnName"].Controls.Clear();
 
        HyperLink hyperLink = new HyperLink();
        hyperLink.NavigateUrl = "telerik.com";
        hyperLink.Text = "telerik.com";
        dataItem["SomeColumnName"].Controls.Add(hyperLink);
    }
}

Hope this helps.


Regards,
Konstantin Dikov
Telerik
 

Check out the Telerik Platform - the only platform that combines a rich set of UI tools with powerful cloud services to develop web, hybrid and native mobile apps.

 
Tags
Grid
Asked by
Developer
Top achievements
Rank 1
Answers by
Konstantin Dikov
Telerik team
Share this question
or