首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 网站开发 > asp.net >

GridView绑定DropdownList,该如何解决

2012-04-10 
GridView绑定DropdownList我现在做的是在页面加载的时候,就绑定DropdownList,然后也可以更新,可是我想要的

GridView绑定DropdownList
我现在做的是在页面加载的时候,就绑定DropdownList,然后也可以更新,可是我想要的效果是加载的时候,显示的是一个label,点编辑的时候这列才显示DropdownList,我现在这个如果显示的时候是label就出错。请问怎么做,我的代码如下:
------------------------------------------
HTML
<asp:GridView   ID= "GridView1 "   runat= "server "   OnRowCancelingEdit= "GridView1_RowCancelingEdit "   OnRowUpdating "GridView1_RowUpdating "   OnRowEditing= "GridView1_RowEditing "   AutoGenerateColumns= "False "   DataKeyNames= "EpId "   OnRowDataBound= "GridView1_RowDataBound ">
    <Columns>
        <asp:TemplateField   HeaderText= "所属大类 "   SortExpression= "goodsType ">
            <EditItemTemplate>
          <asp:DropDownList   ID= "DropDownList1 "runat= "server "> </asp:DropDownList>
            </EditItemTemplate>
            <ItemTemplate>
              <asp:DropDownList   ID= "DropDownList1 "     runat= "server "/>
              </ItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField   HeaderText= "操作 "   ShowHeader= "False ">
        <EditItemTemplate>
          <asp:LinkButton   ID= "LinkButton1 "   runat= "server "   CausesValidation= "True "   CommandName= "Update "Text= "更新 "> </asp:LinkButton>
            <asp:LinkButton   ID= "LinkButton2 "   runat= "server "   CausesValidation= "False "   CommandName= "Cancel "
            Text= "取消 "> </asp:LinkButton>
              </EditItemTemplate>
              <ItemTemplate>
              <asp:LinkButton   ID= "LinkButton1 "   runat= "server "   CausesValidation= "False "   CommandName= "Edit "Text= "编辑 "> </asp:LinkButton>
              </ItemTemplate>
              </asp:TemplateField>
              </Columns>
            </asp:GridView>
------------------------
CS代码中:
protected   void   GridView1_RowDataBound(object   sender,   GridViewRowEventArgs   e)
        {

                if   (e.Row.RowType   ==   DataControlRowType.DataRow)
                {
                        DropDownList   goodsType   =   (DropDownList)e.Row.FindControl( "DropDownList1 ");
                        con   =   new   SqlConnection(ConfigurationManager.ConnectionStrings[ "TestProviderConnectionString "].ConnectionString);


                        DataSet   ds   =   new   DataSet();
                        SqlDataAdapter   da   =   new   SqlDataAdapter( "select   *   from   goodsType ",con);
                        da.Fill(ds, "gt ");
                        goodsType.DataSource   =   ds.Tables[ "gt "];
                        goodsType.DataTextField   =   "goodsType ";
                        goodsType.DataValueField   =   "goodsId ";
                        goodsType.DataBind();
                }

        }

[解决办法]
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
GridView gv = (GridView)sender;
if(gv.Rows.Count ==0)
{
return;
}
DropDownList ddl = (DropDownList)gv.Rows[gv.Rows.Conut-1]FindControl( "DropDownList1 ");
if(ddl != null)
{
con = new SqlConnection(ConfigurationManager.ConnectionStrings[ "TestProviderConnectionString "].ConnectionString);
DataSet ds = new DataSet();
SqlDataAdapter da = new SqlDataAdapter( "select * from goodsType ",con);
da.Fill(ds, "gt ");
goodsType.DataSource = ds.Tables[ "gt "];
goodsType.DataTextField = "goodsType ";
goodsType.DataValueField = "goodsId ";
goodsType.DataBind();


}


拿去试试``````

热点排行