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

Parse RadGrid Rows

2 Answers 108 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Greg
Top achievements
Rank 1
Greg asked on 22 Nov 2013, 09:41 PM
I've spent the last few hours looking through the forums, as I suspect what I am doing is common.  So I am hoping you can point me to a sample of how to do this.

All I am looking to do is parse the rows of a grid and capture the values in a couple of columns, then use that to call an AJAX class. 

Here's the basic idea.  The web page loads a list of users with fields for first, middle, and last.  There is an execute search button that will run through the grid rows and capture the first, middle, last name and fire off an Ajax call that will verify if the name was found in our database.  When that button is pressed, it calls a JavaScript function.  That function needs to start with the first row, capture the first, middle, and last name columns values, and execute the search using AJAX.  The search will return a boolean, which will then be displayed as a string in the last column on the grid.

I'm thinking something like the following, but I do not know how to get the parsing of the grid and rows to work correctly.

Can you point me to a sample or explain how to do this?

Best regards,
Jon

function ExecuteSdnSearch() {
  
    //var grid = $find("<%=RadGrid1.ClientID %>");
    grid = $find("ctl00_MainContent_RadGrid1");
    var masterTableView = grid.get_masterTableView();
  
    for (var i = 0; i < length; i++) {
  
        var row = masterTableView.get_dataItems["Uid"];
        var cell = masterTableView.getCellByColumnUniqueName(row, "Uid");
        if (cell != null) uid = cell.innerHTML;
  
        cell = masterTableView.getCellByColumnUniqueName(row, "FirstName");
        if (cell != null) firstname = cell.innerHTML;
  
        cell = masterTableView.getCellByColumnUniqueName(row, "MiddleName");
        if (cell != null) middlename = cell.innerHTML;
  
        cell = masterTableView.getCellByColumnUniqueName(row, "LastName");
        if (cell != null) lastname = cell.innerHTML;
  
        // Execute AJAX Search
        var boolResult = AjaxService.SdnSearch(firstname, middlename, lastname, ExecuteSdnSearchCallBack);        
    }
}
  
function ExecuteSdnSearchCallback(bool) {
    // Update RadGrid Status Column
    cell = masterTableView.getCellByColumnUniqueName(row, "Status");
    cell.innerHTML = (string)bool; 
}

2 Answers, 1 is accepted

Sort by
0
Accepted
Jayesh Goyani
Top achievements
Rank 2
answered on 25 Nov 2013, 06:44 AM
Hello,
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Forum.aspx.cs" Inherits="Forum" %>
 
<%@ Register Assembly="Telerik.Web.UI" Namespace="Telerik.Web.UI" TagPrefix="telerik" %>
 
<!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>
            function ExecuteSdnSearch() {
                var grid = $find("<%=RadGrid1.ClientID %>");
                var masterTableView = grid.get_masterTableView();
                var Rows = masterTableView.get_dataItems();
 
                for (var i = 0; i < Rows.length; i++) {
                    var row = Rows[i];
                    var firstname = '';
                    var cell = masterTableView.getCellByColumnUniqueName(row, "Name");
                    if (cell != null) {
                        firstname = cell.innerHTML;
                    }
 
                    $.ajax({
                        url: "Forum.aspx/GetSomething",
                        type: "GET",
                        async: false,
                        dataType: "json",
                        contentType: "application/json; charset=utf-8",
                        success: function (res) {
                            cell = masterTableView.getCellByColumnUniqueName(row, "ID");
                            cell.innerHTML = res.d;
                        },
                        error: function (res) { alert("error"); }
                    });
                }
 
                return false;
            }
        </script>
    </telerik:RadCodeBlock>
</head>
<body>
    <form id="form1" runat="server">
        <telerik:RadScriptManager ID="RadScriptManager1" runat="server"></telerik:RadScriptManager>
        <telerik:RadGrid ID="RadGrid1" runat="server" AutoGenerateColumns="false" OnNeedDataSource="RadGrid1_NeedDataSource"
            AllowFilteringByColumn="true">
            <MasterTableView>
                <Columns>
                    <telerik:GridBoundColumn DataField="Name" UniqueName="Name" HeaderText="Name">
                    </telerik:GridBoundColumn>
                    <telerik:GridBoundColumn DataField="ID" UniqueName="ID" HeaderText="ID">
                    </telerik:GridBoundColumn>
                </Columns>
            </MasterTableView>
        </telerik:RadGrid>
        <asp:Button ID="Button1" runat="server" Text="ExecuteSdnSearch" OnClientClick="return ExecuteSdnSearch();" />
    </form>
</body>
</html>

public partial class Forum : System.Web.UI.Page
{
    protected void Page_Init(object source, System.EventArgs e)
    {
 
    }
 
    protected void Page_Load(object sender, EventArgs e)
    {
 
    }
 
 
    protected void RadGrid1_NeedDataSource(object sender, GridNeedDataSourceEventArgs e)
    {
 
        DataTable dt = new DataTable();
 
        dt.Columns.Add("ID", typeof(int));
        dt.Columns.Add("Name", typeof(string));
        dt.Columns.Add("Contact", typeof(DateTime));
 
        dt.Rows.Add(1, "name1", DateTime.Now);
        dt.Rows.Add(2, "name2", DateTime.Now);
        dt.Rows.Add(3, "name3", DateTime.Now);
 
        RadGrid1.DataSource = dt;
    }
 
    [WebMethod]
    [ScriptMethod(UseHttpGet = true)]
    public static string GetSomething()
    {
        //write your logic here
        return "got something_" + new Random().Next(9,19874);
    }
}


Thanks,
Jayesh Goyani
0
Greg
Top achievements
Rank 1
answered on 25 Nov 2013, 09:12 PM
Jayesh: Thanks for posting the sample.  I was able to work through my issue using your sample to how to retrieve the cell values from the grid. 

Best regards,
Jon
Tags
Grid
Asked by
Greg
Top achievements
Rank 1
Answers by
Jayesh Goyani
Top achievements
Rank 2
Greg
Top achievements
Rank 1
Share this question
or