2010-05-04 26 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(); 
} 

希望有所幫助。