private  void  DataGrid1_EditCommand(object  source,  System.Web.UI.WebControls.DataGridCommandEventArgs  e)  
                       {  
//                                    this.DataGrid1.EditItemIndex=e.Item.ItemIndex;  
//                                    this.DataBindToDataGrid();  
 
 
                                   string  dept=((Label)(e.Item.cells[2].findcontrol("Label2"))).text;            //纪录开始的部门名称  
                                   DataGrid1.EditItemIndex=e.Item.ItemIndex;  
                                   this.DataBindToDataGrid();  
                                   DropDownList  DropDownList1=(DropDownList)(DataGrid1.Items[e.Item.ItemIndex].cells[2].controls[0]);//定义datagrid1上的dropdownlist  
                                   SqlConnection  con=db.CreateConnection();  
                                   con.Open();  
                                   SqlCommand  cmd=new  SqlCommand("select    distinct    unitSequence  from  test  ",con);  
                                   SqlDataReader  myreader=cmd.ExecuteReader();  
                                     
                                   DropDownList1.DataSource=myreader;  
                                   DropDownList1.DataTextField="unitSequence";  
                                   DropDownList1.DataBind();  
 
                                   myreader.Close();  
                                   con.Close();  
 
                                   DropDownList1.SelectedIndex=DropDownList1.Items.IndexOf(new    ListItem(dept,dept));//为了让dropdownlist显示正确的项,根据前面声明的dept,改变selectindex值  
                                     
                                     
 
 
                       }  
 
出现的问题是:运行是提示:C:\Inetpub\wwwroot\test\WebForm1.aspx.cs(115):  不可访问“System.Web.UI.WebControls.TableRow.cells”,因为它受保护级别限制  
 
 

解决方案 »

  1.   

    兄弟用gridview哦
    加上sqldatasource超级简单
      

  2.   

    dataview具体怎么用 能举个例子吗
      

  3.   

    zhongkeruanjian 我改后 ,但是出现下列问题 ,能解决吗?
    DropDownList DropDownList1=(DropDownList)(DataGrid1.Items[e.Item.ItemIndex].Cells[2].Controls[0]);//定义datagrid1上的dropdownlist
      

  4.   

    DropDownList DropDownList1=(DropDownList)(DataGrid1.Items[e.Item.ItemIndex].Cells[2].Controls[1]);
      

  5.   

    点击编辑按钮时 :出现问题:指定的转换无效,就是下面的代码出问题了,能解决吗?
    DropDownList DropDownList1=(DropDownList)(DataGrid1.Items[e.Item.ItemIndex].Cells[2].Controls[0]);//定义datagrid1上的dropdownlist
      

  6.   

    DropDownList DropDownList1=(DropDownList)(e.Item.Cells[2].Controls[1]);
      

  7.   

    哈哈,每个模板列中间加了个自动Literal小控件,所以编号是1,3,5,7 。这就是为什么
      

  8.   

    我加入模板列以后,为什么其他非模板列出问题了,运行时说:指定的参数已超出有效值的范围
    string TypeSequence=Convert.ToInt32(((TextBox)(e.Item.Cells[3].Controls[0])).Text.ToString());