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

radGrid and GridHyperLinkColumn

1 Answer 141 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Moisoiu Andrei
Top achievements
Rank 1
Moisoiu Andrei asked on 25 Mar 2010, 11:22 AM
I have a RadGrid which have DatasourceID="1" as default and I need that GridHyperlinkColumn to have the text from DataSourceID="2", because my first datasource don't contain the "Name" field.
Here is my code:
k:RadGrid ID="RadGrid1" runat="server" AllowAutomaticDeletes="True" AllowAutomaticInserts="True" 
    AllowAutomaticUpdates="True" AutoGenerateColumns="False" AutoGenerateDeleteColumn="True" 
    AutoGenerateEditColumn="True" DataSourceID="LinqDataSource1" OnItemDataBound="RadGrid1_ItemDataBound" 
    GridLines="None" AllowSorting="True" Skin="Vista" OnItemCreated="RadGrid1_ItemCreated" 
    OnItemUpdated="RadGrid1_ItemUpdated" EnableLinqExpressions="false" ShowFooter="True" 
    DataKeyNames="ProductID"
    <MasterTableView DataKeyNames="CartID,ProductID" DataSourceID="LinqDataSource1"
        <RowIndicatorColumn> 
            <HeaderStyle Width="20px"></HeaderStyle> 
        </RowIndicatorColumn> 
        <ExpandCollapseColumn> 
            <HeaderStyle Width="20px"></HeaderStyle> 
        </ExpandCollapseColumn> 
        <Columns> 
            <telerik:GridBoundColumn DataField="CartID" HeaderText="CartID" ReadOnly="True" SortExpression="CartID" 
                UniqueName="CartID" Visible="False"
            </telerik:GridBoundColumn> 
            <telerik:GridHyperLinkColumn HeaderText="Name" UniqueName="Name" DataTextField="Name" 
                DataType="System.String" DataNavigateUrlFields="ProductID" DataNavigateUrlFormatString="/Product.aspx?ProductID={0}"
            </telerik:GridHyperLinkColumn> 
            <telerik:GridDropDownColumn DataField="ProductID" DataSourceID="LinqDataSource2" 
                HeaderText="Name" ListTextField="Name" ListValueField="ProductID" ReadOnly="True" 
                UniqueName="column"
            </telerik:GridDropDownColumn> 
            <telerik:GridDropDownColumn DataField="Quantity" HeaderText="Quantity" UniqueName="Quantity1"
            </telerik:GridDropDownColumn> 
            <telerik:GridBoundColumn DataField="DateAdded" DataType="System.DateTime" HeaderText="DateAdded" 
                ReadOnly="True" SortExpression="DateAdded" UniqueName="DateAdded" Visible="False"
            </telerik:GridBoundColumn> 
            <telerik:GridBoundColumn DataField="Price" DataFormatString="{0:c}" DataType="System.Double" 
                HeaderText="Price" ReadOnly="True" SortExpression="Price" UniqueName="Price" 
                Visible="True" /> 
            <telerik:GridCalculatedColumn HeaderText="Subtotal" DataFormatString="{0:c}" UniqueName="TotalPrice" 
                DataType="System.Double" DataFields="Quantity, Price" Expression="{0}*{1}" FooterText="Total : " 
                Aggregate="Sum" /> 
        </Columns> 
    </MasterTableView> 
    <ClientSettings EnableRowHoverStyle="True"
        <Selecting AllowRowSelect="True" /> 
    </ClientSettings> 
    <FilterMenu EnableTheming="True"
        <CollapseAnimation Type="OutQuint" Duration="200"></CollapseAnimation> 
    </FilterMenu> 
</telerik:RadGrid> 
<asp:LinqDataSource ID="ShippingDataSource" runat="server" ContextTypeName="LinqCommerceDataContext" 
    TableName="lc_RegionShippingTables"
</asp:LinqDataSource> 
<br /> 
<asp:Label ID="statusLabel" ForeColor="Red" runat="server" CssClass="style1" /><br /> 
<asp:LinqDataSource ID="LinqDataSource1" runat="server" ContextTypeName="LinqCommerceDataContext" 
    EnableDelete="True" EnableInsert="True" EnableUpdate="True" TableName="lc_ShoppingCarts" 
    Where="CartID == @CartID" OnDeleted="LinqDataSource1_Deleted" OnUpdated="LinqDataSource1_Updated"
    <WhereParameters> 
        <asp:CookieParameter CookieName="BalloonShop_CartID" Name="CartID" Type="String" /> 
    </WhereParameters> 
</asp:LinqDataSource> 
<asp:LinqDataSource ID="LinqDataSource2" runat="server" ContextTypeName="LinqCommerceDataContext" 
    Select="new (ProductID, Name)" TableName="lc_Products"
</asp:LinqDataSource> 
<asp:LinqDataSource ID="LinqDataSource3" runat="server" EnableUpdate="True" TableName="lc_ProductInventory" 
    Where="ProductID == @ProductID" ContextTypeName="LinqCommerceDataContext" Select="new (ProductID, Name)"
</asp:LinqDataSource> 
I need that GridHyperlinkColumn to have the "Name" field as text. Can you help me? Thanks

1 Answer, 1 is accepted

Sort by
0
Accepted
Radoslav
Telerik team
answered on 30 Mar 2010, 11:54 AM
Hi Moisoiu,

You could try the following approach:
On the RadGrid.ItemDataBound event handler you could get the ProductID and execute a linq expression which returns from the database, the Name by the ProductID. Then you could add the returned Name to the Text property of the hyperlink, which is from the telerik:GridHyperLinkColumn column:
void RadGrid1_ItemDataBound(object sender, GridItemEventArgs e)
{
    if (e.Item is GridDataItem)
    {
       GridDataItem item = e.Item as GridDataItem;
       int productID = Convert.ToInt32(item["column"].Text);
       string text = (from p in LinqCommerceDataContext.lc_Products
                      where p.ProductID == productID
                      select p.Name).First();
       (item["Name"].Controls[0].Controls[0] as HyperLink).Text = text;
    }
}

I hope this helps.

Best wishes,
Radoslav
the Telerik team

Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items.
Tags
Grid
Asked by
Moisoiu Andrei
Top achievements
Rank 1
Answers by
Radoslav
Telerik team
Share this question
or