我有一个网格,我通过 aspx 页面将几列添加到模板字段中。
<asp:TemplateField>
<ItemTemplate>
<asp:LinkButton ID="lnkBtnDown" runat="server" CommandName="Download" Text="Download"></asp:LinkButton>
<asp:LinkButton ID="LnkBtnMan" runat="server" CommandName="Manual" Text="Manual"
Style="margin-left: 10px"></asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
网格显示来自表格的搜索结果,并且有几列我必须隐藏,例如文件名列等,但我确实需要数据......所以我使用 css 样式隐藏它们。
private void generateSearchGrid(DataTable dt)
{
if (dt == null || dt.Rows.Count == 0)
return;
dgvSearchResults.DataSource = dt;
dgvSearchResults.AutoGenerateColumns = false;
BoundField bfName = new BoundField();
bfName.DataField = dt.Columns["OsmProjectName"].ToString();
bfName.HeaderText = "Project Name";
BoundField bfProjID = new BoundField();
bfProjID.DataField = dt.Columns["OsmProjectID"].ToString();
bfProjID.HeaderText = "ID";
BoundField bfProjFile = new BoundField();
bfProjFile.DataField = dt.Columns["OsmProjectFile"].ToString();
bfProjFile.HeaderText = "Project File";
BoundField bfProjManual = new BoundField();
bfProjManual.DataField = dt.Columns["OsmProjectManual"].ToString();
bfProjManual.HeaderText = "Project Manual";
BoundField bfProjType = new BoundField();
bfProjType.DataField = dt.Columns["OsmProjectType"].ToString();
bfProjType.HeaderText = "Project Type";
dgvSearchResults.Columns.Add(bfProjID);
dgvSearchResults.Columns.Add(bfName);
dgvSearchResults.Columns.Add(bfProjType);
// WARNING : Keep these two at the end all the time in the same order.
dgvSearchResults.Columns.Add(bfProjFile);
dgvSearchResults.Columns.Add(bfProjManual);
dgvSearchResults.DataBind();
// Assigning a css where display has been set to none.
bfProjManual.HeaderStyle.CssClass = "hiddenCols";
bfProjID.HeaderStyle.CssClass = "hiddenCols";
bfProjFile.HeaderStyle.CssClass = "hiddenCols";
bfProjManual.ItemStyle.CssClass = "hiddenCols";
bfProjID.ItemStyle.CssClass = "hiddenCols";
bfProjFile.ItemStyle.CssClass = "hiddenCols";
}
在每个搜索按钮单击事件开始时,我刷新网格,
dgvSearchResults.DataSource = null;
dgvSearchResults.DataBind();
dgvSearchResults.Columns.Clear();
但这会清除所有列,包括我在 aspx 页面中绑定的列……正如预期的那样。如果我不清除它们,搜索结果中的列就会不断堆积。
有没有办法只清除动态绑定的列?