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”,因为它受保护级别限制
加上sqldatasource超级简单
DropDownList DropDownList1=(DropDownList)(DataGrid1.Items[e.Item.ItemIndex].Cells[2].Controls[0]);//定义datagrid1上的dropdownlist
DropDownList DropDownList1=(DropDownList)(DataGrid1.Items[e.Item.ItemIndex].Cells[2].Controls[0]);//定义datagrid1上的dropdownlist
string TypeSequence=Convert.ToInt32(((TextBox)(e.Item.Cells[3].Controls[0])).Text.ToString());