Wednesday, July 25, 2012

Asp.Net jQuery GridView Row Value


The values in a specific row of a GridView can be obtained using the following jQuery script.

$("#grdEmployee tr:nth-child(n) td").each(function() {
  alert($(this).text());
});



Here n refers to the row whose value is to be obtained, in the following example we will get the values of the 2nd row from the GridView grdEmployee. We shouled add 1 to cover the header row, i.e to get the values from the 2nd row we should pass n=3.

Example

<script type="text/javascript"
src="JavaScript/jquery-1.7.2.js"></script>
<link type="text/css"
href="Stylesheet.css"
rel="Stylesheet" />
<script type="text/javascript">
$(document).ready(function() {
//
// Add Grid Style
$("#grdEmployee").addClass("Grid");
//
// Add Grid Cell Style
$("#grdEmployee td").addClass("GridCell");
      //
      // Add Header Style
$("#grdEmployee th").addClass("GridHeader");
      //
      // Add Row Style
      $("#grdEmployee tr").addClass("GridRow");
      //
      // Add Row Hover Style
      // MouseOver
      $("#grdEmployee tr").mouseover(function() {
      $(this).addClass("GridRowHover");
      })
      // MouseLeave
      $("#grdEmployee tr").mouseleave(function() {
      $(this).removeClass("GridRowHover");
      })
      //
      // Loop Through a Specific Row (2nd Row)
      // Column index starts with 0
      $('#cmdRow2').click(function(event) {
          event.preventDefault();
          $("#grdEmployee tr:nth-child(3) td").each(function() {
             alert($(this).text());
          });
      });
});
</script>  


<asp:GridView
   ID="grdEmployee"
   runat="server"
   AutoGenerateColumns="true"></asp:GridView>
<br>
<asp:Button ID="cmdRow2" runat="server" Text="Get 2nd Row" />

Add your logic in codebehind to populate the Grid with data.

Stylesheet.css

.Grid
{
      border: 1px solid #000000;
}
.GridCell
{
    padding: 3px 3px 3px 3px; /* Cellpadding */
    border: 1px solid #000000;
    font-family:Verdana;
    font-size:10pt;   
}
.GridHeader
{
      background-color:#999966;
}
.GridRow
{
    background-color:#ffffcc;
}
.GridRowHover
{
    background-color:#CCCCCC;
}


Search Flipkart Products:
Flipkart.com

No comments: