2014-03-27 82 views
2

我有一個GridView與一些預定義的Column和一些生成的代碼。我的想法是,我根據用戶選擇的類別顯示Column。我無法創建所有Column,只是隱藏它們,因爲我不知道需要多少個Column。我設法生成我需要的Column,但是當我嘗試刪除生成的Column s時,問題就開始了。情況如下:ASP.NET GridView列刪除

  1. 在第一次加載時,我看到GridView所有類別。
  2. 點擊ListBox我得到了我想要的結果。 1個額外的Column已創建(RemoveAt未被調用,因爲尚未創建其他Column)。
  3. 點擊其他ListBoxItem我仍然得到我想要的結果。上次創建的Column已刪除,並添加了新的Column
  4. 在這一點上,如果我點擊任何其他ListBoxItemLicensesCategoriesListBox_SelectedIndexChanged調試我看到所有的GridViewTemplateField s爲空(http://tinypic.com/r/98vdkm/8)。

如果我評論部分gridView.Columns.RemoveAt(i - 1)一切工作正常,只是Column s不斷產生和生成。任何想法爲什麼我的中的所有我的TemplateField變爲空?

Page看起來像這樣:

<asp:ListBox ID="licensesCategoriesListBox" runat="server" AutoPostBack="true" OnSelectedIndexChanged="LicensesCategoriesListBox_SelectedIndexChanged" /> 
<asp:GridView ID="licencesGridView" runat="server" AutoGenerateColumns="False" Caption="Licencijos" DataKeyNames="id" ShowFooter="True"> 
    <Columns> 
     <asp:TemplateField HeaderText="Pavadinimas"> 
      <EditItemTemplate> 
       <asp:TextBox ID="licenceNameTextBox" runat="server" MaxLength="50" Text='<%# Bind("name") %>' /> 
      </EditItemTemplate> 
      <FooterTemplate> 
       <asp:TextBox ID="newLicenceNameTextBox" runat="server" MaxLength="50" ToolTip="Pavadinimas" /> 
      </FooterTemplate> 
      <ItemTemplate> 
       <span><%# Eval("name") %></span> 
      </ItemTemplate> 
     </asp:TemplateField> 
     <asp:TemplateField HeaderText="Kategorija"> 
       <EditItemTemplate> 
        <asp:DropDownList ID="licenceCategoryDropDownList" runat="server" /> 
       </EditItemTemplate> 
       <FooterTemplate> 
        <asp:DropDownList ID="newLicenceCategoryDropDownList" runat="server" ToolTip="Kategorija"> 
         <asp:ListItem Text="Pasirinkite kategoriją:" /> 
        </asp:DropDownList> 
       </FooterTemplate> 
       <ItemTemplate> 
        <span><%# Eval("category") %></span> 
       </ItemTemplate> 
     </asp:TemplateField> 
    </Columns> 
</asp:GridView> 

代碼:

protected void Page_Load(object sender, EventArgs e) { 
    if (!IsPostBack) { 
     FillLicences(ref licencesGridView); 
    } 
} 

protected void LicensesCategoriesListBox_SelectedIndexChanged(object sender, EventArgs e) { 
    FillLicences(ref licencesGridView, licensesCategoriesListBox.SelectedValue); /// Value is ID of category. 
} 

public void FillLicences(ref GridView gridView, string category = "") { 
    DataTable dataTable; 
    ushort categoryId; 
    if (UInt16.TryParse(category, out categoryId)) { 
     PutAdditionalColumns(ref gridView, categoryId); 
     dataTable = sqlCommands.GetLicences(categoryId); /// Returns DataTable [name], [category] and additional fields that I add in PutAdditionalColumns method. 
    } else { 
     dataTable = sqlCommands.GetAllLicences(); /// Returns DataTable with only [name], [category] 
    } 
    gridView.DataSource = dataTable; 
    gridView.DataBind(); 
} 

public void PutAdditionalColumns(ref GridView gridView, uint category) { 
    for (ushort i = (ushort)gridView.Columns.Count; i > 2; i--) { /// Removes columns at the end (created for other category) 
     gridView.Columns.RemoveAt(i - 1); 
    } 
    foreach (var pair in sqlCommands.GetLicencesCategoryAttributes(category)) { /// Takes additional field needed. 
     TemplateField field = new TemplateField(); /// New empty column. 
     field.AccessibleHeaderText = pair.Key.ToString(); 
     field.HeaderText = pair.Value; 
     gridView.Columns.Add(field); 
    } 
} 

回答

0

任何想法,爲什麼我所有的模板列的,寫在我的頁面變得 空?

因爲每個這些字段都被視爲一個列。你在代碼中刪除它們。

如果這是你的問題,你需要一些方法來將在aspx中定義的列與在代碼中創建的列分開。

我會用DataColumn.ExtendedProperties來做到這一點。

無論何時從代碼添加列,都將值添加到ExtendedProperties中,以顯示它是在代碼中創建的。

//... 
TemplateField field = new TemplateField(); /// New empty column. 
field.AccessibleHeaderText = pair.Key.ToString(); 
field.HeaderText = pair.Value; 
DataColumn ourNewColumn = gridView.Columns.Add(field); 
ourNewColumn.ExtendedProperties.Add("CreatedInCode", true); 
//... 

然後,當你來刪除它們,只有他們有這個屬性設置時刪除它們。 ){gridView.Columns.RemoveAt(I-1 - I; ** I> 2 **;

//... 
if(gridView.Columns[i - 1].ExtendedProperties.ContainsKey("CreatedInCode")) { 
    gridView.Columns.RemoveAt(i - 1); 
} 
//... 
+0

我我的頭兩個(對於(USHORT I =(USHORT)gridView.Columns.Count後刪除列);}。爲什麼第一個'Columns'被刪除,然後呢? –

+0

啊。那麼這不是你要找的答案。你是否檢查過你添加/刪除它們時按正確的順序存儲?在屏幕截圖中,索引0處有一列不應該在那裏? – Jono

+0

Ups,這是我的錯誤。我在表格的開頭添加了帶有編輯按鈕的'Column',但刪除了'Page'文件看起來更乾淨。 –