Add New Line to Gridview

R

RC-

Hello, I am using .NET 2.0. I am using the GridView(s) that come standard
with .NET. Is there a way using the GridView to programmatically add a new
row to enter new data or is there a third party control that is available
that allows the adding of data rows?

Any and all help is greatly appreciated.

TIA,
RC-
 
A

Angel

Yes, but a lot depends on how much time you have and how much you are willing
to commit to get this done.

I created a project from scratch that fundamentally perform the task you
suggested the way I did it was to place a gridview and button on a page then
perform a little magic as show below

Essentially create a datatable with the rows as show then add a new row to
the datatable and set editindex to the row. Of course you need to know the
row number or perform some magic so your new row show on top then you can set
editindex = 0.

For my example I added a command field through the wizard but even this can
be done programmatically. The add button while external for the example
could easily made in to a template field to make it part of the grid.

The more you customize the harder it gets especially if you doing it for the
first time. If you doing this for something you have to deliver soon then I
suggest exploring the third party community for a solution. But if you have
time or just exploring have at it is fun.

Regards

ps
The example is not complete by any means it just shows the approach you
could take and is not a complete solution.


[The Page]

<%@ Page Language="VB" AutoEventWireup="false" CodeFile="Default.aspx.vb"
Inherits="_Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:GridView ID="GridView1" runat="server" CellPadding="4"
ForeColor="#333333"
GridLines="None">
<FooterStyle BackColor="#507CD1" Font-Bold="True"
ForeColor="White" />
<RowStyle BackColor="#EFF3FB" />
<Columns>
<asp:CommandField ShowEditButton="True" />
</Columns>
<PagerStyle BackColor="#2461BF" ForeColor="White"
HorizontalAlign="Center" />
<SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True"
ForeColor="#333333" />
<HeaderStyle BackColor="#507CD1" Font-Bold="True"
ForeColor="White" />
<EditRowStyle BackColor="#2461BF" />
<AlternatingRowStyle BackColor="White" />
</asp:GridView>
</div>
<asp:Button ID="Button1" runat="server" Text="Add" />
</form>
</body>
</html>


[code behind]
Imports System.Data
Partial Class _Default
Inherits System.Web.UI.Page

Protected Sub Page_Load(ByVal sender As Object, ByVal e As
System.EventArgs) Handles Me.Load
BindGridView()

End Sub
Private Sub BindGridView()
Dim da As New DataLayer.SqlDataAccess
Dim sql As String = "Select * from categories"
Dim dt As DataTable = da.GetDataTable(sql)
GridView1.DataSource = dt
GridView1.DataBind()
End Sub

Protected Sub Button1_Click(ByVal sender As Object, ByVal e As
System.EventArgs) Handles Button1.Click
Dim dt As datatable = CType(GridView1.DataSource, datatable)
Dim rw As DataRow = dt.NewRow
rw.Item(0) = "0000"
dt.Rows.Add(rw)

GridView1.EditIndex = 8
GridView1.DataBind()


End Sub

Protected Sub GridView1_RowCancelingEdit(ByVal sender As Object, ByVal e
As System.Web.UI.WebControls.GridViewCancelEditEventArgs) Handles
GridView1.RowCancelingEdit

End Sub

Protected Sub GridView1_RowUpdating(ByVal sender As Object, ByVal e As
System.Web.UI.WebControls.GridViewUpdateEventArgs) Handles
GridView1.RowUpdating

End Sub
End Class
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,583
Members
45,074
Latest member
StanleyFra

Latest Threads

Top