有个小伙伴在用gridview,我是多年没用了,找以前的文章,发出来给他看看。
/*页面 aspx 代码*/
<asp:GridView ID="GridView1" runat="server" PageSize="2" AllowPaging="True" AllowSorting="True" OnSorting="GridView1_Sorting" AutoGenerateColumns="False" OnPageIndexChanging="GridView1_PageIndexChanging" OnRowEditing="GridView1_RowEditing" OnRowCancelingEdit="GridView1_RowCancelingEdit" OnRowUpdating="GridView1_RowUpdating" OnRowDeleting="GridView1_RowDeleting" >
<Columns>
<asp:BoundField HeaderText="编号" DataField="id" SortExpression="id" ReadOnly="True" />
<asp:TemplateField HeaderText="简称" SortExpression="shortname">
<ItemTemplate>
<%# Eval("shortname") %>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtEdShortName" Width="100px" runat="server" Text='<%# Eval("shortname") %>'></asp:TextBox>
</EditItemTemplate>
<InsertItemTemplate>
<asp:TextBox ID="txtInShortName" runat="server"></asp:TextBox>
</InsertItemTemplate>
<ItemStyle Width="100px" />
</asp:TemplateField>
<asp:TemplateField HeaderText="省份" SortExpression="name">
<ItemTemplate>
<%# Eval("name") %>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtEdName" Width="100px" runat="server" Text='<%# Eval("name") %>'></asp:TextBox>
</EditItemTemplate>
<InsertItemTemplate>
<asp:TextBox ID="txtInName" runat="server"></asp:TextBox>
</InsertItemTemplate>
<ItemStyle Width="100px" />
</asp:TemplateField>
<asp:TemplateField HeaderText="操作">
<ItemTemplate>
<asp:Button Text="编辑" CommandName="Edit" CausesValidation="false" runat="server" ID="btEdit" />
<asp:Button Text="删除" OnClientClick="return confirm('确认要删除吗?');" CommandName="Delete" CausesValidation="false" runat="server" ID="btDelete" />
</ItemTemplate>
<EditItemTemplate>
<asp:Button Text="更新" CommandName="Update" CausesValidation="true" runat="server" ID="btUpdate" />
<asp:Button Text="取消" CommandName="Cancel" CausesValidation="false" runat="server" ID="btCancel" />
</EditItemTemplate>
</asp:TemplateField>
<%--<asp:CommandField HeaderText="操作" DeleteText="<div id="de" onclick="JavaScript:return confirm('确定删除吗?')">删除</div> " ShowEditButton="True" ShowDeleteButton="True" ShowHeader="True" />--%>
</Columns>
</asp:GridView>
/*后台 CS 代码*/
private string strSQL = "server=localhost;database=test;uid=sa;password=123456";
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
GetAllPrivence();//绑定数据
ViewState["sortColumn"] = "id";//初始排序字段
ViewState["sortDirection"] = "ASC";//初始排序方向
}
}
/// <summary>
/// GridView绑定数据源
/// </summary>
public void GetAllPrivence()
{
SqlConnection con = new SqlConnection(strSQL);
con.Open();
try
{
string sql = string.Format("select * from Province order by id");
SqlCommand cmd = new SqlCommand(sql, con);
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
da.Fill(ds, "tempProvince");
this.GridView1.DataSource = ds;
this.GridView1.DataBind();
}
catch (Exception)
{
throw;
}
finally
{
con.Close();
}
}
/// <summary>
/// 排序的方法
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void GridView1_Sorting(object sender, GridViewSortEventArgs e)
{
this.GridView1.EditIndex = -1;
if (ViewState["sortColumn"].ToString() == e.SortExpression.ToString())
{
if ("ASC" == ViewState["sortDirection"].ToString())
{
ViewState["sortDirection"] = "DESC";
}
else
{
ViewState["sortDirection"] = "ASC";
}
//重新绑定数据
Sort(ViewState["sortColumn"].ToString(), ViewState["sortDirection"].ToString());
}
else
{
ViewState["sortColumn"] = e.SortExpression.ToString();
ViewState["sortDirection"] = "ASC";
//重新绑定数据
Sort(ViewState["sortColumn"].ToString(), ViewState["sortDirection"].ToString());
}
}
/// <summary>
/// 排序后重新绑定数据的方法
/// </summary>
/// <param name="sortColumn"></param>
/// <param name="sortDirection"></param>
protected void Sort(string sortColumn,string sortDirection)
{
SqlConnection con = new SqlConnection(strSQL);
con.Open();
try
{
string sql = string.Format("select * from Province order by {0} {1}", sortColumn,sortDirection);
SqlCommand cmd = new SqlCommand(sql, con);
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
da.Fill(ds, "tempProvince");
this.GridView1.DataSource = ds;
this.GridView1.DataBind();
}
catch (Exception)
{
throw;
}
finally
{
con.Close();
}
}
/// <summary>
/// 分页
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
this.GridView1.PageIndex = e.NewPageIndex;
Sort(ViewState["sortColumn"].ToString(), ViewState["sortDirection"].ToString());
}
/// <summary>
/// 编辑
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
{
this.GridView1.EditIndex = e.NewEditIndex;
GetAllPrivence();
}
/// <summary>
/// 取消编辑
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
{
this.GridView1.EditIndex = -1;
GetAllPrivence();
}
/// <summary>
/// 更新事件
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
TextBox txtPName = (TextBox)this.GridView1.Rows[e.RowIndex].FindControl("txtEdName");
TextBox txtSName = (TextBox)this.GridView1.Rows[e.RowIndex].FindControl("txtEdShortName");
int id = Convert.ToInt32(this.GridView1.Rows[e.RowIndex].Cells[0].Text);
string pname = txtPName.Text.ToString();
string sname = txtSName.Text.ToString();
string sql = string.Format("update Province set shortname='{0}',name='{1}' where id='{2}'",sname,pname,id);
SQLHelper.ExecuteCommand(sql);
this.GridView1.EditIndex = -1;
GetAllPrivence();
}
/// <summary>
/// 删除
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
int id = Convert.ToInt32(this.GridView1.Rows[e.RowIndex].Cells[0].Text);
//Page.ClientScript.RegisterStartupScript(GetType(), "", "<script>confirm('确认要删除吗?');</script>")
//<div id="de" onclick="JavaScript:return confirm('确定删除吗?')">删除</div>
string sql = string.Format("delete Province where id='{0}'", id);
SQLHelper.ExecuteCommand(sql);
GetAllPrivence();
}