新手求教Gridview后台绑定数据问题!在线等。。
我想问一下我用Gridview绑定了数据,可是为什么在显示器现实不出来的?
我页面的CS代码如下:
using System;
using System.Data;
using System.Configuration;
using System.Collections;
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 edituser : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack) bindgrid();
}
void bindgrid()
{
string sqlconnstr = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString; ;
DataSet ds = new DataSet();
using (SqlConnection sqlconn = new SqlConnection(sqlconnstr))
{
SqlDataAdapter sqld = new SqlDataAdapter("select UID,username,pwd,xm,tel,qq from users", sqlconn);
sqld.Fill(ds, "users");
}
}
protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
{
GridView1.EditIndex = e.NewEditIndex;
bindgrid();
}
protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
{
GridView1.EditIndex = -1;
bindgrid();
}
protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
string sqlconnstr = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString; ;
SqlConnection sqlconn = new SqlConnection(sqlconnstr);
sqlconn.Open();
String sql = "delete from users where UID= '" + GridView1.DataKeys[e.RowIndex].Value.ToString() + "'";
SqlCommand Comm = new SqlCommand(sql, sqlconn);
Comm.ExecuteNonQuery();
sqlconn.Close();
sqlconn = null;
Comm = null;
GridView1.EditIndex = -1;
bindgrid();
}
protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
string sqlconnstr = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString; ;
SqlConnection sqlconn = new SqlConnection(sqlconnstr);
sqlconn.Open();
SqlCommand Comm = new SqlCommand();
Comm.Connection = sqlconn;
Comm.CommandText = "update users set username=@username,pwd=@pwd,xm=@xm,tel=@tel,qq=@qq where UID=@UID";
Comm.Parameters.AddWithValue("@UID",GridView1.DataKeys[e.RowIndex].Value.ToString());
Comm.Parameters.AddWithValue("@username", ((TextBox)GridView1.Rows[e.RowIndex].Cells[1].Controls[0]).Text);
Comm.Parameters.AddWithValue("@pwd", ((TextBox)GridView1.Rows[e.RowIndex].Cells[2].Controls[0]).Text);
Comm.Parameters.AddWithValue("@xm", ((TextBox)GridView1.Rows[e.RowIndex].Cells[3].Controls[0]).Text);
Comm.Parameters.AddWithValue("@tel", ((TextBox)GridView1.Rows[e.RowIndex].Cells[4].Controls[0]).Text);
Comm.Parameters.AddWithValue("@qq", ((TextBox)GridVie