要达到的效果,在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();           }

解决方案 »

  1.   

    你gridview里就一个HiddenField1?
      

  2.   

    不明白你为什么要判断“行”的类型~~我想不需要判断行的类型,从出现的状况来看,应该是我的循环有问题,可我看不出来。/
    ///////////////////
    楼上给你说的应该是对的,如果不加判断的话第一行不是数据行而是DataControlRowType.Header,它里面是没有你的HiddenField控件的
    在循环的时候加上下面的语句:
    if(e.Row.RowType==DataControlRowType.DataRow)
    {
    GridViewRow   row=GridView1   .Rows   [i];  
                    iDCIDTemp=Convert.ToInt32   (((HiddenField   )row   .FindControl("HiddenField1")).Value.ToString   ())   ;  }
      

  3.   

    DropDownList,HiddenField 是放到模板列中的。
      

  4.   

    <script>window.alert("test");</script>
      

  5.   

    如何判断行的类型?按照g_lbz 的做法
     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”的定义
      

  6.   

    如何判断行的类型?按照g_lbz 的做法 有错误
     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”的定义
      

  7.   

    提示:“System.EventArgs”并不包含“Row”的定义
    ///////////
    你上面的代码在GridView的RowCreated事件中来写,写完以后直接绑定到你的控件的数据源上去。另外:不建议将SqlDataReader作为返回值,它是长链接的比较占用资源, return dr;  myconnection.Close();是有问题的,你已经Return了,后面的Close()根本不会执行,如果先Close()的话,你是没有办法返回SqlDataReader的  
      

  8.   

    public SqlDataReader ddliActiveId_Bind(object sender, EventArgs e)  
    、、、、、、、、、
    这个方法是你自己写的吧?里面的参数不要写成那样,没有什么用,如果需要传递,传递有意义的参数,不需要参数就不要写好了