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

Column data from alternative sources

3 Answers 93 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Jim
Top achievements
Rank 1
Jim asked on 11 Mar 2015, 04:25 PM
I've got a fairly basic grid set up, with data being retrieved from database view, but I would like to display some related data that's from a separate source.  In my current requirement it would be a count of records on an item table, but there is logic required to determine which item table is used.  In standard aspx we would use a function call to the code-behind, passing some data from the row, that would return the the required value.

What is the best way of achieving this with Telerik?


Cheers

3 Answers, 1 is accepted

Sort by
0
Jim
Top achievements
Rank 1
answered on 13 Mar 2015, 09:17 AM
Hi,

Is there an answer to this query?

Cheers
0
Konstantin Dikov
Telerik team
answered on 16 Mar 2015, 08:35 AM
Hello Jim,

For achieving the desired result you could place a GridTemplateColumn for example, with a simple Label control, that will display the retrieved data. As for the actual retrieval and setting, you could handle the server-side OnItemDataBound event of the grid, where you could get reference to the available values of the item and furthermore, get reference to the Label control and set the retrieved value.

Following is a simple example demonstrating such implementation:
<telerik:RadGrid runat="server" ID="RadGrid1" OnNeedDataSource="RadGrid1_NeedDataSource" OnItemDataBound="RadGrid1_ItemDataBound">
    <MasterTableView>
        <Columns>
            <telerik:GridTemplateColumn UniqueName="TestColumn">
                <ItemTemplate>
                    <asp:Label runat="server" ID="Label1"></asp:Label>
                </ItemTemplate>
            </telerik:GridTemplateColumn>
        </Columns>
    </MasterTableView>
</telerik:RadGrid>

And the code-behind:
protected void RadGrid1_NeedDataSource(object sender, GridNeedDataSourceEventArgs e)
{
    DataTable table = new DataTable();
    table.Columns.Add("ID", typeof(int));
    table.Columns.Add("FirstName", typeof(string));
    table.Columns.Add("LastName", typeof(string));
    table.Columns.Add("Age", typeof(int));
    table.Columns.Add("Date", typeof(DateTime));
    table.Columns.Add("BoolValue", typeof(Boolean));
    for (int i = 1; i < 5; i++)
    {
        table.Rows.Add(i, "FirstName" + i, "LastName" + i, 20 + i, DateTime.Now.AddDays(i), i % 2 == 0);
    }
 
    (sender as RadGrid).DataSource = table;
}
 
 
protected void RadGrid1_ItemDataBound(object sender, GridItemEventArgs e)
{
    if (e.Item is GridDataItem)
    {
        GridDataItem item = e.Item as GridDataItem;
        Label label = item["TestColumn"].FindControl("Label1") as Label;
        //the following is just a demonstration how to retrieve a value from the item (the ID) and how to set
        //set some new value to the Label control in the template column
        if (int.Parse(item["ID"].Text) % 2 == 0)
        {
            label.Text = "even";
        }
        else
        {
            label.Text = "odd";
        }
    }
}

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.

 
0
Jim
Top achievements
Rank 1
answered on 16 Mar 2015, 09:23 AM
Many thanks, that looks to be exactly what I'm after.

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