locked
How To Insert Heading In Gridview RRS feed

  • Question

  • User2033107836 posted

    Hello

    This is my Gridview1 Data

    Name BelongsTo Available
    Apple AA 5000
    Mango MNP 60
    Lemon AKS 800

    I have one more sql query for example : Select CustomerDetails From Table1 these query shows resultset like this

    Raja
    45 ABCDEFG Road
    Delhi

    I want my gridview1 like this

    Give me asp.net 2.0 C# Code

    Thanking You

    Wednesday, June 6, 2018 12:10 AM

Answers

  • User1724605321 posted

    Hi asp.ambur ,

    Suppose you have a datatable (get from sql database )which stores the header text :

    DataTable dt = new DataTable();
                dt.Columns.AddRange(new DataColumn[1] { new DataColumn("Field", typeof(string)) });
                dt.Rows.Add("Raja");
                dt.Rows.Add("45 ABCDEFG Road");
                dt.Rows.Add("Delhi");

    Firstly , add `OnRowCreated` event to your gridview :

    <asp:GridView ID="gvitems" runat="server" AutoGenerateColumns="false" OnRowCreated="gvitems_RowCreated">    

    Then add your header :

      protected void gvitems_RowCreated(object sender, GridViewRowEventArgs e)
            {
                DataTable dt = new DataTable();
                dt.Columns.AddRange(new DataColumn[1] { new DataColumn("Field", typeof(string)) });
                dt.Rows.Add("Raja");
                dt.Rows.Add("45 ABCDEFG Road");
                dt.Rows.Add("Delhi");
    
                if (e.Row.RowType == DataControlRowType.Header)
                {
                    for (int i = 0; i < dt.Rows.Count; i++)
                    {
                        GridViewRow row = new GridViewRow(i, -1, DataControlRowType.Header, DataControlRowState.Insert);
                        TableCell th = new TableHeaderCell();
                        th.Text = dt.Rows[i][0].ToString();
                        th.ColumnSpan = gvitems.Columns.Count;
                        row.Cells.Add(th);
                        gvitems.Controls[0].Controls.AddAt(i, row);
                    }
                   
                }
            }

    Best Regards,

    Nan Yu

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, June 6, 2018 6:42 AM
  • User1724605321 posted

    Hi asp.ambur ,

    If its three column then how to bind..

    Three column in header query ? Then try with :

      DataTable dt = new DataTable();
                dt.Columns.AddRange(new DataColumn[3] { new DataColumn("Field", typeof(string)), new DataColumn("Field1", typeof(string)), new DataColumn("Field2", typeof(string)) });
                dt.Rows.Add("Raja","1","2");
                dt.Rows.Add("45 ABCDEFG Road", "1", "2");
                dt.Rows.Add("Delhi", "1", "2");
    
                if (e.Row.RowType == DataControlRowType.Header)
                {
                    for (int i = 0; i < dt.Rows.Count; i++)
                    {
                        GridViewRow row = new GridViewRow(i, -1, DataControlRowType.Header, DataControlRowState.Insert);
                        for (int j = 0; j < dt.Columns.Count; j++)
                        {
                            TableCell th = new TableHeaderCell();
                            th.Text = dt.Rows[i][j].ToString();
                            //th.ColumnSpan = gvitems.Columns.Count;
                            row.Cells.Add(th);
                        }
                      
                        gvitems.Controls[0].Controls.AddAt(i, row);
                    }
                   
                }

    Best Regards,

    Nan Yu

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, June 6, 2018 7:00 AM

All replies

  • User1724605321 posted

    Hi asp.ambur ,

    Suppose you have a datatable (get from sql database )which stores the header text :

    DataTable dt = new DataTable();
                dt.Columns.AddRange(new DataColumn[1] { new DataColumn("Field", typeof(string)) });
                dt.Rows.Add("Raja");
                dt.Rows.Add("45 ABCDEFG Road");
                dt.Rows.Add("Delhi");

    Firstly , add `OnRowCreated` event to your gridview :

    <asp:GridView ID="gvitems" runat="server" AutoGenerateColumns="false" OnRowCreated="gvitems_RowCreated">    

    Then add your header :

      protected void gvitems_RowCreated(object sender, GridViewRowEventArgs e)
            {
                DataTable dt = new DataTable();
                dt.Columns.AddRange(new DataColumn[1] { new DataColumn("Field", typeof(string)) });
                dt.Rows.Add("Raja");
                dt.Rows.Add("45 ABCDEFG Road");
                dt.Rows.Add("Delhi");
    
                if (e.Row.RowType == DataControlRowType.Header)
                {
                    for (int i = 0; i < dt.Rows.Count; i++)
                    {
                        GridViewRow row = new GridViewRow(i, -1, DataControlRowType.Header, DataControlRowState.Insert);
                        TableCell th = new TableHeaderCell();
                        th.Text = dt.Rows[i][0].ToString();
                        th.ColumnSpan = gvitems.Columns.Count;
                        row.Cells.Add(th);
                        gvitems.Controls[0].Controls.AddAt(i, row);
                    }
                   
                }
            }

    Best Regards,

    Nan Yu

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, June 6, 2018 6:42 AM
  • User2033107836 posted

    Hello

    Thanks For Your Code

    If its three column then how to bind..

    For one column this perfect...

    Thank You

    Wednesday, June 6, 2018 6:55 AM
  • User1724605321 posted

    Hi asp.ambur ,

    If its three column then how to bind..

    Three column in header query ? Then try with :

      DataTable dt = new DataTable();
                dt.Columns.AddRange(new DataColumn[3] { new DataColumn("Field", typeof(string)), new DataColumn("Field1", typeof(string)), new DataColumn("Field2", typeof(string)) });
                dt.Rows.Add("Raja","1","2");
                dt.Rows.Add("45 ABCDEFG Road", "1", "2");
                dt.Rows.Add("Delhi", "1", "2");
    
                if (e.Row.RowType == DataControlRowType.Header)
                {
                    for (int i = 0; i < dt.Rows.Count; i++)
                    {
                        GridViewRow row = new GridViewRow(i, -1, DataControlRowType.Header, DataControlRowState.Insert);
                        for (int j = 0; j < dt.Columns.Count; j++)
                        {
                            TableCell th = new TableHeaderCell();
                            th.Text = dt.Rows[i][j].ToString();
                            //th.ColumnSpan = gvitems.Columns.Count;
                            row.Cells.Add(th);
                        }
                      
                        gvitems.Controls[0].Controls.AddAt(i, row);
                    }
                   
                }

    Best Regards,

    Nan Yu

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, June 6, 2018 7:00 AM