-1

I have a gridview, on the each column i want Search to implemented. I have implemented the code in the other aspx page and it is working fine. But when I implement here, the page is giving me error as Object reference not set to instance of an object. Please see the code of RowDataBound of GridView.

<asp:GridView ID="grdPostData" runat="server" Width="100%" border="1" Style="border: 1px solid #E5E5E5;" CellPadding="3" AutoGenerateColumns="False" AllowPaging="true" PageSize="10" CssClass="hoverTable" OnPageIndexChanging="grdPostData_PageIndexChanging" OnRowDataBound="grdPostData_RowDataBound">
                <AlternatingRowStyle BackColor="#CCCCCC" />
                <Columns>
                    <asp:BoundField DataField="cat_id" HeaderText="Title" ItemStyle-Width="50" ControlStyle-CssClass="k-grid td" Visible="false" />
                    <asp:BoundField DataField="ngo_id" HeaderText="Title" ItemStyle-Width="50" ControlStyle-CssClass="k-grid td" Visible="false" />
                    <asp:BoundField DataField="title" HeaderText="Title" ItemStyle-Width="50" ControlStyle-CssClass="k-grid td" />
                    <asp:BoundField DataField="description" HeaderText="Description" ItemStyle-Width="50" ControlStyle-CssClass="k-grid td" />
                    <asp:TemplateField HeaderText="Post Category" ItemStyle-Width="50">
                        <ItemTemplate>
                            <asp:DropDownList ID="ddlPostCategory" AppendDataBoundItems="true" runat="server"
                                AutoPostBack="True">
                                <asp:ListItem Text="Select" Value="0"></asp:ListItem>
                            </asp:DropDownList>
                        </ItemTemplate>
                    </asp:TemplateField>
                    <asp:BoundField DataField="active" HeaderText="Active" ItemStyle-Width="30" ControlStyle-CssClass="k-grid td" />
                </Columns>
            </asp:GridView>

Also see the CS code for the same:-

 protected void grdPostData_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        GridViewRow row = new GridViewRow(0, 0, DataControlRowType.Header, DataControlRowState.Normal);
        for (int i = 0; i < grdPostData.Columns.Count; i++)
        {
            TableHeaderCell cell = new TableHeaderCell();
            TextBox txtSearch = new TextBox();
            txtSearch.Attributes["placeholder"] = grdPostData.Columns[i].HeaderText;
            txtSearch.CssClass = "form-control HaydaBre";
            cell.Controls.Add(txtSearch);
            row.Controls.Add(cell);
        }
        grdPostData.HeaderRow.Parent.Controls.AddAt(1, row);
   }

I am getting the mentioned error at grdPostData.HeaderRow.Parent.Controls.AddAt(1, row);

Tried debugging the RowDataBound Command but, it is not getting inside the above mentioned line. Please help. Also let me know if you need anything else.

hud
  • 4,373
  • 10
  • 49
  • 124

1 Answers1

0

define your GridViewRow as GridViewRow row = new GridViewRow(-1, -1, DataControlRowType.Header, DataControlRowState.Normal); and then add it as e.Row.Parent.Controls.AddAt(1, row); to gridview

Akshay Randive
  • 374
  • 2
  • 9
  • yes it is working, but my all textboxes which are coming is getting messed up and the columns are skipping. I want that textbox to be in Header and it is taking as Rows. – hud Nov 29 '14 at 11:16
  • make one more change `GridViewRow row = new GridViewRow(-1, -1, DataControlRowType.Header, DataControlRowState.Normal);` and e.Row.Parent.Controls.AddAt(0, row); – Akshay Randive Nov 29 '14 at 11:23
  • I didnt got u, can u make change in the answer and post – hud Nov 29 '14 at 11:24
  • still it is getting widened like this http://www.imagesup.net/?di=16141726071411 I want to be like this http://www.imagesup.net/?di=12141726070616 – hud Nov 29 '14 at 11:33
  • put your code under if block `if (e.Row.RowType == DataControlRowType.Header)`{ //your code } – Akshay Randive Nov 29 '14 at 11:40
  • Thanks it worked perfectly..What was the issue can you explain please ? – hud Nov 29 '14 at 11:49
  • in event `grdPostData_RowDataBound` you dont get object of gridview so `grdPostData` was `null` – Akshay Randive Nov 29 '14 at 11:51
  • Ohhk. Thanks, got it. Marking your answer :) Have a great weeekend. – hud Nov 29 '14 at 11:51