0

The first rows in my gridview are blank rows and these blank rows are used for searching values in my gridview. I am adding a blank row on every column but i don't want to add any blank row on the first column which is the ID for my data. I only want add the blank rows starting from the 2nd column. How can i do this? here is my code behind:

protected void OnDataBound(object sender, EventArgs e)
{
    GridViewRow row = new GridViewRow(0, 0, DataControlRowType.Header, DataControlRowState.Normal);
    for (int i = 0; i < GV_EditProject.Columns.Count; i++)
    {
        TableHeaderCell cell = new TableHeaderCell();
        TextBox txtSearch = new TextBox();
        txtSearch.Attributes["placeholder"] = GV_Test.Columns[i].HeaderText;
        txtSearch.CssClass = "search_textbox";
        cell.Controls.Add(txtSearch);
        row.Controls.Add(cell);
    }
    GV_Test.HeaderRow.Parent.Controls.AddAt(1, row);
}

here is my aspx file

<Columns>
    <asp:BoundField DataField="ID" HeaderText="ID" ItemStyle-Width="5">
        <ItemStyle Width="5px" />
    </asp:BoundField>
4
  • If you are using template fields, it is easier to add textboxes in the markup. HeaderTemplate could be a good place to add search TextBox. Commented Feb 9, 2014 at 22:50
  • i am using bound field, can you show me how to add the header-template? Commented Feb 9, 2014 at 22:59
  • i just added the aspx code for my gridview in which i am using bound field. please look at my initial post. thanks Commented Feb 9, 2014 at 23:03
  • You can convert those boundfields to template field to add HeaderTemplate. See the example below. Commented Feb 9, 2014 at 23:21

1 Answer 1

3

I would suggest to use TemplateField for the columns with search textbox at header. For example, I have added one to "Comments" below :

<Columns>
    <asp:BoundField DataField="ID" HeaderText="ID" ItemStyle-Width="5">
        <ItemStyle Width="5px" />
    </asp:BoundField>

    <%-- This bound field is converted to Template Field --%>
    <%--<asp:BoundField DataField="COMMENTS" HeaderText="COMMENTS" ItemStyle-Width="5">
        <ItemStyle Width="5px" />
    </asp:BoundField>--%>

    <asp:TemplateField ItemStyle-Width="150px" HeaderText="COMMENTS">
        <ItemTemplate>
            <asp:Label ID="lblComm" runat="server" Text ='<%# Eval("COMMENTS")%>' ></asp:Label>
        </ItemTemplate>
        <HeaderTemplate>
            <asp:TextBox ID="txtSearchComment" runat="server" CssClass="search_textbox" Text=""></asp:TextBox>
        </HeaderTemplate>
    </asp:TemplateField>
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.