急急急!gridView控件问题 我数据绑定什么都没问题,为什么运行后不显示gridView控件里面的数据???我没有在page_load事件里面它就是不显示. 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 不显示,先检查sql语句写的有问题么、看看数据库本身有数据库没?报错的话把错误贴出来! using System;using System.Data;using System.Configuration;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 _Default : System.Web.UI.Page { public SqlConnection GetConn() { string connStr = ConfigurationManager.AppSettings["ConnStr"].ToString(); SqlConnection myConn = new SqlConnection(connStr); return myConn; } public void bind() { SqlConnection conn = GetConn(); conn.Open(); string sqlStr = "select * from YG_xxb"; SqlDataAdapter sda = new SqlDataAdapter(sqlStr,conn); DataSet ds = new DataSet(); sda.Fill(ds); GridView1.DataSource = ds; GridView1.DataKeyNames = new string[] {"YG_ID"}; GridView1.DataBind(); sda.Dispose(); ds.Dispose(); conn.Close(); } protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e) { GridView1.EditIndex = -1; this.bind(); } protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e) { GridView1.EditIndex = e.NewEditIndex; this.bind(); } protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e) { int YG_ID = Convert.ToInt32(GridView1.DataKeys[e.RowIndex].Value.ToString()); string C_name =((TextBox)(GridView1.Rows[e.RowIndex].Cells[2].Controls[0])).Text.ToString(); string sqlStr = "update YG_xxb set YG_name='"+C_name+"'where YG_ID="+YG_ID; SqlConnection myConn = GetConn(); myConn.Open(); SqlCommand cmd = new SqlCommand(sqlStr,myConn); cmd.ExecuteNonQuery(); cmd.Dispose(); myConn.Close(); GridView1.EditIndex = -1; this.bind(); } protected void Page_Load(object sender, EventArgs e) { }} 大哥你绑定没 都没调用bind() 方法 我不要在page_load 里面显示 好像有两个错误,第一:没有调用bind()方法。第二:public void bind() { SqlConnection conn = GetConn(); conn.Open(); string sqlStr = "select * from YG_xxb"; SqlDataAdapter sda = new SqlDataAdapter(sqlStr,conn); DataSet ds = new DataSet(); sda.Fill(ds); GridView1.DataSource = ds.Tables[0]; GridView1.DataKeyNames = new string[] {"YG_ID"}; GridView1.DataBind(); sda.Dispose(); ds.Dispose(); conn.Close(); } using System; using System.Data; using System.Configuration; 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 _Default : System.Web.UI.Page { public SqlConnection GetConn() { string connStr = ConfigurationManager.AppSettings["ConnStr"].ToString(); SqlConnection myConn = new SqlConnection(connStr); return myConn; } public void bind() { SqlConnection conn = GetConn(); conn.Open(); string sqlStr = "select * from YG_xxb"; SqlDataAdapter sda = new SqlDataAdapter(sqlStr,conn); DataSet ds = new DataSet(); sda.Fill(ds); GridView1.DataSource = ds; GridView1.DataKeyNames = new string[] {"YG_ID"}; GridView1.DataBind(); sda.Dispose(); ds.Dispose(); conn.Close(); } protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e) { GridView1.EditIndex = -1; this.bind(); } protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e) { GridView1.EditIndex = e.NewEditIndex; this.bind(); } protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e) { int YG_ID = Convert.ToInt32(GridView1.DataKeys[e.RowIndex].Value.ToString()); string C_name =((TextBox)(GridView1.Rows[e.RowIndex].Cells[2].Controls[0])).Text.ToString(); string sqlStr = "update YG_xxb set YG_name='"+C_name+"'where YG_ID="+YG_ID; SqlConnection myConn = GetConn(); myConn.Open(); SqlCommand cmd = new SqlCommand(sqlStr,myConn); cmd.ExecuteNonQuery(); cmd.Dispose(); myConn.Close(); GridView1.EditIndex = -1; this.bind(); } protected void Page_Load(object sender, EventArgs e) { [size=14px] if(!IsPostback) { this.bind(); //这里要绑定 }[/size] } } 也就是说,必须要在page_load事件中绑定才能显示控件 数据吗? 在ASP中,想实现把自己的用户名和密码在实现页面跳转后自动填写到一个别的网站的登录框中,请问怎么实现,请大侠指点,谢谢 linq转换 csdn上下载的Web实例附加数据库为只读的,网页也不能运行,怎么办? 关于取IP? 线程的问题 ASP.NET中函数能不能对session变量进行传址 为什么一定要用.net自带的控件吗? C#连接有密码的外网ACCESS 急.急.急怎样才能把Dataset中的数据表的内容发入一个二维数组. 关于提交到本页后,还保存提交前页面位置的问题 如何在SQL语句中调用一个存储过程,并得到它的返回的数据或值? GridView横排数据记录
using System.Data;
using System.Configuration;
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 _Default : System.Web.UI.Page
{
public SqlConnection GetConn()
{
string connStr = ConfigurationManager.AppSettings["ConnStr"].ToString();
SqlConnection myConn = new SqlConnection(connStr);
return myConn;
}
public void bind()
{
SqlConnection conn = GetConn();
conn.Open();
string sqlStr = "select * from YG_xxb";
SqlDataAdapter sda = new SqlDataAdapter(sqlStr,conn);
DataSet ds = new DataSet();
sda.Fill(ds);
GridView1.DataSource = ds;
GridView1.DataKeyNames = new string[] {"YG_ID"};
GridView1.DataBind();
sda.Dispose();
ds.Dispose();
conn.Close();
}
protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
{
GridView1.EditIndex = -1;
this.bind();
}
protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
{
GridView1.EditIndex = e.NewEditIndex;
this.bind();
}
protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
int YG_ID = Convert.ToInt32(GridView1.DataKeys[e.RowIndex].Value.ToString());
string C_name =((TextBox)(GridView1.Rows[e.RowIndex].Cells[2].Controls[0])).Text.ToString(); string sqlStr = "update YG_xxb set YG_name='"+C_name+"'where YG_ID="+YG_ID;
SqlConnection myConn = GetConn();
myConn.Open();
SqlCommand cmd = new SqlCommand(sqlStr,myConn);
cmd.ExecuteNonQuery();
cmd.Dispose();
myConn.Close();
GridView1.EditIndex = -1;
this.bind();
}
protected void Page_Load(object sender, EventArgs e)
{ }
}
第一:没有调用bind()方法。
第二:public void bind()
{
SqlConnection conn = GetConn();
conn.Open();
string sqlStr = "select * from YG_xxb";
SqlDataAdapter sda = new SqlDataAdapter(sqlStr,conn);
DataSet ds = new DataSet();
sda.Fill(ds);
GridView1.DataSource = ds.Tables[0];
GridView1.DataKeyNames = new string[] {"YG_ID"};
GridView1.DataBind();
sda.Dispose();
ds.Dispose();
conn.Close();
}
using System.Data;
using System.Configuration;
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 _Default : System.Web.UI.Page
{
public SqlConnection GetConn()
{
string connStr = ConfigurationManager.AppSettings["ConnStr"].ToString();
SqlConnection myConn = new SqlConnection(connStr);
return myConn;
}
public void bind()
{
SqlConnection conn = GetConn();
conn.Open();
string sqlStr = "select * from YG_xxb";
SqlDataAdapter sda = new SqlDataAdapter(sqlStr,conn);
DataSet ds = new DataSet();
sda.Fill(ds);
GridView1.DataSource = ds;
GridView1.DataKeyNames = new string[] {"YG_ID"};
GridView1.DataBind();
sda.Dispose();
ds.Dispose();
conn.Close();
}
protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
{
GridView1.EditIndex = -1;
this.bind();
}
protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
{
GridView1.EditIndex = e.NewEditIndex;
this.bind();
}
protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
int YG_ID = Convert.ToInt32(GridView1.DataKeys[e.RowIndex].Value.ToString());
string C_name =((TextBox)(GridView1.Rows[e.RowIndex].Cells[2].Controls[0])).Text.ToString(); string sqlStr = "update YG_xxb set YG_name='"+C_name+"'where YG_ID="+YG_ID;
SqlConnection myConn = GetConn();
myConn.Open();
SqlCommand cmd = new SqlCommand(sqlStr,myConn);
cmd.ExecuteNonQuery();
cmd.Dispose();
myConn.Close();
GridView1.EditIndex = -1;
this.bind();
}
protected void Page_Load(object sender, EventArgs e)
{
[size=14px] if(!IsPostback)
{
this.bind(); //这里要绑定
}[/size]
}
}