0

i have a grid views that generated columns through the code, and also ItemTemplate field from aspx, how can i clear columns generated from the code without clearing the template fields?

code from my previous post

TemplateField templateField = new TemplateField();
TemplateField uid = new TemplateField();
uid.HeaderText = "userid";
uid.ItemTemplate = new AddItemTemplate(ListItemType.Item, "userid");
GridView1.Columns.Add(uid);
for (int i = 0; i < dt.Columns.Count; i++)
{
   BoundField boundField = new BoundField();
   if (dt.Columns[i].ColumnName.ToString() != "userid")
{
  boundField.DataField = dt.Columns[i].ColumnName.ToString();
  boundField.HeaderText = dt.Columns[i].ColumnName.ToString();
  GridView1.Columns.Add(boundField);
}
}

aspx

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false">
    <Columns>
        <asp:TemplateField>
            <ItemTemplate>
                <tr>
                    <td>
                        <div id="div<%# Eval("userid") %>" >
                            <asp:GridView ID="GridView2" AllowSorting="true" >
                                <Columns>
                                    --code--
                                </Columns>
                            </asp:GridView>
                        </div>
                    </td>
                </tr>
            </ItemTemplate>
        </asp:TemplateField>
    </Columns>
</asp:GridView>
Noora
  • 319
  • 3
  • 10
  • 26
  • Do you wish to remove/clear columns or data? – KV Prajapati Aug 01 '12 at 07:24
  • actually i need to clear data from the generated columns, but if not possible then i need to remove them – Noora Aug 01 '12 at 07:35
  • i modified my post, by adding code from my previous post – Noora Aug 01 '12 at 07:47
  • So you want to clear `GridView2` datasource. Isn't it? – KV Prajapati Aug 01 '12 at 07:53
  • i need to clear the content of the grid view without removing the ItemTemplate field, because i am facing problem, when i run the grid view first time it work perfectly, but when again select variables from the form to run it i am getting error that the grid view still contain columns from previous run – Noora Aug 01 '12 at 08:06

1 Answers1

0

1) You can try rebinding to a different datasource altogether. One that returns all custom fields & other that returns only the Id

2) Easier way is to just hide the columns like YourGridView.Columns[0].Visible = false;

Zo Has
  • 12,599
  • 22
  • 87
  • 149