新手求教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)GridView1.Rows[e.RowIndex].Cells[5].Controls[0]).Text);
Comm.ExecuteNonQuery();
sqlconn.Close();
sqlconn = null;
Comm = null;
GridView1.EditIndex = -1;
bindgrid();
}
}
就是我想把users表中的数据绑定到gridview里面,可以进行编辑,删除等操作,我调试成功了,可是为什么浏览器不显示数据啊?我都急死了,弄了一早上都没弄好,新得不能再新的菜鸟求指教啊!!!555555555555
[解决办法]
bindgrid函数中哪有对grid的绑定操作啊,也没为grid设置数据集啊
[解决办法]
bindgrid方法最后加两句:
GridView1.DataSource=ds.Tables["users"];
GridView1.DataBind();
[解决办法]
bindgrid() 这个方法里,要指明你的GV的数据源,然后绑定的方法。楼上已经给出了。
[解决办法]