Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Add a row to the top of gridview

Posted on 2011-10-12
11
Medium Priority
?
351 Views
Last Modified: 2012-05-12
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

0
Comment
Question by:mshaji
  • 4
  • 3
  • 3
  • +1
11 Comments
 
LVL 8

Expert Comment

by:guvera
ID: 36954633
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
 
LVL 17

Accepted Solution

by:
sonawanekiran earned 668 total points
ID: 36954639
your_data_table.Rows.InsertAt(row, 0)
0
 
LVL 17

Expert Comment

by:sonawanekiran
ID: 36954656
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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
LVL 12

Assisted Solution

by:Ramkisan Jagtap
Ramkisan Jagtap earned 1332 total points
ID: 36954718
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
 

Author Comment

by:mshaji
ID: 36954726

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
 
LVL 17

Expert Comment

by:sonawanekiran
ID: 36954744
Are you using same row object?
0
 
LVL 12

Assisted Solution

by:Ramkisan Jagtap
Ramkisan Jagtap earned 1332 total points
ID: 36954772

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
 

Author Comment

by:mshaji
ID: 36954845

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
 

Author Comment

by:mshaji
ID: 36954863

Dears,

I have done it.

Much thanks for all . I appreciate it.

Regards

Shaji

0
 
LVL 12

Expert Comment

by:Ramkisan Jagtap
ID: 36954868
What was the solution? I will really apreciate if you share with us.

Thanks
0
 

Author Comment

by:mshaji
ID: 36960955
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

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

One of the pain points with developing AJAX, JavaScript, JQuery, and other client-side behaviors is that JavaScript doesn’t allow for cross domain request for pulling content. For example, JavaScript code on www.johnchapman.name could not pull conte…
Introduction This article shows how to use the open source plupload control to upload multiple images. The images are resized on the client side before uploading and the upload is done in chunks. Background I had to provide a way for user…
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…
This lesson discusses how to use a Mainform + Subforms in Microsoft Access to find and enter data for payments on orders. The sample data comes from a custom shop that builds and sells movable storage structures that are delivered to your property. …
Suggested Courses
Course of the Month21 days, 7 hours left to enroll

810 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question