在上一篇博客中举的例子是用的sqldatasource作为数据源的,但是在实际中,我们还是往往要用到手动的数据源的,这篇博客就来说说如果用手动数据源来为gridview编辑、删除数据。
说明:
1、所用数据库为pubs数据库中的authors表。
2、实现手动设置数据源的更新、删除等操作。
界面设置:
页面的后台代码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Configuration;
using System.Data;
namespace gridView
{
public partial class _4 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
bind();
}
}
private void bind()
{
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["pubsConnectionString"].ConnectionString);
SqlDataAdapter sda = new SqlDataAdapter("select * from authors", con);
DataSet ds = new DataSet();
sda.Fill(ds, "authors");
GridView1.DataSource=ds.Tables["authors"];
GridView1.AllowPaging = true;
GridView1.PageSize = 5;
GridView1.DataBind();
if (GridView1.PageIndex==0)
{
Button1.Enabled = false;
Button2.Enabled = false;
}
else
{
Button1.Enabled = true;
Button2.Enabled = true;
}
if (GridView1.PageIndex==GridView1.PageCount-1)
{
Button3.Enabled = false;
Button4.Enabled = false;
}
else
{
Button3.Enabled = true;
Button4.Enabled = true;
}
}
private int cmd(string query)
{
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["pubsConnectionString"].ConnectionString);
using (con)
{
con.Open();
SqlCommand cmd = new SqlCommand(query, con);
return cmd.ExecuteNonQuery();
}
}
protected void Button1_Click(object sender, EventArgs e)
{
switch (((Button)sender).CommandName.ToString())
{
case "first":
GridView1.PageIndex = 0;
break;
case "prev":
GridView1.PageIndex = GridView1.PageIndex - 1;
break;
case "last":
GridView1.PageIndex = GridView1.PageCount-1;
break;
case "next":
GridView1.PageIndex = GridView1.PageIndex + 1;
break;
}
bind();
}
protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
{
GridView1.EditIndex = e.NewEditIndex;
bind();
}
protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
string id = GridView1.DataKeys[e.RowIndex]["au_id"].ToString();
string fname = ((TextBox)GridView1.Rows[e.RowIndex].FindControl("TextBox1")).Text;
string city = ((TextBox)GridView1.Rows[e.RowIndex].FindControl("TextBox2")).Text;
string phone = ((TextBox)GridView1.Rows[e.RowIndex].FindControl("TextBox3")).Text;
string query = "update authors set au_fname='"+fname+"',city='"+city+"',phone='"+phone+"' where au_id='"+id+"'";
if (cmd(query)>0)
{
GridView1.EditIndex = -1;
bind();
}
}
protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
{
GridView1.EditIndex = -1;
bind();
}
protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
//得到该行的主键
string id = GridView1.DataKeys[e.RowIndex]["au_id"].ToString();
string query = "delete from authors where au_id='"+id+"'";
if (cmd(query)>0)
{
GridView1.EditIndex = -1;
bind();
}
}
}
}
代码其实挺简单的,一看就能看懂,不过我还是建议自己动手去做做,这样肯定会有新的收获。比如说,通过做这个小例子,对gridview的一些属性有新的认识,比如说以前的时候一直不明白“自动生成字段”是什么意思,现在看来原来是这么的简单,呵呵。
由于博客篇幅有限,没有看懂或者想要源码的博友可以留下您的邮箱,我们大家互相学习!