Get Value of hidden column of radgrid telerik in asp.net
Asked Answered
F

3

7

I have a radgrid in which I hide Id column. Now I want to get its value on linkbutton click. If column is visible it work fine but

it show blank value when it is invisible. my code is

 protected void RadGrid1_ItemCommand(object sender, Telerik.Web.UI.GridCommandEventArgs e)
{
    if (e.CommandName == "Detail")
    {
        GridDataItem dataItm = e.Item as GridDataItem;

        string value = dataItm["Id"].Text;
    }
}
Foot answered 21/6, 2013 at 8:6 Comment(2)
can u post your html code of the grid pleaseQuick
telerik.com/forums/…Catie
M
18

Please try with the below code snippet.

ASPX

 <telerik:RadGrid ID="RadGrid1" runat="server" AutoGenerateColumns="false" OnNeedDataSource="RadGrid1_NeedDataSource"
    OnItemCommand="RadGrid1_ItemCommand">
    <MasterTableView DataKeyNames="ID">
        <Columns>
            <telerik:GridBoundColumn DataField="ID" UniqueName="ID" HeaderText="ID">
            </telerik:GridBoundColumn>
            <telerik:GridBoundColumn DataField="ID" UniqueName="ID1" HeaderText="ID" Visible="false">
            </telerik:GridBoundColumn>
            <telerik:GridBoundColumn DataField="ID" UniqueName="ID2" HeaderText="ID" Display="false">
            </telerik:GridBoundColumn>
            <telerik:GridTemplateColumn>
                <ItemTemplate>
                    <asp:Button ID="Button1" runat="server" CommandName="Detail" CommandArgument='<%# Eval("ID") %>' />
                </ItemTemplate>
            </telerik:GridTemplateColumn>
        </Columns>
    </MasterTableView>
</telerik:RadGrid>

ASPX.CS

protected void RadGrid1_NeedDataSource(object sender, GridNeedDataSourceEventArgs e)
{
    dynamic data = new[] {
        new { ID = 1, Name ="Name1"},
        new { ID = 2, Name = "Name2"},
        new { ID = 3, Name = "Name3"},
         new { ID = 4, Name = "Name4"},
        new { ID = 5, Name = "Name5"}
    };

    RadGrid1.DataSource = data;

}

protected void RadGrid1_ItemCommand(object sender, GridCommandEventArgs e)
{
    if (e.CommandName == "Detail")
    {
        GridDataItem item = e.Item as GridDataItem;

        string strID = item.GetDataKeyValue("ID").ToString(); // We are able to get ID field value here
        string strID1 = item["ID1"].Text; // We are NOT able to get ID field value here Because column is Visible false
        string strID2 = item["ID2"].Text; // We are able to get ID field value here
        string strCommandArgument = e.CommandArgument.ToString(); // We are able to get ID field value here

    }
}

Please use Display property in place-of Visible property.

Maretz answered 21/6, 2013 at 12:46 Comment(1)
This is the best approach. Notice he is using Visible="false" instead of using Display="false".Korwin
K
3

The easiest way is to set Visible = true and Display=false and you should be fine.

Kibbutznik answered 12/6, 2014 at 21:53 Comment(1)
upvote for this one. indeed is the easiest way and works perfectly.Potted
A
1

Is the column always invisible? If so, you can put the ID in the DataKeyNames property as in:

DataKeyNames="ID"

And then access it via:

var id = (int)dataItm.getDataKeyValue("ID");
Attaway answered 21/6, 2013 at 11:45 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.