您好,登錄后才能下訂單哦!
如何在Asp.net中使用GridView方法?很多新手對此不是很清楚,為了幫助大家解決這個難題,下面小編將為大家詳細講解,有這方面需求的人可以來學習下,希望你能有所收獲。
GridView自帶的分頁功能實現:
要實現GrdView分頁的功能
操作如下:
1、更改GrdView控件的AllowPaging屬性為true。
2、更改GrdView控件的PageSize屬性為 任意數值(默認為10)
3、更改GrdView控件的PageSetting->Mode為Numeric等(默認為Numeric)該屬性為分頁樣式。
GridView屬性設置好了,從頁面上也能看到分頁樣式。
現在開始實現分頁的功能:
1、在<<asp:GridView ID=......>后添加,OnPageIndexChanging="GridView1_PageIndexChanging"
2、在對應的aspx.cs中添加:
protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
GridView1.PageIndex = e.NewPageIndex;
InitPage(); //重新綁定GridView數據的函數
}
3、
GridView1.PageIndex = e.NewPageIndex;
完了之后再重新綁定一下GridView。
GridView添加CheckBox列實現全選/全部取消功能
首先GridView編輯模版,在模板上添加CheckBox控件,之后將新添加列字段轉換為TemplateFiled
代碼如下:
using System;using System.Data;using System.Configuration;using System.Web;using System.Web.Security;using System.Web.UI;using System.Web.UI.WebControls;using System.Web.UI.WebControls.WebParts;using System.Web.UI.HtmlControls;using System.Data.SqlClient; public partial class Default5 : System.Web.UI.Page{ SqlConnection sqlcon; string strCon = "Data Source=(local);Database=北風貿易;Uid=sa;Pwd=sa"; protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { bind(); } } protected void CheckBox2_CheckedChanged(object sender, EventArgs e) { for (int i = 0; i <= GridView1.Rows.Count - 1; i++) { CheckBox cbox = (CheckBox)GridView1.Rows[i].FindControl("CheckBox1"); if (CheckBox2.Checked == true) { cbox.Checked = true; } else { cbox.Checked = false; } } } protected void Button2_Click(object sender, EventArgs e) { sqlcon = new SqlConnection(strCon); SqlCommand sqlcom; for (int i = 0; i <= GridView1.Rows.Count - 1; i++) { CheckBox cbox = (CheckBox)GridView1.Rows[i].FindControl("CheckBox1"); if (cbox.Checked == true) { string sqlstr = "delete from 飛狐工作室 where 身份證號碼='" + GridView1.DataKeys[i].Value + "'"; sqlcom = new SqlCommand(sqlstr, sqlcon); sqlcon.Open(); sqlcom.ExecuteNonQuery(); sqlcon.Close(); } } bind(); } protected void Button1_Click(object sender, EventArgs e) { CheckBox2.Checked = false; for (int i = 0; i <= GridView1.Rows.Count - 1; i++) { CheckBox cbox = (CheckBox)GridView1.Rows[i].FindControl("CheckBox1"); cbox.Checked = false; } } public void bind() { string sqlstr = "select top 5 * from 飛狐工作室"; sqlcon = new SqlConnection(strCon); SqlDataAdapter myda = new SqlDataAdapter(sqlstr, sqlcon); DataSet myds = new DataSet(); sqlcon.Open(); myda.Fill(myds, "tb_Member"); GridView1.DataSource = myds; GridView1.DataKeyNames = new string[] { "身份證號碼" }; GridView1.DataBind(); sqlcon.Close(); }}
前臺主要代碼:
<asp:GridView ID="GridView1" runat="server" AllowSorting="True" AutoGenerateColumns="False" CellPadding="3" Font-Size="9pt" BackColor="White" BorderColor="#CCCCCC" BorderStyle="None" BorderWidth="1px"> <FooterStyle BackColor="White" ForeColor="#000066" /> <Columns> <asp:TemplateField> <ItemTemplate> <asp:CheckBox ID="CheckBox1" runat="server" /> </ItemTemplate> </asp:TemplateField> <asp:BoundField DataField="身份證號碼" HeaderText="用戶ID" SortExpression="身份證號碼" /> <asp:BoundField DataField="姓名" HeaderText="用戶姓名" SortExpression="姓名"/> <asp:BoundField DataField="家庭住址" HeaderText="家庭住址" SortExpression="家庭住址"/> </Columns> <RowStyle ForeColor="#000066" /> <SelectedRowStyle BackColor="#669999" Font-Bold="True" ForeColor="White" /> <PagerStyle BackColor="White" ForeColor="#000066" HorizontalAlign="Left" /> <HeaderStyle BackColor="#006699" Font-Bold="True" ForeColor="White" /> </asp:GridView> <asp:CheckBox ID="CheckBox2" runat="server" AutoPostBack="True" Font-Size="9pt" OnCheckedChanged="CheckBox2_CheckedChanged" Text="全選" /> <asp:Button ID="Button1" runat="server" Font-Size="9pt" Text="取消" onClick="Button1_Click" /> <asp:Button ID="Button2" runat="server" Font-Size="9pt" Text="刪除" onClick="Button2_Click" />
看完上述內容是否對您有幫助呢?如果還想對相關知識有進一步的了解或閱讀更多相關文章,請關注億速云行業資訊頻道,感謝您對億速云的支持。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。