我有一個GridView
與一些預定義的Column
和一些生成的代碼。我的想法是,我根據用戶選擇的類別顯示Column
。我無法創建所有Column
,只是隱藏它們,因爲我不知道需要多少個Column
。我設法生成我需要的Column
,但是當我嘗試刪除生成的Column
s時,問題就開始了。情況如下:ASP.NET GridView列刪除
- 在第一次加載時,我看到
GridView
所有類別。 - 點擊
ListBox
我得到了我想要的結果。 1個額外的Column
已創建(RemoveAt
未被調用,因爲尚未創建其他Column
)。 - 點擊其他
ListBox
Item
我仍然得到我想要的結果。上次創建的Column
已刪除,並添加了新的Column
。 - 在這一點上,如果我點擊任何其他
ListBox
Item
在LicensesCategoriesListBox_SelectedIndexChanged
調試我看到所有的GridView
TemplateField
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);
}
}
我我的頭兩個(對於(USHORT I =(USHORT)gridView.Columns.Count後刪除列);}。爲什麼第一個'Columns'被刪除,然後呢? –
啊。那麼這不是你要找的答案。你是否檢查過你添加/刪除它們時按正確的順序存儲?在屏幕截圖中,索引0處有一列不應該在那裏? – Jono
Ups,這是我的錯誤。我在表格的開頭添加了帶有編輯按鈕的'Column',但刪除了'Page'文件看起來更乾淨。 –