gridview 编辑时生成下拉列表

一、首先在girdview的<EditItemTemplate>  </EditItemTemplate>中添加DropDownList

二、gridview 的RowDataBound事件中添加代码

     

if (e.Row.RowType == DataControlRowType.DataRow)
            {
                DropDownList ddlKind = e.Row.FindControl("ddlKind") as DropDownList;
                if (ddlKind != null)
                {
                    DataRowView dataRowView = e.Row.DataItem as DataRowView;
                    string strKind = dataRowView["kind"].ToString();
                    ListItem item = ddlKind.Items.FindByValue(strKind);
                    if (item != null)
                    {
                        ddlKind.SelectedIndex = -1;
                        item.Selected = true;
                    }
                }

                DropDownList ddlCategory = e.Row.FindControl("ddlCategory") as DropDownList;
                if (ddlCategory != null)
                {
                    ComFun.BindDropDownList(ddlCategory, new Category().GetAllCategory(), "categoryname", "categorysn");          //给DropDownList捆绑数据
                    ddlCategory.Items.Insert(0, new ListItem("", ""));
                    DataRowView dataRowView = e.Row.DataItem as DataRowView;
                    string strCategorySN = dataRowView["categorysn"].ToString();
                    ListItem item = ddlCategory.Items.FindByValue(strCategorySN);
                    if (item != null)
                    {
                        ddlCategory.SelectedIndex = -1;
                        item.Selected = true;
                    }
                }

                DropDownList ddlLocationName = e.Row.FindControl("ddlLocationName") as DropDownList;
                if (ddlLocationName != null)
                {
                    ComFun.BindDropDownList(ddlLocationName, new Location().GetAllLocation(), "locationname", "locationsn");   //给DropDownList捆绑数据
                    ddlLocationName.Items.Insert(0, new ListItem("", ""));
                    DataRowView dataRowView = e.Row.DataItem as DataRowView;
                    string strLocationSN = dataRowView["locationsn"].ToString();
                    ListItem item = ddlLocationName.Items.FindByValue(strLocationSN);
                    if (item != null)
                    {
                        ddlLocationName.SelectedIndex = -1;
                        item.Selected = true;
                    }
                }
            }

posted @ 2008-11-19 13:42  只想做好  阅读(2377)  评论(0)    收藏  举报