Link to home
Start Free TrialLog in
Avatar of mshaji
mshajiFlag for India

asked on

Add a row to the top of gridview

Dears,

I need to add a row to the top of a gridview same like a header in a grid. I used this code (VB)  but its coming to the bottom of gridview.


Thanks
Dim row as DataRow
row = DS.Tables(0).NewRow

row("Heading1") = "First Name"

Open in new window

Avatar of guvera
guvera
Flag of India image

Here's a small working example in C#:

ASPX

<%@ page language="C#" masterpagefile="~/MasterPages/Default.master" autoeventwireup="true"
        codefile="AddEmptyRowsAtTop.aspx.cs" inherits="GridView_AddEmptyRowAtTop" title="GridView: Add Empty Row At Top" %>
 
<asp:content id="Content1" contentplaceholderid="ContentPlaceHolder1" runat="Server">
        <asp:gridview id="GridView1" runat="server" autogeneratecolumns="false"
                ondatabound="GridView1_DataBound">
                <columns>
                        <asp:templatefield headertext="Letter">
                                <itemtemplate>
                                        <%# Container.DataItem %>
                                </itemtemplate>
                        </asp:templatefield>
                </columns>
        </asp:gridview>
</asp:content>

CODE-BEHIND
using System;
using System.Web.UI.WebControls;
 
public partial class GridView_AddEmptyRowAtTop : System.Web.UI.Page
{
        private void GetData()
        {
                GridView1.DataSource = new string[] { "A", "B", "C", "D", "E", "F", "G" };
                GridView1.DataBind();
        }
 
        protected void Page_Load(object sender, EventArgs e)
        {
                if (!this.IsPostBack)
                {
                        this.GetData();
                }
        }
 
        protected void GridView1_DataBound(object sender, EventArgs e)
        {
                GridViewRow row = new GridViewRow(
                        0,
                        0,
                        DataControlRowType.DataRow,
                        DataControlRowState.Alternate);
 
                foreach (DataControlField field in GridView1.Columns)
                {
                        TableCell cell = new TableCell();
                        cell.Text = "&nbsp;";
                        row.Cells.Add(cell);
                }
 
                GridView1.Controls[0].Controls.AddAt(1, row);
        }
}

Hope it may helpful to you.

Regards
Guvera
ASKER CERTIFIED SOLUTION
Avatar of Kiran Sonawane
Kiran Sonawane
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
In short

DataTable dataTable = new DataTable();
dataTable = (DataTable)gv.DataSource;
DataRow dataRow = dataTable.NewRow();
dataTable.Rows.InsertAt(dataRow, 0);
gv.DataSource = dataTable;
gv.DataBind();
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of mshaji

ASKER


I used this  ds.Tables(0).Rows.InsertAt(row,0)

but I am getting the error: This row already belongs to this table


regards
Are you using same row object?
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of mshaji

ASKER


Look at this code.

Please tell me if anything is wrong here.
Dim DSList1 As DataSet
        dbCon = New SqlConnection(ConnString)
        Dim DAList1 As SqlDataAdapter
        Dim sqlString1 As String
        
            sqlString1 = "select * from MachineLists where header_id = '" & Request.QueryString("HeaderId") & "' and sub_header_id = '" & Request.QueryString("SubHId") & "'"


 DAList1 = New SqlDataAdapter(sqlString1, dbCon)
        DAList1.SelectCommand.CommandTimeout = 300
        DSList1 = New DataSet
        DAList1.Fill(DSList1)


Dim row As DataRow

        row = DSList1.Tables(0).NewRow



        row("Caption1") = DSList.Tables(0).Rows(0).Item("Caption1").ToString()
        row("Caption2") = DSList.Tables(0).Rows(0).Item("Caption2").ToString()
        DSList1.Tables(0).Rows.Add(row)

          
        DSList1.Tables(0).Rows.InsertAt(row,0)

        GridView1.DataSource = DSList1.Tables(0)
        GridView1.ShowHeader = False

        GridView1.DataBind()

Open in new window

Avatar of mshaji

ASKER


Dears,

I have done it.

Much thanks for all . I appreciate it.

Regards

Shaji

What was the solution? I will really apreciate if you share with us.

Thanks
Avatar of mshaji

ASKER

Two times i tried to insert value to the variable row. So its throw a error message: This row already belongs to this table

I removed duplicate then issue solved.

Much thanks dears