手动绑定数据到GridView并实现编辑,删除,取消···
前台代码:
后台代码:
using System;
using System.Collections;
using System.Configuration;
using System.Data;
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.Data.SqlClient;
public partial class Default5 : System.Web.UI.Page
{
protected void Page_Load( object sender, EventArgs e)
{
if ( ! IsPostBack)
{
Bind();
}
}
private void Bind()
{
SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings[ " Personal " ].ConnectionString);
SqlDataAdapter adq = new SqlDataAdapter( " select * from information " , conn);
DataSet dataset = new DataSet();
adq.Fill(dataset, " information " );
GridView1.DataSource = dataset;
GridView1.DataKeyNames = new String[] { " id " } ;
GridView1.DataBind();
}
protected void GridView1_RowEditing( object sender, GridViewEditEventArgs e)
{
GridView1.EditIndex = e.NewEditIndex;
Bind();
}
protected void GridView1_RowDeleting( object sender, GridViewDeleteEventArgs e)
{
SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings[ " Personal " ].ConnectionString);
SqlCommand comm = new SqlCommand( " delete from information where id=' " + GridView1.DataKeys[e.RowIndex].Value.ToString() + " ' " ,conn);
conn.Open();
try
{
int i = Convert.ToInt32(comm.ExecuteNonQuery());
if (i > 0 )
{
Response.Write( " <script language=javascript>alert('删除成功!')</script> " );
}
else
{
Response.Write( " <script language=javascript>alert('删除失败!')</script> " );
}
Bind();
}
catch (Exception erro)
{
Response.Write( " 错误信息: " + erro.Message);
}
finally
{
conn.Close();
}
}
protected void GridView1_RowUpdating( object sender, GridViewUpdateEventArgs e)
{
string id = ((TextBox)GridView1.Rows[e.RowIndex].Cells[ 0 ].Controls[ 0 ]).Text.ToString().Trim();
string name = ((TextBox)GridView1.Rows[e.RowIndex].Cells[ 1 ].Controls[ 0 ]).Text.ToString().Trim();
string sex = ((TextBox)GridView1.Rows[e.RowIndex].Cells[ 2 ].Controls[ 0 ]).Text.ToString().Trim();
string age = ((TextBox)GridView1.Rows[e.RowIndex].Cells[ 3 ].Controls[ 0 ]).Text.ToString().Trim();
string department = ((TextBox)GridView1.Rows[e.RowIndex].Cells[ 4 ].Controls[ 0 ]).Text.ToString().Trim();
string grade = ((TextBox)GridView1.Rows[e.RowIndex].Cells[ 5 ].Controls[ 0 ]).Text.ToString().Trim();
SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings[ " Personal " ].ConnectionString);
SqlCommand comm = new SqlCommand( " update information set id=' " + id + " ', name=' " + name + " ' , sex=' " + sex + " ' , age=' " + age + " ' , department=' " + department + " ' , grade=' " + grade + " ' where id=' " + GridView1.DataKeys[e.RowIndex].Value.ToString() + " ' " , conn);
conn.Open();
try
{
int i = Convert.ToInt32(comm.ExecuteNonQuery());
if (i > 0 )
{
Response.Write( " <script language=javascript>alert('保存成功!')</script> " );
}
else
{
Response.Write( " <script language=javascript>alert('保存失败!')</script> " );
}
GridView1.EditIndex = - 1 ;
Bind();
}
catch (Exception erro)
{
Response.Write( " 错误信息: " + erro.Message);
}
finally
{
conn.Close();
}
}
protected void GridView1_RowCancelingEdit( object sender, GridViewCancelEditEventArgs e)
{
GridView1.EditIndex = - 1 ;
Bind();
}
}