How to change data-table Column names value with first row of same data-table RRS feed

  • Question

  • User-471420332 posted

    I am getting data table(dt1) values from other data table(dt) like below

    DataTable dt1 = dt; 

    i am getting output like below in dt1


    I want output like below

     ChildrenBoth  CCTVcameras   AvailableOpenSpace    SufficientSpace
     ChildrenBoth  CCTVcameras   AvailableOpenSpace    SufficientSpace
          No             No             No                  No

    I don't want Column2,Column3,Column4,Column5 as header and header should be first row of datatable

    Thursday, March 28, 2019 11:58 AM

All replies

  • User475983607 posted

    Your question makes little to no sense.

    Thursday, March 28, 2019 12:17 PM
  • User-1038772411 posted

    You can do this on multiple levels. Like in the GridView with HeaderText

    <asp:GridView ID="GridView1" runat="server">
            <asp:BoundField HeaderText="MyHeader 1" />
            <asp:TemplateField HeaderText="MyHeader 2">

    Please follow below Link Its surely help you


    Thursday, March 28, 2019 1:51 PM
  • User-1174608757 posted

    Hi mazhar,

    According to your description,you could set the header text of Gridview in Gridview databind event, you could use below code to set its header text as the value of first row :

    GridView1.HeaderRow.Cells[i].Text = GridView1.Rows[0].Cells[i].Text.ToString();

    Here is the demo, I hope it could help you.


    <!DOCTYPE html>
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
        <form id="form1" runat="server">
                <asp:GridView ID="GridView1" runat="server" OnDataBound="GridView1_DataBound" ></asp:GridView>


      public partial class gridview : System.Web.UI.Page
            protected void Page_Load(object sender, EventArgs e)
                if (!IsPostBack)
    //bind source to Gridview string sql = "select * from ss "; GridView1.DataSource = SqlHelper.ExecuteDataTable(sql); GridView1.DataBind(); } } protected void GridView1_DataBound(object sender, EventArgs e) { // set the value of header text int count = 4; for (int i = 0; i < count; i++) { GridView1.HeaderRow.Cells[i].Text = GridView1.Rows[0].Cells[i].Text.ToString(); } } }

    You could see:

    Best Regards


    Friday, March 29, 2019 2:24 AM