mshaji
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
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"
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
In short
DataTable dataTable = new DataTable();
dataTable = (DataTable)gv.DataSource;
DataRow dataRow = dataTable.NewRow();
dataTable.Rows.InsertAt(da taRow, 0);
gv.DataSource = dataTable;
gv.DataBind();
DataTable dataTable = new DataTable();
dataTable = (DataTable)gv.DataSource;
DataRow dataRow = dataTable.NewRow();
dataTable.Rows.InsertAt(da
gv.DataSource = dataTable;
gv.DataBind();
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
I used this ds.Tables(0).Rows.InsertAt
but I am getting the error: This row already belongs to this table
regards
Are you using same row object?
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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()
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
Thanks
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
I removed duplicate then issue solved.
Much thanks dears
ASPX
<%@ page language="C#" masterpagefile="~/MasterPa
codefile="AddEmptyRowsAtTo
<asp:content id="Content1" contentplaceholderid="Cont
<asp:gridview id="GridView1" runat="server" autogeneratecolumns="false
ondatabound="GridView1_Dat
<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
{
GridViewRow row = new GridViewRow(
0,
0,
DataControlRowType.DataRow
DataControlRowState.Altern
foreach (DataControlField field in GridView1.Columns)
{
TableCell cell = new TableCell();
cell.Text = " ";
row.Cells.Add(cell);
}
GridView1.Controls[0].Cont
}
}
Hope it may helpful to you.
Regards
Guvera