2010-05-04 52 views
1

我想使用GridView来显示ASP.NET中的组件列表。我试图让它在同一时间可编辑。其中一列是应在用户编辑行时从列表中选出的字符串。gridview dropbox

所以我尝试了以下内容:

  1. 转换的绑定列行一个ItemTemplate
  2. 保管箱添加到模板窗口在GridView
  3. 约束selectedItem设置字符串

在这一点上,我收到一个错误,因为列表项目还没有设置在保管箱中。所以我想我想知道的两件事是:

  1. 如何将保存箱中的项目分配给动态创建的选项列表?
  2. 如何让保存框只在正在编辑行时出现?

好了,所以我发现在Visual Studio中的 “EditItemTemplate模板” 字段中,回答#2。

现在我发现dropbox有一个数据源字段,可以链接到数据对象中的一个属性,并保存选项列表。

+0

向我们展示您的GridView代码和代码。 – TheGeekYouNeed 2010-05-04 22:19:53

+0

所以我对数据绑定和ASP很新颖......我真的很想了解如何在设计器中构建这些报告,而不是修复一个错误。我的代码隐藏真的只有我绑定的数据(工程)。标记生成的作品。我不知道如何在Visual Studio设计器中绑定Dropbox listitem选项。 – tbischel 2010-05-04 22:33:14

回答

0

在您的DropDownList中,您可以分配一个OnDataBinding事件,然后使用自定义数据填充您的DropDownList事件。

例子:

<Columns> 
    <asp:TemplateField> 
     <EditItemTemplate> 
      <asp:DropDownList ID="yourDropDownList" runat="server" 
       DataTextField="YourTextFieldName" DataValueField="YourValueFieldName" 
       OnDataBinding="yourDropDownList_DataBinding"></asp:DropDownList> 
     </EditItemTemplate> 
    </asp:TemplateField> 
</Columns> 

然后在后面的代码实现OnDataBinding:

protected void yourDropDownList_DataBinding(object sender, System.EventArgs e) 
{ 
    DropDownList ddl = (DropDownList)(sender); 
    // GetMyDropDownListData should return cached data so your not hitting your DB 
    // each time. You can customize the data for each row here. Use the Eval command 
    // to access the current rows databound values. 
    ddl.DataSource = GetMyDropDownListData(); 
    ddl.DataBind(); // Now all the options will be loaded 

    // Set the current field's selected value 
    ddl.SelectedValue = Eval("YourSelectedValueFieldName").ToString(); 
} 

希望有所帮助。