Get RadGrid Cell Value on Button Click

16 posts, 1 answers
  1. Sravanthi
    Sravanthi avatar
    21 posts
    Member since:
    May 2012

    Posted 03 Dec 2012 Link to this post

    Hello There, I have a RadGrid, populated using NeedDataSource Event.
    Now, I want to get the names of all my groups(ie, values of cells in a column) on a button click as follows 
    (note: the button is on the same page as grid, not inside the grid)

    On Button Click Event :
     foreach (GridItem item in rgGroups.Items)
                {
                    GridDataItem ditem = item as GridDataItem;
                    if (ditem["GroupType"].Text.Trim() == "Customer")  //GroupType is a Template Column
                    {
                        string grpname = ditem["GroupName"].Text;  //GroupName is BoundColumn
                        customers.Add(gname);
                    }
                }
                            
                listGroups.DataSource = customers;
                listGroups.DataBind();

    But, the string grpname is always null. Not sure, if this the right way.

    Thanks,
    Sri
  2. Shinu
    Shinu avatar
    17764 posts
    Member since:
    Mar 2007

    Posted 03 Dec 2012 Link to this post

    Hi,

    Try the following code to achieve your scenario.
    C#:
    protected void Button1_Click(object sender, EventArgs e)
    {
        foreach (GridDataItem item in RadGrid2.Items)
        {
            string value = item["GroupName"].Text;
        }
    }

    Thanks,
    Shinu.
  3. UI for ASP.NET Ajax is Ready for VS 2017
  4. Answer
    Jayesh Goyani
    Jayesh Goyani avatar
    2732 posts
    Member since:
    May 2010

    Posted 03 Dec 2012 Link to this post

    Hello,
    <Columns>
     
                       <telerik:GridBoundColumn DataField="Name" UniqueName="Name" HeaderText="Name">
                       </telerik:GridBoundColumn>
                       <telerik:GridTemplateColumn>
                           <ItemTemplate>
                               <asp:Label ID="Label1" runat="server" Text='<%# Eval("Name") %>'></asp:Label>
                           </ItemTemplate>
                       </telerik:GridTemplateColumn>
               
                   </Columns>

    protected void LinkButton1_Click(object sender, EventArgs e)
       {
           foreach (GridDataItem item in RadGrid1.MasterTableView.Items)
           {
               // Bound column
               string strName = item["Name"].Text;
     
     
               // Template Column
               string StrNAME = (item.FindControl("Label1") as Label).Text;
           }
                 
       }


    Thanks,
    Jayesh Goyani
  5. Sravanthi
    Sravanthi avatar
    21 posts
    Member since:
    May 2012

    Posted 04 Dec 2012 Link to this post

    Thanks Jayesh. That worked! I was trying to get TemplateColumn values using item["colname"] and thats why its null.

    Sri!!
  6. Arun
    Arun avatar
    3 posts
    Member since:
    Mar 2013

    Posted 15 Jun 2013 Link to this post

    Hi Jeyesh,

    I need cell click event on radgrid. And on cellclikc i need to take a particular cell value to trrieve the data from the db to bind to the form. After that i need to make invisible radgrid and i need the user fill some details by the user. Please let me know some idea of urs..
  7. Jayesh Goyani
    Jayesh Goyani avatar
    2732 posts
    Member since:
    May 2010

    Posted 15 Jun 2013 Link to this post

  8. bharath
    bharath avatar
    23 posts
    Member since:
    Jun 2011

    Posted 28 Jan 2014 in reply to Jayesh Goyani Link to this post

    Hi,
      
       i want to place textbox in each cell of dynamic radgrid. i did this by itemtemplate and i have other columns in Radgrid.
       i want save  all the radgrid data into sqlserver table.

     i want retrieve the textbox and other sells of data.

    Thanks Bharath
  9. Jayesh Goyani
    Jayesh Goyani avatar
    2732 posts
    Member since:
    May 2010

    Posted 28 Jan 2014 in reply to bharath Link to this post

    Hello,

    About add textbox in dynamic grid : I will provide demo on tomorrow.
    About save the data in Sql Server : for this you have to use jquery Ajax call to save the Data. because we can not access control in back end side which was dynamically added in client side.


    Thanks,
    Jayesh Goyani
  10. bharath
    bharath avatar
    23 posts
    Member since:
    Jun 2011

    Posted 28 Jan 2014 in reply to Jayesh Goyani Link to this post

    Thank you will wait for your example
  11. Jayesh Goyani
    Jayesh Goyani avatar
    2732 posts
    Member since:
    May 2010

    Posted 29 Jan 2014 in reply to bharath Link to this post

    Hello,

    Please try with the below code snippet.
    <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Forum.aspx.cs" Inherits="Forum" %>
     
     
    <!DOCTYPE html>
    <html>
    <head runat="server">
        <title></title>
        <script src="Script/jquery-1.10.2.min.js"></script>
        <telerik:RadCodeBlock ID="RadCodeBlock1" runat="server">
            <script type="text/javascript">
     
                var GridData;
     
                function OnbuttonClient() {
     
     
     
                    jQuery.ajax({
                        type: 'POST',
                        contentType: 'application/json; charset=utf-8',
                        data: '',
                        dataType: 'JSON',
                        url: 'Forum.aspx/BindGrid',
                        success: function (result) {
     
                            GridData = result.d;
                            if (GridData.length > 0) {
                                var divGridContainer = document.getElementById('divGridContainer');
                                divGridContainer.style.display = "";
                                var tableView = $find("<%= RadGrid1.ClientID %>").get_masterTableView();
                                tableView.set_dataSource(GridData);
                                tableView.dataBind();
     
                            }
                            else {
                                var divGridContainer = document.getElementById('divGridContainer');
                                divGridContainer.style.display = "none";
                            }
                        },
                        error: function () {
                            alert('Error on binding the data');
                        }
                    });
     
                    return false;
                }
                function RowDataBound(sender, args) {
                    var _name = args.get_item().get_cell("Name");
                    if (_name != null) {
                        $(_name).empty();
     
                        var input = document.createElement("input");
                        input.type = "text";
                        input.value = args.get_dataItem().Name;
                        _name.appendChild(input);
                    }
                }
     
                function OnbuttonSaveClient() {
     
                    var test = new Object();
     
                    var Employee = new Array();
     
                    var grid = $find("<%= RadGrid1.ClientID %>");
                    if (grid) {
                        var MasterTable = grid.get_masterTableView();
                        var Rows = MasterTable.get_dataItems();
                        for (var i = 0; i < Rows.length; i++) {
                            var row = Rows[i];
                            var emp = new Object();
                            emp.id = row.getDataKeyValue("ID");
                            emp.name = $(row.get_cell('Name')).find('input').val();
                            Employee.push(emp);
                        }
                    }
     
                    test.Employee = Employee;
     
                    jQuery.ajax({
                        type: 'POST',
                        contentType: 'application/json; charset=utf-8',
                        data: JSON.stringify(test),
                        dataType: 'JSON',
                        url: 'Forum.aspx/SaveData',
                        success: function (result) {
                            alert('success');
                        },
                        error: function () {
                            alert('Error on binding the data');
                        }
                    });
     
                    return false;
                }
            </script>
        </telerik:RadCodeBlock>
    </head>
    <body>
        <form id="form1" runat="server">
            <div>
                <telerik:RadScriptManager ID="RadScriptManager1" EnablePageMethods="true" runat="server">
                </telerik:RadScriptManager>
                <asp:Button ID="Button1" Text="Bind Grid" runat="server" OnClientClick="return OnbuttonClient();" />
                <asp:Button ID="Button2" Text="Save Grid" runat="server" OnClientClick="return OnbuttonSaveClient();" />
     
                <div id="divGridContainer" style="display: none;">
                    <telerik:RadGrid ID="RadGrid1" runat="server" AutoGenerateColumns="false">
                        <MasterTableView HierarchyLoadMode="Client" DataKeyNames="ID,ParentID" ClientDataKeyNames="ID,ParentID">
                            <Columns>
                                <telerik:GridBoundColumn DataField="ID" UniqueName="ID" HeaderText="ID">
                                </telerik:GridBoundColumn>
                                <telerik:GridTemplateColumn UniqueName="Name">
                                    <ItemTemplate>
                                        <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
                                    </ItemTemplate>
                                </telerik:GridTemplateColumn>
                            </Columns>
                        </MasterTableView>
                        <ClientSettings>
                            <ClientEvents OnRowDataBound="RowDataBound" />
                        </ClientSettings>
                    </telerik:RadGrid>
                </div>
            </div>
        </form>
    </body>
    </html>

    using System;
    using System.Collections.Generic;
    using System.Data;
    using System.Globalization;
    using System.IO;
    using System.Linq;
    using System.Net;
    using System.Text;
    using System.Web;
    using System.Web.Script.Services;
    using System.Web.Services;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using Telerik.Web.UI;
    using System.Data.OleDb;
    using System.Threading;
    using System.Collections;
    using System.DirectoryServices;
     
    public partial class Forum : System.Web.UI.Page
    {
     
        protected void Page_Init(object source, System.EventArgs e)
        {
     
     
        }
     
        public void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                dynamic data = new[] {
                    new { ID = "1", Name ="Name11",ParentID = "0"}
                };
                RadGrid1.MasterTableView.DataSource = data;
                RadGrid1.DataBind();
            }
        }
     
     
        protected void Page_PreRender(object sender, EventArgs e)
        {
     
        }
     
        [WebMethod]
        public static void SaveData(List<Employee> Employee)
        {
            //perform your DB operation
        }
     
     
        [WebMethod]
        public static List<Employee> BindGrid()
        {
            List<Employee> list = new List<Employee>(); ;
     
            Employee obj = new Employee();
            obj.ID = "1";
            obj.Name = "Name1";
            list.Add(obj);
     
            obj = new Employee();
            obj.ID = "2";
            obj.Name = "Name2";
            list.Add(obj);
     
            return list;
        }
     
        
        [Serializable]
        public class Employee
        {
            public string ID { get; set; }
            public string Name { get; set; }
             
        }
    }

    Let me know if any concern.

    Thanks,
    Jayesh Goyani
  12. Jaya
    Jaya avatar
    81 posts
    Member since:
    Mar 2011

    Posted 11 Jun 2015 in reply to Jayesh Goyani Link to this post

    Hi

    Webmaster 

    I saw this url for Multiple column for Autocomplete following url . But here mention DatasourceID="Sqldatasource" but i need use this same sample for call asmx method how will do this any one guide me. 

    http://demos.telerik.com/aspnet-ajax/combobox/examples/functionality/multicolumncombo/defaultcs.aspx

  13. Jaya
    Jaya avatar
    81 posts
    Member since:
    Mar 2011

    Posted 11 Jun 2015 in reply to Shinu Link to this post

    Hi

    Webmaster

     I saw this url for Multiple column for Autocomplete following url . But here mention DatasourceID="Sqldatasource" but i need use this same sample for call asmx method how will do this any one guide me. http://demos.telerik.com/aspnet-ajax/combobox/examples/functionality/multicolumncombo/defaultcs.aspx

  14. Jayesh Goyani
    Jayesh Goyani avatar
    2732 posts
    Member since:
    May 2010

    Posted 16 Jun 2015 in reply to Jaya Link to this post

    Hello Jaya,

    My sincere apologies for late reply but is your problem is resolved. 

    Thanks,

    Jayesh Goyani

  15. Jaya
    Jaya avatar
    81 posts
    Member since:
    Mar 2011

    Posted 11 Feb in reply to Jayesh Goyani Link to this post

    Hi

    Jayesh

     

    Can you fix this most urgent

     

    http://www.telerik.com/forums/autocompletebox---telerik-control

     

  16. Viktor Tachev
    Admin
    Viktor Tachev avatar
    1488 posts

    Posted 15 Feb Link to this post

    Hello Jaya,

    Please open a separate ticket/thread for every unrelated query you have. This way the information in the thread will be more consistent. Thus, it will be easier for you to search for information in your past threads.

    Also, if someone is facing similar issue they would be able to find the solution faster.

    Regards,
    Viktor Tachev
    Telerik
    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 Feedback Portal and vote to affect the priority of the items
  17. Abdulrahman
    Abdulrahman avatar
    1 posts
    Member since:
    Aug 2014

    Posted 26 Apr in reply to Jayesh Goyani Link to this post

    Hi,

    could you please send me the code in VB.net if you are familiar with VB coding? I am having the same issue of reading cells from Radgride. 

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