Get Row values in RadGrid?

6 posts, 2 answers
  1. Prasanth100
    Prasanth100 avatar
    17 posts
    Member since:
    Oct 2012

    Posted 02 Feb 2014 Link to this post

    Hello Gurus,   

    Here i have attached both GridView and RadGrid. Here getting the row values working in normal Gridview, but it is not working in RadGrid.

    (i do not want to get it through through row click, when i click on the button it should be called.)


    <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
    <%@ Register TagPrefix="telerik" Namespace="Telerik.Web.UI" Assembly="Telerik.Web.UI" %><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
    <title></title>
    <script type = "text/javascript">
    function GetSelectedRow(lnk) {
    var row = lnk.parentNode.parentNode;
    var rowIndex = row.rowIndex - 1;
    var customerId = row.cells[0].innerHTML;
    var city = row.cells[1].getElementsByTagName("input")[0].value;
    alert("RowIndex: " + rowIndex + " CustomerId: " + customerId + " City:" + city);
    return false;
    }
    </script>
    </head>
    <body>
    <form id="form1" runat="server"><asp:ScriptManager ID="Sc1" runat="server"></asp:ScriptManager><asp:GridView ID="gvCustomers" runat="server" AutoGenerateColumns = "false" AllowPaging = "true" OnPageIndexChanging = "PageIndexChanging">
    <Columns>
    <asp:BoundField DataField = "CustomerID" HeaderText = "CustomerID" />
    <asp:TemplateField HeaderText = "City">
    <ItemTemplate>
    <asp:TextBox ID="txtCity" runat="server" Text = '<%# Eval("City") %>'></asp:TextBox>
    </ItemTemplate>
    </asp:TemplateField>
    <asp:TemplateField>
    <ItemTemplate>
    <asp:LinkButton ID="lnkSelect" runat="server" Text="Select" CommandName = "Select" OnClientClick = "return GetSelectedRow(this)" />
    </ItemTemplate>
    </asp:TemplateField>
    </Columns>
    </asp:GridView><telerik:RadGrid ID="RadGrid1" runat="server" AllowPaging="false" Width="97%">

    <PagerStyle Mode="NumericPages"></PagerStyle>
    <MasterTableView AutoGenerateColumns="False"
    Width="100%" CommandItemDisplay="Top" PageSize="5">
    <Columns>

    <telerik:GridTemplateColumn UniqueName="TemplateEditColumn">
    <ItemTemplate>
    <asp:TextBox ID="txtCity" runat="server" Text = '<%# Eval("City") %>'></asp:TextBox>
    </ItemTemplate>
    </telerik:GridTemplateColumn><telerik:GridTemplateColumn UniqueName="TemplateEditColumn">
    <ItemTemplate>
    <asp:LinkButton ID="lnkSelect" runat="server" Text="Select" CommandName = "Select" OnClientClick = "return GetSelectedRow(this)" />
    </ItemTemplate>
    </telerik:GridTemplateColumn>
    </Columns>

    </MasterTableView>

    </telerik:RadGrid></form>
    </body>
    </html>

    In telerik:I tried this example,but it dont work.

    http://www.telerik.com/help/aspnet-ajax/grid-getting-cell-values-for-selected-rows-client-side.html

    Help with this.

    Thanks in Advance.
  2. Answer
    Princy
    Princy avatar
    17421 posts
    Member since:
    Mar 2007

    Posted 02 Feb 2014 in reply to Prasanth100 Link to this post

    Hi Prasanth,

    I tried your code and it works fine at my end. The only issue I noticed is that to the RadGrid you haven't added the CustomerID column. Please add that column and check if the issue exist:

    ASPX:
    <telerik:GridBoundColumn UniqueName="CustomerID" DataField="CustomerID" HeaderText="CustomerID">
    </telerik:GridBoundColumn>

    Thanks,
    Princy
  3. UI for ASP.NET Ajax is Ready for VS 2017
  4. Prasanth100
    Prasanth100 avatar
    17 posts
    Member since:
    Oct 2012

    Posted 03 Feb 2014 in reply to Princy Link to this post

    Hi Princy,
     
     That worked like charm.
    Is it possible to get the values by hiding the customerID.
    B'coz in my case CustomerID is Guid. (so i dont want to show that.)
    If so,How to accomplish?

    Thanks in Advance.
       Prasanth

  5. Answer
    Princy
    Princy avatar
    17421 posts
    Member since:
    Mar 2007

    Posted 03 Feb 2014 in reply to Prasanth100 Link to this post

    Hi Prasanth,

    You can set "CustomerID" as ClientDataKeyNames and access it in the clientside as follows:

    ASPX:
    <MasterTableView ClientDataKeyNames="CustomerID" >

    JS:
    <script type="text/javascript">
        function GetSelectedRow(lnk) {       
            var row = lnk.parentNode.parentNode;
            var rowIndex = row.rowIndex - 1;
            var grid = $find("<%= RadGrid1.ClientID %>");
            if (grid) {
             var MasterTable = grid.get_masterTableView();
             var data = MasterTable.get_dataItems();
             var rows = data[rowIndex - 1];
             var customerId = rows.getDataKeyValue("CustomerID"); //Access CustomerID using DataKeyValue
             var city = row.cells[1].getElementsByTagName("input")[0].value;
             alert("RowIndex: " + rowIndex + " CustomerId: " + customerId + " City:" + city);
            }
            return false;
        }
    </script>

    Thanks,
    Princy
  6. Hon EK
    Hon EK avatar
    2 posts
    Member since:
    Feb 2014

    Posted 04 Feb 2014 Link to this post

    Hello Guys,

    Anybody who can assist me in acheving the same in ASP.NET with vb code
  7. Princy
    Princy avatar
    17421 posts
    Member since:
    Mar 2007

    Posted 04 Feb 2014 in reply to Hon EK Link to this post

    Hi Hon EK,

    I'm not clear about your requirement. The above code is to get the selected row details on LinkButton click in the Client-side. I guess you want to do this from the code behind. Please try the following code snippet and let me know if any concern:

    ASPX:
    <telerik:RadGrid ID="RadGrid1" runat="server"
        <MasterTableView DataKeyNames="CustomerID">
            <Columns>           
                <telerik:GridTemplateColumn UniqueName="CityTemplateColumn">
                  <ItemTemplate>
                   <asp:TextBox ID="txtCity" runat="server" Text='<%# Eval("ShipCity") %>'></asp:TextBox>
                  </ItemTemplate>
                </telerik:GridTemplateColumn>
                <telerik:GridTemplateColumn UniqueName="LinkTemplateColumn">
                  <ItemTemplate>
                    <asp:LinkButton ID="lnkSelect" runat="server" Text="Select" CommandName="Select" OnClick="lnkSelect_Click" />                
                  </ItemTemplate>
                </telerik:GridTemplateColumn>
            </Columns>
        </MasterTableView>
    </telerik:RadGrid>

    VB:
    Protected Sub lnkSelect_Click(sender As Object, e As EventArgs)
       Dim lnk As LinkButton = DirectCast(sender, LinkButton)
       Dim data As GridDataItem = DirectCast(lnk.NamingContainer, GridDataItem)
       Dim CustmerId As String = data.GetDataKeyValue("CustomerID").ToString()
       Dim txt As TextBox = DirectCast(data.FindControl("txtCity"), TextBox)
       Dim City As String = txt.Text
    End Sub

    Thanks,
    Princy
Back to Top
UI for ASP.NET Ajax is Ready for VS 2017