This is a migrated thread and some comments may be shown as answers.
Binding to a DataTable with objects
1 Answer 11 Views
This is a migrated thread and some comments may be shown as answers.
Mathew
Top achievements
Rank 1
Mathew asked on 04 Oct 2013, 02:25 PM
Hi,

I have a RadGrid that I'm binding to a datatable, no problems there.  The datatable contains any number of columns that store a custom object which has some basic properties (text, link, etc.).

By overriding the ToString() method on the custom object, I can get the RadGrid to auto generate GridBoundColumns for each object, which is great, but what I really need is a HyperLink column, or possibly a template column that will allow me to bind to more than just the ToString() method of my objects.

So the question is, are any of the following possible?

1) Get AutoGenerateColumns to give me hyperlink columns with the appropriate properties assigned to DataTextField and DataNavigateURLField.
2) Use a template column to extract the values from the objects inside the bound data rows?
3) Create the columns dynamically in code-behind, and somehow bind the properties to the appropriate properties on my object?
4) Any other way of doing this?

I've got around a similar problem in the past by using reflection on a custom collection to expose members of a collection as properties that can be bound to a grid, but I'd really like to avoid doing something that complicated in this instance because it seems like there must be a simpler way to achiever it.  Hopefully there is and I just haven't come across it yet!

Thanks,
Mathew

1 Answer, 1 is accepted

Sort by
0
Angel Petrov
Telerik team
answered on 09 Oct 2013, 12:02 PM
Hello Mathew,

It seems that the scenario is quite complex however such a setup can be implemented. The only requirement which can not be met is using AutoGeneratedColumns and forcing the grid to auto-generate a GridHyperLinkColumn. As for the other questions on hand you can try the suggestions below:
  • Since I am not familiar with the exact setup I can suggest extracting the values from the custom object in the OnItemDataBound event of the grid. The data is available in the DataItem and you can extract it and then assign it to any control in the ItemTemplate according to some criteria. For obtaining a reference to the DataItem you can use this code:
    protected void RadGrid1_ItemDataBound(object sender, GridItemEventArgs e)
        {
            GridDataItem item = e.Item as GridDataItem;
            if (item != null)
            {
                DataRowView rowView = item.DataItem as DataRowView;
            }
        }
  • An example on how to create the columns dynamically in the code-behind is available in this help article. Note that when following this approach you can again use the code shown above to extract the information from the DataItem.

Generally by following the upper-mentioned suggestions you should be able to make things work. Since custom objects are being used I also recommend examining the following online resources:


Regards,
Angel Petrov
Telerik
If you want to get updates on new releases, tips and tricks and sneak peeks at our product labs directly from the developers working on the RadControls for ASP.NET AJAX, subscribe to the blog feed now.
Tags
Grid
Asked by
Mathew
Top achievements
Rank 1
Answers by
Angel Petrov
Telerik team
Share this question
or