Related Posts Plugin for WordPress, Blogger...

About

Follow Us

Monday, 27 July 2015

In this article I will explain how to count rows of gridview using jquery in asp.net(c# or vb.net.).
In previous article I have explained how to get count of gridviews rows using javascript. In this article I will use jquery to achieve the same.

Demo:

Following Jquery code we will use to get count  Gridview’s rows:

<script type="text/javascript">
    $(function () {
        $("[id*=btngetCount]").click(function () {
            var totalRowCount = $("[id*=GridView1] tr").length;
            var rowCount = $("[id*=GridView1] td").closest("tr").length;
            var message = "Total Row Count: " + totalRowCount;
            message += "\nRow Count: " + rowCount;
            alert(message);
            return false;
        });
    });
</script>

Design Section:

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
    <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
<script type="text/javascript">
    $(function () {
        $("[id*=btngetCount]").click(function () {
            var totalRowCount = $("[id*=GridView1] tr").length;
            var rowCount = $("[id*=GridView1] td").closest("tr").length;
            var message = "Total Row Count: " + totalRowCount;
            message += "\nRow Count: " + rowCount;
            alert(message);
            return false;
        });
    });
</script>

</head>
<body>
    <form id="form1" runat="server">
    <center>
     <fieldset style="width:280px"><legend><strong>Coutn Rows Of Gridview</strong></legend>
    <div>
    <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False">
                      <Columns>
                <asp:BoundField DataField="Student_Name" HeaderText="Student name" />
                <asp:BoundField DataField="age" HeaderText="Age" />
                <asp:BoundField DataField="class" HeaderText="Class" />
                </Columns>
        </asp:GridView>
<br />
<br />
<asp:Button ID="btngetCount" Text="Count Rows" runat="server" />
           </div>
    </fieldset></center>
    </form>
</body>

</html>

Asp.NET code using c#:

using System.Data.SqlClient;
using System.Data;
using System.Configuration;

public partial class gridopen : System.Web.UI.Page
{
    SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["con"].ConnectionString);
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            BindGrid();
        }
    }
    public void BindGrid()
    {
        try
        {
            SqlDataAdapter adp = new SqlDataAdapter("select * from student_Info", con);        
            DataTable dt = new DataTable();
            adp.Fill(dt);
            if (dt.Rows.Count > 0)
            {
                GridView1.DataSource = dt;
                GridView1.DataBind();
            }
        }
        catch (Exception ex)
        {
        }
    }
}



Using Vb.NET:

Imports System.Data.SqlClient
Imports System.Data
Imports System.Configuration

Partial Public Class gridopen
    Inherits System.Web.UI.Page
    Private con As New SqlConnection(ConfigurationManager.ConnectionStrings("con").ConnectionString)
Protected Sub Page_Load(sender As Object, e As EventArgs)
    If Not IsPostBack Then
        BindGrid()
    End If
End Sub
Public Sub BindGrid()
    Try
        Dim adp As New SqlDataAdapter("select * from student_Info", con)
        Dim dt As New DataTable()
        adp.Fill(dt)
        If dt.Rows.Count > 0 Then
            GridView1.DataSource = dt
            GridView1.DataBind()
        End If
    Catch ex As Exception
    End Try
End Sub




0 comments:

Post a Comment