locked
First Column And Second Column Data As Top Row In Gridview RRS feed

  • Question

  • User2033107836 posted

    Hello

    This is my gridview

    <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataKeyNames="ID"
                    OnRowDataBound="GridView1_RowDataBound"
                    OnRowCreated="GridView1_RowCreated"
                    EnableModelValidation="True">
                     <Columns>
                        <asp:BoundField DataField="RefNo" HeaderText="RefNo" SortExpression="RefNo" />
                         <asp:BoundField DataField="CustomerName" HeaderText="CustomerName" SortExpression="CustomerName" />
                        <asp:BoundField DataField="ItemName" HeaderText="ItemName" SortExpression="ItemName" />
                        <asp:BoundField DataField="Qty" HeaderText="Qty" SortExpression="Qty" />
                        <asp:BoundField DataField="Price" HeaderText="Price" SortExpression="Price" />
                    </Columns>
                </asp:GridView>

    my code behind 

     protected void Page_Load(object sender, EventArgs e)
            {
                if (!IsPostBack)
                {
                    BindGridView();
                }
            }
    
            protected void BindGridView()
            {
                DataTable dt = new DataTable();
                using (SqlConnection con = new SqlConnection(constr))
                {
                    string cmdtext = "select ID, RefNo, CustomerName, ItemName, Price, Qty from TestTable2";
                    using (SqlCommand cmd = new SqlCommand(cmdtext, con))
                    {
                        con.Open();
                        SqlDataAdapter sda = new SqlDataAdapter(cmd);
                        sda.Fill(dt);
                        GridView1.DataSource = dt;
                        GridView1.DataBind();
                    }
                }
            }

    My output is coming like this below in gridview

    Ref No Customer Name Item Name Price Qty
    R1 ABC Apple 1 2
    R1 ABC Mango 2 5
    R1 ABC Lemon 5 9
    R1 ABC Mango 5 9

    But, I want output like this below

    Ref No : R1
    Customer Name : ABC
    SI.No Item Name Price Qty
    1 Apple 1 2
    2 Mango 2 5
    3 Lemon 5 9
    4 Mango 5 9
    Monday, December 11, 2017 10:31 AM

Answers

  • User-1838255255 posted

    Hi asp.ambur,

    According to your needs, you could get the data from the database, you will use datatable to fill it,  about how to get the value from the datatable, please check the following sample code:

    Sample Code:

    protected void Page_Load(object sender, EventArgs e)
            {
                DataTable table = new DataTable();
                table.Columns.Add(new DataColumn("refno", typeof(string)));
                table.Columns.Add(new DataColumn("CusName", typeof(string)));
                table.Columns.Add(new DataColumn("ItemName", typeof(string)));
                table.Rows.Add("R1", "ABC", "Apple");
                table.Rows.Add("R1", "ABC", "Apple");
                table.Rows.Add("R1", "ABC", "Mango");
                table.Rows.Add("R1", "ABC", "Apple");
    
                var refno = table.Rows[0][0].ToString();
                var CusName = table.Rows[0][1].ToString();
                Response.Write("refno: " + refno + "<br/>" + "CusName: " + CusName);
            }

     Best Regards,

    Eric Du 

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, December 15, 2017 2:42 AM

All replies

  • User-1838255255 posted

    Hi asp.ambur,

    According to your description and needs, i make a modify based on your code, please check: 

    Sample Code:

    <body>
        <form id="form1" runat="server">
            <div>
                <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
                    EnableModelValidation="True" OnDataBound="GridView1_DataBound">
                    <Columns>
                        <asp:BoundField DataField="SlNo" HeaderText="Sl.No" SortExpression="SlNo" />
                        <asp:BoundField DataField="ItemName" HeaderText="ItemName" SortExpression="ItemName" />
                        <asp:BoundField DataField="Qty" HeaderText="Qty" SortExpression="Qty" />
                        <asp:BoundField DataField="Price" HeaderText="Price" SortExpression="Price" />
                    </Columns>
                </asp:GridView>
            </div>
        </form>
    </body>
    
    protected void Page_Load(object sender, EventArgs e)
            {
                if (!this.IsPostBack)
                {
                    DataTable dt = new DataTable();
                    dt.Columns.AddRange(new DataColumn[4] { new DataColumn("SlNo"), new DataColumn("ItemName"), new DataColumn("Qty"), new DataColumn("Price") });
                    dt.Rows.Add(1, "Apple", 1, 2);
                    dt.Rows.Add(2, "Apple", 2, 5);
                    dt.Rows.Add(3, "Mango", 5, 9);
                    dt.Rows.Add(4, "Apple", 5, 9);
                    GridView1.DataSource = dt;
                    GridView1.DataBind();
                }
            }
    
            protected void GridView1_DataBound(object sender, EventArgs e)
            {
                // suppose RefNo value from database.
                // suppose CusName value from database.
                string refno = "R1";
                string CusName = "ABC";
    
    
                GridViewRow row = new GridViewRow(0, 0, DataControlRowType.Header, DataControlRowState.Normal);
                TableHeaderCell cell = new TableHeaderCell();
                cell.Text = "Ref No:" + refno;
                cell.ColumnSpan = 4;
                row.Controls.Add(cell);
                GridView1.HeaderRow.Parent.Controls.AddAt(0, row);
    
                GridViewRow newrow = new GridViewRow(0, 0, DataControlRowType.Header, DataControlRowState.Normal);
                TableHeaderCell newcell = new TableHeaderCell();
                newcell = new TableHeaderCell();
                newcell.ColumnSpan = 4;
                newcell.Text = "Customer Name:" + CusName;
                newrow.Controls.Add(newcell);
                GridView1.HeaderRow.Parent.Controls.AddAt(0, newrow);
            }

    Result:

    Best Regards,

    Eric Du 

    Tuesday, December 12, 2017 3:30 AM
  • User2033107836 posted

    Hello Eric

    ABC

    R1

    Comes from sql table 

    so how to show that in row

    Wednesday, December 13, 2017 6:58 AM
  • User-1838255255 posted

    Hi asp.ambur,

    According to your needs, you could get the data from the database, you will use datatable to fill it,  about how to get the value from the datatable, please check the following sample code:

    Sample Code:

    protected void Page_Load(object sender, EventArgs e)
            {
                DataTable table = new DataTable();
                table.Columns.Add(new DataColumn("refno", typeof(string)));
                table.Columns.Add(new DataColumn("CusName", typeof(string)));
                table.Columns.Add(new DataColumn("ItemName", typeof(string)));
                table.Rows.Add("R1", "ABC", "Apple");
                table.Rows.Add("R1", "ABC", "Apple");
                table.Rows.Add("R1", "ABC", "Mango");
                table.Rows.Add("R1", "ABC", "Apple");
    
                var refno = table.Rows[0][0].ToString();
                var CusName = table.Rows[0][1].ToString();
                Response.Write("refno: " + refno + "<br/>" + "CusName: " + CusName);
            }

     Best Regards,

    Eric Du 

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, December 15, 2017 2:42 AM