R R
R R

Reputation: 2956

templateField Control are lost while deleting the other columns in gridview

I have a gridview :

<asp:GridView ID="grdData" OnRowCreated="GridData_RowCreated" runat="server" DataKeyNames="ID" AutoGenerateColumns="false" OnRowDataBound="grdData_RowDataBound">

      <Columns>
             <asp:TemplateField>
               <HeaderTemplate>
              <asp:CheckBox ID="chkAll" AutoPostBack="true" OnCheckedChanged="OnCheckedChanged" runat="server" />
               </HeaderTemplate>
                  <ItemTemplate>
               <asp:CheckBox ID="editbtn" AutoPostBack="true" OnCheckedChanged="OnCheckedChanged" runat="server" />
                         </ItemTemplate>
                </asp:TemplateField>
       </Columns>
                </asp:GridView>

If I am trying to alter the gridview i.e when I am removing other columns except the existing template field the template field becomes empty:

int grdCount = grdData.Columns.Count;
            for (int i = 1; i < grdData.Columns.Count; i++)
            {
                grdData.Columns.RemoveAt(1);
            }

how to maintain the templateField Control while deleting the other columns in gridview.?

Please suggest. Thanks in advance.

Upvotes: 0

Views: 121

Answers (1)

bindi.raval
bindi.raval

Reputation: 262

//You have to bind your grid view after deleting column.

public void bindGridData()
{
  //Add your logic here which binds your grid.
   grdData.Databind();
}

//You have to call above method after you delete a column from grid view.

bindGridData();

Upvotes: 1

Related Questions