2013年4月9日 星期二

[ASP.NET][WebControls] GridView取得總筆數

GirdView如果是用SqlDataSource連結來源的話

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="GridViewPage.aspx.cs" Inherits="GridViewPage" %>
 
<!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>
</head>
<body>
    <form id="form1" runat="server">
    <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
    <div>
        <asp:GridView ID="GridView1" runat="server">
        </asp:GridView>
    </div>
    <asp:SqlDataSource ID="SqlDataSource1" runat="server" 
        ConnectionString="<%$ ConnectionStrings:DBConnection %>"></asp:SqlDataSource>
    </form>
</body>
</html>





GridView看看有無分頁,沒有分頁的話,直接取GridView的RowsCount,不過這方式不太實用也不方便




using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
 
public partial class GridViewPage : System.Web.UI.Page
{
 
    protected void Page_Load(object sender, EventArgs e)
    {
        
        
        if (!IsPostBack)
        {
            bindgv();
        }
    }
 
    protected void bindgv()
    {
        string str = "select EmployeeID,LastName,FirstName,Title from Employees ";
        SqlDataSource1.SelectCommand = str;
        GridView1.DataSource = SqlDataSource1;
        GridView1.DataBind();
        Label1.Text = "總筆數:" + GridView1.Rows.Count;
    }
}


顯示結果


image


 


有分頁的話,直接用SqlDataSource去取,在onselected事件中取得




<%@ Page Language="C#" AutoEventWireup="true" CodeFile="GridViewPage.aspx.cs" Inherits="GridViewPage" %>
 
<!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>
</head>
<body>
    <form id="form1" runat="server">
    <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
    <div>
        <asp:GridView ID="GridView1" runat="server" AllowPaging="True" PageSize="2">
        </asp:GridView>
    </div>
    <asp:SqlDataSource ID="SqlDataSource1" runat="server" 
        ConnectionString="<%$ ConnectionStrings:DBConnection %>" 
        onselected="SqlDataSource1_Selected"></asp:SqlDataSource>
    </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 GridViewPage : System.Web.UI.Page
{
 
    protected void Page_Load(object sender, EventArgs e)
    {
        
        
        if (!IsPostBack)
        {
            bindgv();
        }
    }
 
    protected void bindgv()
    {
        string str = "select EmployeeID,LastName,FirstName,Title from Employees ";
        SqlDataSource1.SelectCommand = str;
        GridView1.DataSource = SqlDataSource1;
        GridView1.DataBind();
    }
    protected void SqlDataSource1_Selected(object sender, SqlDataSourceStatusEventArgs e)
    {
        Label1.Text = "總筆數:" + e.AffectedRows;
    }
}


其範例結果顯示


image











 


利用SqlDataSource取得資料筆數,範例用一button去取得資料數




<%@ Page Language="C#" AutoEventWireup="true" CodeFile="GridViewPage.aspx.cs" Inherits="GridViewPage" %>
 
<!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>
</head>
<body>
    <form id="form1" runat="server">
    <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
    <asp:Button ID="btnShowGVCount"
        runat="server" Text="顯示總筆數" onclick="btnShowGVCount_Click" />
    <div>
        <asp:GridView ID="GridView1" runat="server" AllowPaging="True" PageSize="2">
        </asp:GridView>
    </div>
    <asp:SqlDataSource ID="SqlDataSource1" runat="server" 
        ConnectionString="<%$ ConnectionStrings:DBConnection %>" 
        onselected="SqlDataSource1_Selected"></asp:SqlDataSource>
    </form>
</body>
</html>


這邊要注意的是這樣取得資料的方式,必須要讓SqlDataSource進行連結,也就是我在


Page_Load的時候都進行連結,這樣才會讀取到。



using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
 
public partial class GridViewPage : System.Web.UI.Page
{
 
    protected void Page_Load(object sender, EventArgs e)
    {
 
        bindgv();
    }
 
    protected void bindgv()
    {
        string str = "select EmployeeID,LastName,FirstName,Title from Employees ";
        SqlDataSource1.SelectCommand = str;
        GridView1.DataSource = SqlDataSource1;
        GridView1.DataBind();
    }
    protected void SqlDataSource1_Selected(object sender, SqlDataSourceStatusEventArgs e)
    {
        //Label1.Text = "總筆數:" + e.AffectedRows;
    }
    protected void btnShowGVCount_Click(object sender, EventArgs e)
    {
        DataView view = (DataView)SqlDataSource1.Select(DataSourceSelectArguments.Empty);
        int count = view.Count; //(資料總筆數)
        Label1.Text = "總筆數:" + count;
        view.Dispose();
    }
}

顯示結果


image

沒有留言:

張貼留言