GridView控件用來在表中顯示資料源中的值.. GridView控件可用于任何重複的結構中的資料.
控件支援以下功能:
1:可綁定資料源控件;
2:内置排序功能;
3:内置更新和删除功能;
4:内置行選擇功能;
5:用于超連結列的多個資料字段;
6:可通過主題和樣式進行自定義外觀;
首先建立資料實體層MessageType
using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
namespace JingXin.Model
{
public class MessageType
{
private int _M_Id;
public int M_Id
{
get { return _M_Id; }
set { _M_Id = value; }
}
private string _M_Name;
public string M_Name
get { return _M_Name; }
set { _M_Name = value; }
}
}
建立資料庫連接配接: help
using System.Data.SqlClient;
namespace JingXin.DAL
public class Help
private SqlConnection conn = null;
private SqlCommand com = null;
private string sqlconnection = "Data Source=.;Initial Catalog=JingXin;User ID=sa;Password=sa";
public Help()
conn = new SqlConnection(sqlconnection);
com = new SqlCommand();
com.Connection = conn;
public int ExeNo(string sql, System.Data.CommandType ct)
int fh;
com.CommandText = sql;
com.CommandType = ct;
conn.Open();
fh = com.ExecuteNonQuery();
conn.Close();
return fh;
public System.Data.DataTable ExeTable(string sql,CommandType ct)
System.Data.DataTable dt = new System.Data.DataTable();
SqlDataAdapter oda = new SqlDataAdapter(com);
oda.Fill(dt);
return dt;
建立方法類MessageTypeDAL
public class MessageTypeDAL:Help
private Model.MessageType Mt;
public MessageTypeDAL()
{ }
public MessageTypeDAL(Model.MessageType mt)
this.Mt = mt;
public int Update()
string sql = " UPDATE [MessageType] SET M_Name='" + Mt.M_Name + "' WHERE M_Id = " + Mt.M_Id + " ";
return ExeNo(sql, CommandType.Text);
public int Delete()
string sql = " DELETE FROM [MessageType] WHERE M_Id = " + Mt.M_Id + " ";
public DataTable Select()
string sql = " SELECT * FROM [MessageType] ";
return ExeTable(sql, CommandType.Text);
建立頁面
MessageTypeManage.aspx
從工具中資料拖取GridView控件
![](https://img.laitimes.com/img/_0nNw4CM6IyYiwiM6ICdiwiInBnauUTN0czM1MDMy8CXxATMxAjMvwFduVWboNWY0RXYvwVbvNmLvR3YxUjL5M3Lc9CX6MHc0RHaiojIsJye.jpg)
在GridView中實作顯示資料
1、編輯GridView的頁眉模闆
2、編輯GridView的項模闆(itemtemplete)
3、寫方法,在一個類中将要顯示的資料庫表中所有資料
添加到記憶體的一個DataTable裡
4、在頁面的load事件裡加載資料,注意需要設定GridView的datasource屬性和調用GridView的databind()方法
5、在itemtemplete中将資料庫表中的字段綁定到指定的label上去,注意綁定的方法,調用Eval()
在GridView中實作删除資料
1、在項模闆中添加一列,放入linkbutton,并且将該linkButton的CommandName屬性設定為delete
2、調用GridView的DeleteCommand事件,在該事件中實作删除
3、通過事件的參數e來擷取選中行的索引值,根據該索引值在GridView中findControl你指定的控件
4、去資料庫删除(即調用删除方法),重新整理本頁面
在GridView中實作編輯
1、編輯editItemTemplete,注意主鍵列不能使用textBox
2、将頁面編輯按鈕的CommandName屬性設定為edit
3、在GridView的EditCommand事件中将目前選中行指派給GridView的EditItemIndex屬性
4、重新整理本頁面
在GridView中實作更新
1、将編輯模闆的更新按鈕的CommandName屬性設定為update
2、寫更新的方法
3、在GridView的UpdateCommand事件中擷取編輯模闆中各個項的具體值,然後調用方法修改資料庫
4、将目前編輯項的索引值改為-1,重新整理本頁面
完成後頁面代碼:
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="MessageTypeManage.aspx.cs" Inherits="JingXin.Admin.MessageTypeManage" %>
<!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 id="Head1" runat="server">
<title>分類管理</title>
<link rel="stylesheet" href="inc/css.css" type="text/css" />
</head>
<body>
<center>
<form id="form1" runat="server">
<div style="width:100%;">
<table width="100%" border=0 align=center cellpadding=2 cellspacing=1 bordercolor="#799AE1" class=tableBorder>
<tbody>
<tr>
<th align=center colspan=16 style="height: 23px">分類管理
</th>
</tr>
<tr bgcolor="#DEE5FA">
<td colspan="16" align="center" class=txlrow>&nbsp;</td>
</tbody>
</table>
</div>
<asp:GridView ID="GridView1" Width="100%" runat="server"
AutoGenerateColumns="False" AllowPaging="True" BackColor="White"
BorderColor="#3366CC" BorderStyle="None" BorderWidth="1px" CellPadding="4"
PageSize="5" GridLines="Vertical"
onpageindexchanging="GridView1_PageIndexChanging"
onrowcancelingedit="GridView1_RowCancelingEdit"
onrowdeleting="GridView1_RowDeleting" onrowediting="GridView1_RowEditing"
onrowupdating="GridView1_RowUpdating">
<RowStyle BackColor="White" ForeColor="#003399" />
<Columns>
<asp:TemplateField HeaderText="編号">
<ItemTemplate>
<%# Eval("M_Id")%>
<asp:HiddenField ID="HiddenField1" runat="server" Value='<%# Eval("M_Id") %>' />
</ItemTemplate>
<HeaderStyle BackColor="#799AE1" />
</asp:TemplateField>
<asp:TemplateField HeaderText="分類名">
<EditItemTemplate>
<asp:TextBox ID="TextBox1" runat="server" Text='<%# Eval("M_Name") %>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<%# Eval("M_Name")%>
<asp:CommandField ShowEditButton="True" >
</asp:CommandField>
<asp:CommandField ShowDeleteButton="True" >
</Columns>
<FooterStyle BackColor="#99CCCC" ForeColor="#003399" />
<PagerStyle BackColor="#99CCCC" ForeColor="#003399" HorizontalAlign="Left" />
<SelectedRowStyle BackColor="#009999" Font-Bold="True" ForeColor="#CCFF99" />
<HeaderStyle HorizontalAlign="Center"
VerticalAlign="Middle" />
</asp:GridView>
<hr color="blue" />
<div style="width:100%">
</form>
</center>
</body>
</html>
背景代碼:
using System.Collections;
namespace JingXin.Admin
public partial class MessageTypeManage : System.Web.UI.Page
protected void Page_Load(object sender, EventArgs e)
if (!IsPostBack)
{
Data_Bind();
}
protected void Data_Bind()
DAL.MessageTypeDAL mt = new JingXin.DAL.MessageTypeDAL();
GridView1.DataSource = mt.Select();
GridView1.DataBind();
protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
GridView1.PageIndex = e.NewPageIndex;
Data_Bind();
protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
GridView1.EditIndex = e.NewEditIndex;
protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
GridView1.EditIndex = -1;
protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
String id = (((HiddenField)GridView1.Rows[e.RowIndex].FindControl("HiddenField1")).Value);
string mname = ((TextBox)GridView1.Rows[e.RowIndex].FindControl("TextBox1")).Text;
DAL.MessageTypeDAL MT = new JingXin.DAL.MessageTypeDAL(new Model.MessageType() { M_Id = int.Parse(id), M_Name = mname });
MT.Update();
protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
DAL.MessageTypeDAL MT = new JingXin.DAL.MessageTypeDAL(new Model.MessageType() { M_Id = int.Parse(id) });
MT.Delete();