GridView中的下拉菜单DropDownList在GridView换页时未实现同步重新绑定。 10
publicSqlDataReaderddlbind(){sqlcon.Close();stringsqlstr="selectdistinctSytypfromSyst...
public SqlDataReader ddlbind()
{
sqlcon.Close();
string sqlstr = "select distinct Sytyp from Systab";
sqlcon = new SqlConnection(strConnetion);
SqlCommand sqlcom = new SqlCommand(sqlstr, sqlcon);
sqlcon.Open();
return sqlcom.ExecuteReader();
}
public void bind()
{
DropDownList ddl;
string sqlstr;
sqlstr = "select [Syid],[Sypwd],[Syemail],[Sytyp] from [Systab]";
sqlcon = new SqlConnection(strConnetion);
SqlDataAdapter myda = new SqlDataAdapter(sqlstr, sqlcon);
DataSet myds = new DataSet();
sqlcon.Open();
myda.Fill(myds, "AccountT");
GridView1.DataSource = myds;
GridView1.DataKeyNames = new string[] { "Syid" };
GridView1.DataBind();
for (int i = 0; i <= GridView1.Rows.Count - 1; i++)
{
DataRowView mydrv = myds.Tables["AccountT"].DefaultView[i];
if (Convert.ToString(mydrv["Sytyp"]).Trim() == "教师")
{
ddl = (DropDownList)GridView1.Rows[i].FindControl("DropDownList1");
ddl.SelectedIndex = 1;
}
if (Convert.ToString(mydrv["Sytyp"]).Trim() == "管理员")
{
ddl = (DropDownList)GridView1.Rows[i].FindControl("DropDownList1");
ddl.SelectedIndex = 0;
}
}
sqlcon.Close();
}
protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
GridView1.PageIndex = e.NewPageIndex;
bind();
}
如何让Dropdownlist随着GRIDVIEW的换页也能同步绑定数据呢?我的GRIDVIEW在换页时,DROPDOWNLIST的值还是第一次绑定时候的值。 展开
{
sqlcon.Close();
string sqlstr = "select distinct Sytyp from Systab";
sqlcon = new SqlConnection(strConnetion);
SqlCommand sqlcom = new SqlCommand(sqlstr, sqlcon);
sqlcon.Open();
return sqlcom.ExecuteReader();
}
public void bind()
{
DropDownList ddl;
string sqlstr;
sqlstr = "select [Syid],[Sypwd],[Syemail],[Sytyp] from [Systab]";
sqlcon = new SqlConnection(strConnetion);
SqlDataAdapter myda = new SqlDataAdapter(sqlstr, sqlcon);
DataSet myds = new DataSet();
sqlcon.Open();
myda.Fill(myds, "AccountT");
GridView1.DataSource = myds;
GridView1.DataKeyNames = new string[] { "Syid" };
GridView1.DataBind();
for (int i = 0; i <= GridView1.Rows.Count - 1; i++)
{
DataRowView mydrv = myds.Tables["AccountT"].DefaultView[i];
if (Convert.ToString(mydrv["Sytyp"]).Trim() == "教师")
{
ddl = (DropDownList)GridView1.Rows[i].FindControl("DropDownList1");
ddl.SelectedIndex = 1;
}
if (Convert.ToString(mydrv["Sytyp"]).Trim() == "管理员")
{
ddl = (DropDownList)GridView1.Rows[i].FindControl("DropDownList1");
ddl.SelectedIndex = 0;
}
}
sqlcon.Close();
}
protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
GridView1.PageIndex = e.NewPageIndex;
bind();
}
如何让Dropdownlist随着GRIDVIEW的换页也能同步绑定数据呢?我的GRIDVIEW在换页时,DROPDOWNLIST的值还是第一次绑定时候的值。 展开
3个回答
展开全部
1.
DataSet myds ;设置成全局变量
在函数GridView1_PageIndexChanging中重新绑定一次,即在函数中加入以下代码
GridView1.DataSource = null;
GridView1.DataSource = myds;
GridView1.DataKeyNames = new string[] { "Syid" };
GridView1.DataBind();
2.
可以做以下尝试,在函数bind()中:
加入代码GridView1.DataSource = null;
在 GridView1.DataSource = myds;之前
DataSet myds ;设置成全局变量
在函数GridView1_PageIndexChanging中重新绑定一次,即在函数中加入以下代码
GridView1.DataSource = null;
GridView1.DataSource = myds;
GridView1.DataKeyNames = new string[] { "Syid" };
GridView1.DataBind();
2.
可以做以下尝试,在函数bind()中:
加入代码GridView1.DataSource = null;
在 GridView1.DataSource = myds;之前
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询