要达到的效果,在GridView中有模板列有DropDownList,DropDownList根据同一个单元格中的HiddenField值绑定另一个表中的数据。
现在的问题是:在GridView中第一行DropDownList绑定不了数据。而第二行显示的是本应第一行显示的数据~~
代码如下: //活动内容由代号转换为名称;
public SqlDataReader ddliActiveId_Bind()
{
SqlConnection myconnection = new SqlConnection(sqlconn);
SqlCommand mycommand = new SqlCommand("DdliActiveIdBind", myconnection);
mycommand.CommandType = CommandType.StoredProcedure;
mycommand.Parameters.Add(new SqlParameter("@iActiveId", SqlDbType.Int, 4));
int iDCIDTemp = 0;
for (int i=0;i <GridView1 .Rows .Count;i++)
{
GridViewRow row=GridView1 .Rows [i];
iDCIDTemp=Convert.ToInt32 (((HiddenField )row .FindControl("HiddenField1")).Value.ToString ()) ;
}
mycommand.Parameters["@iActiveId"].Value = iDCIDTemp;
myconnection.Open();
SqlDataReader dr = mycommand.ExecuteReader();
return dr;
myconnection.Close(); }
现在的问题是:在GridView中第一行DropDownList绑定不了数据。而第二行显示的是本应第一行显示的数据~~
代码如下: //活动内容由代号转换为名称;
public SqlDataReader ddliActiveId_Bind()
{
SqlConnection myconnection = new SqlConnection(sqlconn);
SqlCommand mycommand = new SqlCommand("DdliActiveIdBind", myconnection);
mycommand.CommandType = CommandType.StoredProcedure;
mycommand.Parameters.Add(new SqlParameter("@iActiveId", SqlDbType.Int, 4));
int iDCIDTemp = 0;
for (int i=0;i <GridView1 .Rows .Count;i++)
{
GridViewRow row=GridView1 .Rows [i];
iDCIDTemp=Convert.ToInt32 (((HiddenField )row .FindControl("HiddenField1")).Value.ToString ()) ;
}
mycommand.Parameters["@iActiveId"].Value = iDCIDTemp;
myconnection.Open();
SqlDataReader dr = mycommand.ExecuteReader();
return dr;
myconnection.Close(); }
解决方案 »
- 听说有日历控件`````
- 如何用JQuery获得table中第一行的td是否触发了mouseenter
- @ViewBag.Title 基础
- 推荐一下精点的关于ASP.NET方面的书籍或手册
- 为何有时取到的是ipv6地址,有时又是ipv4
- 关于我的个人网站的建设,基础问题,过来人请指点一下
- 上路前,先探路
- 求一个返回SqlDataReader的存储过程
- .net如何连接Orical数据库?
- 求一正则
- 80分送上,只求高手回头一看!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
- 如何使用ADO.NET?急啊!!!
///////////////////
楼上给你说的应该是对的,如果不加判断的话第一行不是数据行而是DataControlRowType.Header,它里面是没有你的HiddenField控件的
在循环的时候加上下面的语句:
if(e.Row.RowType==DataControlRowType.DataRow)
{
GridViewRow row=GridView1 .Rows [i];
iDCIDTemp=Convert.ToInt32 (((HiddenField )row .FindControl("HiddenField1")).Value.ToString ()) ; }
public SqlDataReader ddliActiveId_Bind(object sender, EventArgs e)
{
SqlConnection myconnection = new SqlConnection(sqlconn);
SqlCommand mycommand = new SqlCommand("DdliActiveIdBind", myconnection);
mycommand.CommandType = CommandType.StoredProcedure;
mycommand.Parameters.Add(new SqlParameter("@iActiveId", SqlDbType.Int, 4));
int iDCIDTemp = 0;
for (int i=0;i<GridView1 .Rows .Count;i++)
{
//判断是否是数据行;
if (e.Row.RowType == DataControlRowType.DataRow)
{
GridViewRow row = GridView1.Rows[i];
iDCIDTemp = Convert.ToInt32(((HiddenField)row.FindControl("HiddenField1")).Value.ToString());
}
}
mycommand.Parameters["@iActiveId"].Value = iDCIDTemp;
myconnection.Open();
SqlDataReader dr = mycommand.ExecuteReader();
return dr;
myconnection.Close(); }提示:“System.EventArgs”并不包含“Row”的定义
public SqlDataReader ddliActiveId_Bind(object sender, EventArgs e)
{
SqlConnection myconnection = new SqlConnection(sqlconn);
SqlCommand mycommand = new SqlCommand("DdliActiveIdBind", myconnection);
mycommand.CommandType = CommandType.StoredProcedure;
mycommand.Parameters.Add(new SqlParameter("@iActiveId", SqlDbType.Int, 4));
int iDCIDTemp = 0;
for (int i=0;i <GridView1 .Rows .Count;i++)
{
//判断是否是数据行;
if (e.Row.RowType == DataControlRowType.DataRow)
{
GridViewRow row = GridView1.Rows[i];
iDCIDTemp = Convert.ToInt32(((HiddenField)row.FindControl("HiddenField1")).Value.ToString());
}
}
mycommand.Parameters["@iActiveId"].Value = iDCIDTemp;
myconnection.Open();
SqlDataReader dr = mycommand.ExecuteReader();
return dr;
myconnection.Close(); } 提示:“System.EventArgs”并不包含“Row”的定义
///////////
你上面的代码在GridView的RowCreated事件中来写,写完以后直接绑定到你的控件的数据源上去。另外:不建议将SqlDataReader作为返回值,它是长链接的比较占用资源, return dr; myconnection.Close();是有问题的,你已经Return了,后面的Close()根本不会执行,如果先Close()的话,你是没有办法返回SqlDataReader的
、、、、、、、、、
这个方法是你自己写的吧?里面的参数不要写成那样,没有什么用,如果需要传递,传递有意义的参数,不需要参数就不要写好了