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

mshajiAsked:
Who is Participating?
 
Kiran SonawaneConnect With a Mentor Project LeadCommented:
your_data_table.Rows.InsertAt(row, 0)
0
 
guveraCommented:
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
0
 
Kiran SonawaneProject LeadCommented:
In short

DataTable dataTable = new DataTable();
dataTable = (DataTable)gv.DataSource;
DataRow dataRow = dataTable.NewRow();
dataTable.Rows.InsertAt(dataRow, 0);
gv.DataSource = dataTable;
gv.DataBind();
0
Cloud Class® Course: Microsoft Office 2010

This course will introduce you to the interfaces and features of Microsoft Office 2010 Word, Excel, PowerPoint, Outlook, and Access. You will learn about the features that are shared between all products in the Office suite, as well as the new features that are product specific.

 
Ramkisan JagtapConnect With a Mentor Lead DeveloperCommented:
Dim row as DataRow
row = DS.Tables(0).NewRow

row("Heading1") = "First Name"
           
 ds.Tables(0).Rows.InsertAt(row,0) ' This will insert your nre row at oth Position.

0
 
mshajiAuthor Commented:

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

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


regards
0
 
Kiran SonawaneProject LeadCommented:
Are you using same row object?
0
 
Ramkisan JagtapConnect With a Mentor Lead DeveloperCommented:

Dim row As DataRow = ds.Tables(0).NewRow()
row("Heading1") = "First Name"
dt.Rows.InsertAt(row, 0)


Change below line
 Dim row As DataRow = ds.Tables(0).NewRow
to
Dim row As DataRow = ds.Tables(0).NewRow()
0
 
mshajiAuthor Commented:

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

0
 
mshajiAuthor Commented:

Dears,

I have done it.

Much thanks for all . I appreciate it.

Regards

Shaji

0
 
Ramkisan JagtapLead DeveloperCommented:
What was the solution? I will really apreciate if you share with us.

Thanks
0
 
mshajiAuthor Commented:
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
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.