Hy, I need to add a new row to a GridView. I need my GridView to have template columns with TextBoxes. I found an example but it doesn't display anything. No GridView. PS. I am not very familiar with C#, I know only VB.
Then I need to insert those fields into a DB table , but that's no problem.
Here is the code for the first example that I've tried:
For Default.aspx.vb
Imports System.Data
Partial Class _Default
Inherits System.Web.UI.Page
Protected Sub Page_Load(sender As Object, e As EventArgs)
If Not Page.IsPostBack Then
SetInitialRow()
End If
End Sub
Private Sub SetInitialRow()
Dim dt As New DataTable()
Dim dr As DataRow = Nothing
dt.Columns.Add(New DataColumn("RowNumber", GetType(String)))
dt.Columns.Add(New DataColumn("Column1", GetType(String)))
dt.Columns.Add(New DataColumn("Column2", GetType(String)))
dt.Columns.Add(New DataColumn("Column3", GetType(String)))
dr = dt.NewRow()
dr("RowNumber") = 1
dr("Column1") = String.Empty
dr("Column2") = String.Empty
dr("Column3") = String.Empty
dt.Rows.Add(dr)
ViewState("CurrentTable") = dt
Gridview1.DataSource = dt
Gridview1.DataBind()
End Sub
Private Sub AddNewRowToGrid()
Dim rowIndex As Integer = 0
If ViewState("CurrentTable") IsNot Nothing Then
Dim dtCurrentTable As DataTable = DirectCast(ViewState("CurrentTable"), DataTable)
Dim drCurrentRow As DataRow = Nothing
If dtCurrentTable.Rows.Count > 0 Then
For i As Integer = 1 To dtCurrentTable.Rows.Count
Dim box1 As TextBox = DirectCast(Gridview1.Rows(rowIndex).Cells(1).FindControl("TextBox1"), TextBox)
Dim box2 As TextBox = DirectCast(Gridview1.Rows(rowIndex).Cells(2).FindControl("TextBox2"), TextBox)
Dim box3 As TextBox = DirectCast(Gridview1.Rows(rowIndex).Cells(3).FindControl("TextBox3"), TextBox)
drCurrentRow = dtCurrentTable.NewRow()
drCurrentRow("RowNumber") = i + 1
dtCurrentTable.Rows(i - 1)("Column1") = box1.Text
dtCurrentTable.Rows(i - 1)("Column2") = box2.Text
dtCurrentTable.Rows(i - 1)("Column3") = box3.Text
rowIndex += 1
Next
dtCurrentTable.Rows.Add(drCurrentRow)
ViewState("CurrentTable") = dtCurrentTable
Gridview1.DataSource = dtCurrentTable
Gridview1.DataBind()
End If
Else
Response.Write("ViewState is null")
End If
SetPreviousData()
End Sub
Private Sub SetPreviousData()
Dim rowIndex As Integer = 0
If ViewState("CurrentTable") IsNot Nothing Then
Dim dt As DataTable = DirectCast(ViewState("CurrentTable"), DataTable)
If dt.Rows.Count > 0 Then
For i As Integer = 0 To dt.Rows.Count - 1
Dim box1 As TextBox = DirectCast(Gridview1.Rows(rowIndex).Cells(1).FindControl("TextBox1"), TextBox)
Dim box2 As TextBox = DirectCast(Gridview1.Rows(rowIndex).Cells(2).FindControl("TextBox2"), TextBox)
Dim box3 As TextBox = DirectCast(Gridview1.Rows(rowIndex).Cells(3).FindControl("TextBox3"), TextBox)
box1.Text = dt.Rows(i)("Column1").ToString()
box2.Text = dt.Rows(i)("Column2").ToString()
box3.Text = dt.Rows(i)("Column3").ToString()
rowIndex += 1
Next
End If
End If
End Sub
Protected Sub ButtonAdd_Click(sender As Object, e As EventArgs)
AddNewRowToGrid()
End Sub
End Class
Maybe you could add a couple exception handlers around your code to see what's the error.
Just a suggestion: why not make the initial DataTable dt public so that you can access it from the other functions? At the moment, when you want to add a row, you are reading every single row from the DataGridView, then adding it to another DataTable and
the binding it to the DataGridView? Correct me if I'm wrong, but I think will slow down the performance and gives you unnecessary work.
Maybe you could add a couple exception handlers around your code to see what's the error.
I found my mistake. Handles Me.Load was missing in:
Protected Sub Page_Load(sender As Object, e As EventArgs) Handles Me.Load
The example from
Swathi Nagaraj is working well, but I want to try what you said:
ladinku
Just a suggestion: why not make the initial DataTable dt public so that you can access it from the other functions? At the moment, when you want to add a row, you are reading every single row from the DataGridView, then adding it to another DataTable and
the binding it to the DataGridView? Correct me if I'm wrong, but I think will slow down the performance and gives you unnecessary work.
SparX23
Member
47 Points
111 Posts
Insert new row in GridView (VB)
Jul 17, 2012 01:25 PM|LINK
Hy, I need to add a new row to a GridView. I need my GridView to have template columns with TextBoxes. I found an example but it doesn't display anything. No GridView. PS. I am not very familiar with C#, I know only VB.
Here is the example: http://www.aspsnippets.com/Articles/Adding-Dynamic-Rows-in-ASP.Net-GridView-Control-with-TextBoxes.aspx
Another example that I found is: http://amitpatriwala.wordpress.com/2008/04/18/inserting-new-row-in-gridview-in-aspnet-20/
Then I need to insert those fields into a DB table , but that's no problem.
Here is the code for the first example that I've tried:
For Default.aspx.vb
Imports System.Data Partial Class _Default Inherits System.Web.UI.Page Protected Sub Page_Load(sender As Object, e As EventArgs) If Not Page.IsPostBack Then SetInitialRow() End If End Sub Private Sub SetInitialRow() Dim dt As New DataTable() Dim dr As DataRow = Nothing dt.Columns.Add(New DataColumn("RowNumber", GetType(String))) dt.Columns.Add(New DataColumn("Column1", GetType(String))) dt.Columns.Add(New DataColumn("Column2", GetType(String))) dt.Columns.Add(New DataColumn("Column3", GetType(String))) dr = dt.NewRow() dr("RowNumber") = 1 dr("Column1") = String.Empty dr("Column2") = String.Empty dr("Column3") = String.Empty dt.Rows.Add(dr) ViewState("CurrentTable") = dt Gridview1.DataSource = dt Gridview1.DataBind() End Sub Private Sub AddNewRowToGrid() Dim rowIndex As Integer = 0 If ViewState("CurrentTable") IsNot Nothing Then Dim dtCurrentTable As DataTable = DirectCast(ViewState("CurrentTable"), DataTable) Dim drCurrentRow As DataRow = Nothing If dtCurrentTable.Rows.Count > 0 Then For i As Integer = 1 To dtCurrentTable.Rows.Count Dim box1 As TextBox = DirectCast(Gridview1.Rows(rowIndex).Cells(1).FindControl("TextBox1"), TextBox) Dim box2 As TextBox = DirectCast(Gridview1.Rows(rowIndex).Cells(2).FindControl("TextBox2"), TextBox) Dim box3 As TextBox = DirectCast(Gridview1.Rows(rowIndex).Cells(3).FindControl("TextBox3"), TextBox) drCurrentRow = dtCurrentTable.NewRow() drCurrentRow("RowNumber") = i + 1 dtCurrentTable.Rows(i - 1)("Column1") = box1.Text dtCurrentTable.Rows(i - 1)("Column2") = box2.Text dtCurrentTable.Rows(i - 1)("Column3") = box3.Text rowIndex += 1 Next dtCurrentTable.Rows.Add(drCurrentRow) ViewState("CurrentTable") = dtCurrentTable Gridview1.DataSource = dtCurrentTable Gridview1.DataBind() End If Else Response.Write("ViewState is null") End If SetPreviousData() End Sub Private Sub SetPreviousData() Dim rowIndex As Integer = 0 If ViewState("CurrentTable") IsNot Nothing Then Dim dt As DataTable = DirectCast(ViewState("CurrentTable"), DataTable) If dt.Rows.Count > 0 Then For i As Integer = 0 To dt.Rows.Count - 1 Dim box1 As TextBox = DirectCast(Gridview1.Rows(rowIndex).Cells(1).FindControl("TextBox1"), TextBox) Dim box2 As TextBox = DirectCast(Gridview1.Rows(rowIndex).Cells(2).FindControl("TextBox2"), TextBox) Dim box3 As TextBox = DirectCast(Gridview1.Rows(rowIndex).Cells(3).FindControl("TextBox3"), TextBox) box1.Text = dt.Rows(i)("Column1").ToString() box2.Text = dt.Rows(i)("Column2").ToString() box3.Text = dt.Rows(i)("Column3").ToString() rowIndex += 1 Next End If End If End Sub Protected Sub ButtonAdd_Click(sender As Object, e As EventArgs) AddNewRowToGrid() End Sub End ClassAnd for Default.aspx
<%@ Page Title="Home Page" Language="VB" MasterPageFile="~/Site.Master" AutoEventWireup="false" CodeFile="Default.aspx.vb" Inherits="_Default" %> <asp:Content ID="HeaderContent" runat="server" ContentPlaceHolderID="HeadContent"> </asp:Content> <asp:Content ID="BodyContent" runat="server" ContentPlaceHolderID="MainContent"> <asp:gridview ID="Gridview1" runat="server" ShowFooter="true" AutoGenerateColumns="false"> <Columns> <asp:BoundField DataField="RowNumber" HeaderText="Row Number" /> <asp:TemplateField HeaderText="Header 1"> <ItemTemplate> <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox> </ItemTemplate> </asp:TemplateField> <asp:TemplateField HeaderText="Header 2"> <ItemTemplate> <asp:TextBox ID="TextBox2" runat="server"></asp:TextBox> </ItemTemplate> </asp:TemplateField> <asp:TemplateField HeaderText="Header 3"> <ItemTemplate> <asp:TextBox ID="TextBox3" runat="server"></asp:TextBox> </ItemTemplate> <FooterStyle HorizontalAlign="Right" /> <FooterTemplate> <asp:Button ID="ButtonAdd" runat="server" Text="Add New Row" /> </FooterTemplate> </asp:TemplateField> </Columns> </asp:gridview> </asp:Content>Swathi Nagar...
Member
254 Points
52 Posts
Re: Insert new row in GridView (VB)
Jul 17, 2012 01:35 PM|LINK
http://shibashishdotnetocean.blogspot.in/2012/02/adding-dynamic-rows-in-aspnet-gridview.html
ladinku
Member
194 Points
48 Posts
Re: Insert new row in GridView (VB)
Jul 17, 2012 02:19 PM|LINK
Maybe you could add a couple exception handlers around your code to see what's the error.
Just a suggestion: why not make the initial DataTable dt public so that you can access it from the other functions? At the moment, when you want to add a row, you are reading every single row from the DataGridView, then adding it to another DataTable and the binding it to the DataGridView? Correct me if I'm wrong, but I think will slow down the performance and gives you unnecessary work.
SparX23
Member
47 Points
111 Posts
Re: Insert new row in GridView (VB)
Jul 18, 2012 05:44 AM|LINK
I found my mistake. Handles Me.Load was missing in:
The example from Swathi Nagaraj is working well, but I want to try what you said:
Can you give me an example, please ?