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

Get RadGrid Cell Value on Button Click

18 Answers 2173 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Sravanthi
Top achievements
Rank 1
Sravanthi asked on 03 Dec 2012, 08:28 PM
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

18 Answers, 1 is accepted

Sort by
0
Shinu
Top achievements
Rank 2
answered on 04 Dec 2012, 04:03 AM
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.
0
Accepted
Jayesh Goyani
Top achievements
Rank 2
answered on 04 Dec 2012, 04:06 AM
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
0
Sravanthi
Top achievements
Rank 1
answered on 04 Dec 2012, 03:36 PM
Thanks Jayesh. That worked! I was trying to get TemplateColumn values using item["colname"] and thats why its null.

Sri!!
0
Arun
Top achievements
Rank 1
answered on 15 Jun 2013, 01:56 PM
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..
0
Jayesh Goyani
Top achievements
Rank 2
answered on 15 Jun 2013, 03:04 PM
0
bharath
Top achievements
Rank 1
answered on 28 Jan 2014, 04:01 PM
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
0
Jayesh Goyani
Top achievements
Rank 2
answered on 28 Jan 2014, 04:31 PM
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
0
bharath
Top achievements
Rank 1
answered on 28 Jan 2014, 05:04 PM
Thank you will wait for your example
0
Jayesh Goyani
Top achievements
Rank 2
answered on 29 Jan 2014, 01:11 PM
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
0
Jaya
Top achievements
Rank 1
answered on 11 Jun 2015, 09:20 AM

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

0
Jaya
Top achievements
Rank 1
answered on 11 Jun 2015, 09:23 AM

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

0
Jayesh Goyani
Top achievements
Rank 2
answered on 16 Jun 2015, 06:36 PM

Hello Jaya,

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

Thanks,

Jayesh Goyani

0
Jaya
Top achievements
Rank 1
answered on 11 Feb 2016, 09:52 AM

Hi

Jayesh

 

Can you fix this most urgent

 

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

 

0
Viktor Tachev
Telerik team
answered on 15 Feb 2016, 02:10 PM
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
0
Abdulrahman
Top achievements
Rank 1
answered on 26 Apr 2016, 06:37 AM

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. 

0
Jaya
Top achievements
Rank 1
answered on 17 Feb 2017, 04:27 AM

Hi

Jayesh Goyani

I have some issue for Telerikgrid with Select all checkbox and filter option.

How to implement a telerik grid having filter control and select all option(Check Box).....  The purpose is to filter grid row items from a list and then to cancel the selection of the filtered result set.

Can you fixed this?

0
Jaya
Top achievements
Rank 1
answered on 17 Feb 2017, 04:29 AM

Hi
Shinu

I have some issue for Telerikgrid with Select all checkbox and filter option.
How to implement a telerik grid having filter control and select all option(Check Box).....  The purpose is to filter grid row items from a list and then to cancel the selection of the filtered result set.
Can you fixed this?

0
Swati
Top achievements
Rank 1
answered on 20 Jul 2017, 01:05 PM

Hi Jayesh,

I am not able to find control GridTemplateColumn with the code you suggested . In my case I have added GridTemplateColumn  with checkbox dynamically from code behind and trying to get value of checkbox checked at custom Button click which is outside of Grid.

Tried below code but didnt worked. Your help in appreciated.

// Template Column
           string StrNAME = (item.FindControl("Label1") as Label).Text;

 

Thank you.

Swati

Tags
Grid
Asked by
Sravanthi
Top achievements
Rank 1
Answers by
Shinu
Top achievements
Rank 2
Jayesh Goyani
Top achievements
Rank 2
Sravanthi
Top achievements
Rank 1
Arun
Top achievements
Rank 1
bharath
Top achievements
Rank 1
Jaya
Top achievements
Rank 1
Viktor Tachev
Telerik team
Abdulrahman
Top achievements
Rank 1
Swati
Top achievements
Rank 1
Share this question
or