Click here to Skip to main content
15,913,685 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am trying to add a search header within my gridview, however when I render the grdiview databound method, I get a blank page output on the client-side.

I am little unclear where I may be going wrong. I have also debugged the my databound gridview event and the rows parameter is shown to be null. However, I have tested my 'bindgrid' method and this does work (outputs user table).

C#
private void BindGrid()
       {
           string strConnString = ConfigurationManager.ConnectionStrings["cdwConnectionString"].ConnectionString;
           using (SqlConnection con = new SqlConnection(strConnString))
           {
               using (SqlCommand cmd = new SqlCommand())
               {
                   cmd.CommandText = "select top 20 u.[uID], u.[uForenames], u.[uSurname], u.[uCompany], u.[uEmailAddress], s.[sStartDate] from [dbo].[UserDetails]";
                   cmd.Connection = con;
                   con.Open();
                   GridView3.DataSource = cmd.ExecuteReader();
                   GridView3.DataBind();
                   con.Close();
               }
           }
       }


       protected void OnDataBound(object sender, EventArgs e)
       {
           GridViewRow row = new GridViewRow(0, 0, DataControlRowType.Header, DataControlRowState.Normal);
           if (row == null) { return; }

           else
           {
               if (GridView3.Rows.Count > 0) // Add this condition
               {
                   for (int i = 0; i < GridView3.Columns.Count; i++)
                   {
                       TableHeaderCell cell = new TableHeaderCell();
                       TextBox txtSearch = new TextBox();
                       txtSearch.Attributes["placeholder"] = GridView3.Columns[i].HeaderText;
                       txtSearch.CssClass = "search_textbox";
                       cell.Controls.Add(txtSearch);
                       row.Controls.Add(cell);
                   }
                   GridView3.HeaderRow.Parent.Controls.AddAt(1, row);

               }

           }
       }

ASP.NET
<asp:gridView ID="GridView3" HeaderStyle-BackColor="#3AC0F2" HeaderStyle-ForeColor="White"
   runat="server" AutoGenerateColumns="false" OnDataBound="OnDataBound">

   </asp:gridView>


Any further advice would be very much appreciated. Many thanks.
Posted

1 solution

 
Share this answer
 
Comments
miss786 20-Jan-15 7:39am    
Apology for the late response. I manage to solve the issue, using row bound event for creating header in my gridview. many thanks
Member 13686547 27-Feb-18 5:54am    
kk

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900