How to access gridview cell value with javascript

I have a javascript function that I am trying to validate the inputs of a gridview. My problem is that I cannot get the value of the cell. Here is what I have:

    function fcnCheck() {
        var grid = document.getElementById("<%= GridViewProducts.ClientID %>");
        var cellPivot;

        if (grid.rows.length > 0) {
            for (i = 1; i < grid.rows.length-1; i++) {
                cellPivot = grid.rows[i].cells[0];
                cellStatus = grid.rows[i].cells[1];
                if (cellPivot == "Yes" and cellStatus == "") {
                    alert("You must select an answer for all columns if Pivot is yes")
                    return false;
                }
            }
        }
    }

This line does not work: cellPivot = grid.rows[i].cells[0];

Answers:

Answer

Most likely you want (edit)

var theDropdown = grid.rows[i].cells[0].elements[0];    
var selIndex = theDropdown.selectedIndex;
cellPivot = theDropdown.options[selIndex].value;

Another possibly easier or more reliable way to do this would be to tag the cells controls you want in some way and select them directly?

Answer

http://aspdotnetcodebook.blogspot.com/2010/01/how-to-get-cell-value-of-gridview-using.html#comment-form

Code

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>

<!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 src="Scripts/jquery-1.3.2.js" type="text/javascript"></script>

    <script type="text/javascript">
        $(document).ready(function() {
            var list = "";
            $("#btnGet").click(function() {
                $("#<%=GridView1.ClientID %> tr").each(function() {
                    //Skip first(header) row
                    if (!this.rowIndex) return;
                    var age = $(this).find("td:last").html();
                    list += age + "</br>";

                });
                $("#listAge").html(list)
            });

        });
    </script>

</head>
<body>
    <form id="form1" runat="server">
    <div>
    </div>
    <asp:GridView ID="GridView1" runat="server">
    </asp:GridView>
    <input type="button" id="btnGet" value="Get Cell Value" />
    <div id="listAge">
    </div>
    </form>
</body>
</html>

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class _Default : System.Web.UI.Page 
{
    protected void Page_Load(object sender, EventArgs e)
    {
       //Create Object of person class
        Person personObject = new Person();
        //Assign Person list to GridView
        GridView1.DataSource = personObject.GetPersonList();
        //Call Bindmethod of GridView
        GridView1.DataBind();

    }
}
public class Person
{
    public int ID { get; set; }
    public string Name { get; set; }
    public int Age { get; set; }
    public List<Person> GetPersonList()
    {
        //Retrun List of Person
        List<Person> list = new List<Person>()
        {
            new Person{ID=1,Name="Person1",Age=32},
            new Person{ID=2,Name="Person2",Age=45},
            new Person{ID=3,Name="Person3",Age=43},
            new Person{ID=4,Name="Person4",Age=21},
            new Person{ID=5,Name="Person5",Age=76},
            new Person{ID=6,Name="Person6",Age=54},

        };

        return list;

    }

}
Answer
<script language="javascript" type="text/javascript"> 
function Calculate() 
<br/>
{ 
<br/>
var grid = document.getElementById("<%=GridID.ClientID%>");
<br/> 
var sum = 0; <br/>
for (var i = 1; i < grid.rows.length; i++)<br/>
 { <br/>
var Cell = grid.rows[i].getElementsByTagName("input"); 
<br/>if (!Cell[4].value) {sum  += 0; } else { sum += parseFloat(Cell[4].value);} } 
<br/>
document.getElementById("<%=TextBox1.ClientID%>").value = sum; 
 }
<br/>
 </script>
------------------------------------------------------------------------



<asp:TemplateField HeaderText="Current payment" >
                            <ItemTemplate>
                                <asp:TextBox ID="cridnvalue" runat="server" Width="70px" BorderStyle="None" onkeyup="CalculateTax();" ></asp:TextBox>
                            </ItemTemplate>
                            <ItemStyle Width="120px" />
                        </asp:TemplateField>
Answer
//your gridview id  in my case my gridview is named dgd  and the id comes from
// ClientID
        var myGrid = document.getElementById("<%= dgd.ClientID  %>");
        var oRows = myGrid.rows;
            var k;
            for (k = 1; k < oRows.length; k++)
            {
                var currentRow = myGrid.rows[k];
                //now you can see the 1st,2nd and 3trd column value
                alert(currentRow.cells[1].innerHTML);   
                alert(currentRow.cells[2].innerHTML); 
                alert(currentRow.cells[3].innerHTML);
            }
Answer
function rowWisegetcellvalueingridview() {
    ///concat string
    var str1 = "";

    var n = document.getElementById('hdnColumn').value;
    ///concat string 
    var grid = document.getElementById('GridView1');
    var Inputs = grid.getElementsByTagName('input');
    var Inputsa = grid.getElementsByTagName('a');
    var Inputsspan = grid.getElementsByTagName('span');
    var Input = Inputs.length;
    var j = 0;
    var p = 0;
    var r = 0;
    for (t = 0; t < grid.rows.length - 1; t++) {
        var HdnID1 = Inputs[j].value;
        var HdnID2 = Inputs[j + 1].value;
        var HdnID3 = Inputs[j + 2].value;
        var HdnID4 = Inputs[j + 3].value;
        var HdnID5 = Inputsa[p].innerHTML;
        var HdnID6 = Inputsa[p + 1].innerHTML;
        var HdnID7 = Inputsspan[r].innerHTML;
        var varConcat = "(" + HdnID1 + "," + HdnID2 + "," + HdnID3 + "," + HdnID4 + "," + HdnID5 + "," + HdnID6 + "," + HdnID7 + "),\n";
        n = n.concat(varConcat);
        j = j + 4;
        p = p + 2;
        r = r + 1;
    }
   return false;
}

Tags

Recent Questions

Top Questions

Home Tags Terms of Service Privacy Policy DMCA Contact Us

©2020 All rights reserved.