Vadiklk
Vadiklk

Reputation: 3764

Assign data to RadGrid

I want to have a grid with two columns, one with title and the second with two hyper links (if it is easier, you can create 2 different columns, one for each link).

I have an array of the titles and an array of ids. I want that the link will be something like "edit.aspx?id=INSERT_ID_FROM_DATA_HERE".

What I have so far:

<telerik:RadGrid ID="RadGrid1" runat="server" Skin="Telerik" AutoGenerateColumns="True">
                                            <MasterTableView>
                                                <Columns>
                                                    <telerik:GridBoundColumn HeaderText="Title" UniqueName="Title" DataFormatString="'{0}'">
                                                    </telerik:GridBoundColumn>
                                                    <telerik:GridHyperLinkColumn HeaderText="Actions" UniqueName="Actions" 
                                                        DataNavigateUrlFormatString="default.aspx?id='{0}" DataTextField="Edit">
                                                    </telerik:GridHyperLinkColumn>
                                                </Columns>
                                            </MasterTableView>
                                        </telerik:RadGrid>

AND:

DataTable dt = new DataTable();
            DataColumn dcTitle = new DataColumn("Title");
            DataColumn dcId = new DataColumn("Actions");
            dt.Columns.Add(dcTitle);
            dt.Columns.Add(dcId);

            String[] widgetNames = new String[list.Count];
            String[] widgetIds = new String[list.Count];

            for (int i = 0; i < list.Count; i++)
            {
                widgetNames[i] = list[i].SelectSingleNode("title").InnerText;
                widgetIds[i] = list[i].SelectSingleNode("instanceid").InnerText;

                String[] row = new String[2];
                row[0] = widgetNames[i];
                row[1] = widgetIds[i];

                dt.LoadDataRow(row, true);
            }

            RadGrid1.DataSource = dt;

It creates 2 new columns and just prints the titles and ids there, without the link.

Upvotes: 0

Views: 972

Answers (1)

ulty4life
ulty4life

Reputation: 3012

Since you are specifying columns, be sure to set AutoGenerateColumns="False". Set the DataField and DataNavigateUrlFields attributes to the column names of your data source.

The placeholder values in the data format strings refer to the specified data fields for a grid column. A GridBoundColumn can only have one DataField, but the GridHyperLinkColumn can have multiple data fields separated by commas; {0} refers to the first field, {1} to the second, etc...

To answer your question about multiple hyperlinks, it is easier to use a separate GridHyperLinkColumn for each one. Otherwise you would have to use a TemplateColumn to write your own completely custom rendering.

<telerik:RadGrid ID="RadGrid1" runat="server" Skin="Telerik" AutoGenerateColumns="False">
    <MasterTableView>
        <Columns>
            <telerik:GridBoundColumn HeaderText="Title" DataField="Title" DataFormatString="'{0}'" />
            <telerik:GridHyperLinkColumn HeaderText="Actions" UniqueName="Actions" DataNavigateUrlFields="Actions" DataNavigateUrlFormatString="default.aspx?id='{0}'" DataTextField="Edit" />
        </Columns>
    </MasterTableView>
</telerik:RadGrid>

Upvotes: 1

Related Questions