Manuel Rodriguez
Manuel Rodriguez

Reputation: 17

How to get gridview cell value iterating with JS or JQuery

I need to get cell values by iterating my GridView with JQuery. The thing is that my gridview in a content placeholder, so I can't call my grid by using #GridId. The way I call a grid in JQuery is using $('[id$=GridId']). I'm new in JQuery :p

Upvotes: 0

Views: 2846

Answers (2)

Bhanu Pratap
Bhanu Pratap

Reputation: 1761

In case if grid view is in content placeholder then inspect your Grid view in browser and copy Id of grid view (i.e. Id of your content placeholder + "_" + Id of Grid view ex: ContentPlaceholder1_GridView1) and use that combined ID in JQuery.

Then get Rows from grid Then get Columns for each row.

For reference have a look : https://www.aspsnippets.com/Articles/Get-selected-Row-Cell-value-in-GridView-using-jQuery-in-ASPNet.aspx

Upvotes: 0

Selim Yildiz
Selim Yildiz

Reputation: 5370

Let's say you have a GridView like this:

<form id="form1" runat="server">
    <asp:GridView ID="MyGridView" runat="server">
    </asp:GridView>
</form>

You can get values of cell with iterating by using each as following:

<script type="text/javascript">
        $(document).ready(function() {
              $("#<%=MyGridView.ClientID %> tr").each(function() {
                    var firstCellValue = $(this).find("td:eq(1)").html(); 
                    var lastCellValue = $(this).find("td:last").html();
                    //etc...
                });
        });
    </script>

Remember if you have table header then you should skip first row:

if (!this.rowIndex) return;

Upvotes: 2

Related Questions