GridView Row
In this post we will see, how to add multiple rows. The complete source code can be downloded from here
Download: GridView_Dynamic_rows.zip (3.34 kb)
As shown in figure. Here we are adding 12 new rows into existing Grid.
GridView code
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" CellPadding="4"
ForeColor="#333333" GridLines="None">
<Columns>
<asp:TemplateField>
<ItemTemplate>
<%# Container.DataItemIndex+1 %>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="First name">
<ItemTemplate>
<asp:Label ID="Label1" Text='<%#DataBinder.Eval(Container.DataItem,"FirstName") %>'
runat="server" Visible='<% # (DataBinder.Eval(Container.DataItem,"FirstName")!="")?true:false%>'></asp:Label>
<asp:TextBox ID="TextBox2" Text='<%# DataBinder.Eval(Container.DataItem,"LastName") %>'
Visible='<%# IsNullItem((DataBinder.Eval(Container.DataItem,"FirstName"))) ? true : false %>'
runat="server"></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Last name">
<ItemTemplate>
<asp:Label ID="Label2" runat="server" Visible='<% # (DataBinder.Eval(Container.DataItem,"LastName")!="")?true:false%>'
Text='<%#DataBinder.Eval(Container.DataItem,"LastName") %>'></asp:Label>
<asp:TextBox ID="txtLastName" Visible='<%# IsNullItem((DataBinder.Eval(Container.DataItem,"LastName"))) ? true : false %>'
runat="server"></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Email address">
<ItemTemplate>
<asp:Label ID="Label3" runat="server" Text='<%#DataBinder.Eval(Container.DataItem,"Email") %>'
Visible='<% # (DataBinder.Eval(Container.DataItem,"Email")!="")?true:false%>'></asp:Label>
<asp:TextBox ID="txtEmail" Visible='<%# IsNullItem((DataBinder.Eval(Container.DataItem,"Email"))) ? true : false %>'
runat="server"></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
</Columns>
<FooterStyle BackColor="#990000" Font-Bold="True" ForeColor="White" />
<RowStyle BackColor="#FFFBD6" ForeColor="#333333" />
<PagerStyle BackColor="#FFCC66" ForeColor="#333333" HorizontalAlign="Center" />
<SelectedRowStyle BackColor="#FFCC66" Font-Bold="True" ForeColor="Navy" />
<HeaderStyle BackColor="#990000" Font-Bold="True" ForeColor="White" />
<AlternatingRowStyle BackColor="White" />
</asp:GridView>
Based on wheather the Data is null or empty we are changing visibility of Labelboxes and TextBoxes.
C#.Net Code:
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
public partial class GridView_Add_Multiple_rows : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void LinkButton1_Click(object sender, EventArgs e)
{
DataTable dt = GridDataProvider.GetData();
for(int i=1; i< Convert.ToInt16(TextBox1.Text); i++)
{
DataRow dr = dt.NewRow();
dt.Rows.Add(dr);
}
GridView1.DataSource = dt;
GridView1.DataBind();
}
public bool IsNullItem(Object o)
{
if( o == System.DBNull.Value)
return true;
else return false;
}
protected void LinkButton2_Click(object sender, EventArgs e)
{
foreach(GridViewRow gr in GridView1.Rows)
{
string email = ((TextBox)gr.FindControl("txtEmail")).Text;
// Here you can call your Data base routines to store values
if(!string.IsNullOrEmpty(email))
Response.Write(email);
}
}
}
VB.Net code:
Imports System
Imports System.Data
Imports System.Configuration
Imports System.Collections
Imports System.Web
Imports System.Web.Security
Imports System.Web.UI
Imports System.Web.UI.WebControls
Imports System.Web.UI.WebControls.WebParts
Imports System.Web.UI.HtmlControls
Public Partial Class GridView_Add_Multiple_rows
Inherits System.Web.UI.Page
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs)
End Sub
Protected Sub LinkButton1_Click(ByVal sender As Object, ByVal e As EventArgs)
Dim dt As DataTable = GridDataProvider.GetData()
For i As Integer = 1 To Convert.ToInt16(TextBox1.Text) - 1
Dim dr As DataRow = dt.NewRow()
dt.Rows.Add(dr)
Next
GridView1.DataSource = dt
GridView1.DataBind()
End Sub
Public Function IsNullItem(ByVal o As [Object]) As Boolean
If o = System.DBNull.Value Then
Return True
Else
Return False
End If
End Function
Protected Sub LinkButton2_Click(ByVal sender As Object, ByVal e As EventArgs)
For Each gr As GridViewRow In GridView1.Rows
Dim email As String = DirectCast(gr.FindControl("txtEmail"), TextBox).Text
' Here you can call your Data base routines to store values
If Not String.IsNullOrEmpty(email) Then
Response.Write(email)
End If
Next
End Sub
End Class
IsNullItem function will check wheather data is null or not. It will help us to toggle visibility of Label and textBox.
Satalaj
Currently rated 5.0 by 1 people
- Currently 5/5 Stars.
- 1
- 2
- 3
- 4
- 5
Tags: