Skip to main content

using arrayList as DataSource for Gridview



Let us suppose that you have four controls (TextBox ,DropDownList ,ListBox ) and your requirement is that when the user enters texts or make a selection all these items/values gets stored inside an ArrayList and then this Array List is used as the Data Source for a Gridview control.




<%@ Page Language="C#" AutoEventWireup="true" CodeFile="DynamicData.aspx.cs" Inherits="DynamicData" %>

<!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:TextBox ID="TextBox1" runat="server" /><br />
<asp:DropDownList ID="DropDownList1" runat="server">
<asp:ListItem>A</asp:ListItem>
<asp:ListItem>B</asp:ListItem>
<asp:ListItem>C</asp:ListItem>
</asp:DropDownList><br />
<asp:DropDownList ID="DropDownList2" runat="server">
<asp:ListItem>A</asp:ListItem>
<asp:ListItem>B</asp:ListItem>
<asp:ListItem>C</asp:ListItem>
</asp:DropDownList><br />
<asp:ListBox ID="ListBox1" runat="server">
<asp:ListItem>A</asp:ListItem>
<asp:ListItem>B</asp:ListItem>
<asp:ListItem>C</asp:ListItem>
</asp:ListBox><br />
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false">
<Columns>
<asp:BoundField DataField="Txt1" HeaderText="TextBox1" />
<asp:BoundField DataField="DDL1" HeaderText="DropDownList1" />
<asp:BoundField DataField="DDL2" HeaderText="DropDownList2" />
<asp:BoundField DataField="Lst1" HeaderText="ListBox1" />
</Columns>
</asp:GridView>
<asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Button" /></div>
</form>
</body>
</html>


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;
using System.Collections.Generic;

public partial class DynamicData : System.Web.UI.Page
{
private List<Row> Data
{
get
{
if (this.ViewState["Data"] == null)
{
this.ViewState["Data"] = new List<Row>();
}

return this.ViewState["Data"] as List<Row>;
}
}

protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
this.GetData();
}
}

private void GetData()
{
GridView1.DataSource = this.Data;
GridView1.DataBind();
}

protected void Button1_Click(object sender, EventArgs e)
{
Row obj = new Row();
obj.Txt1 = TextBox1.Text;
obj.DDL1 = DropDownList1.SelectedValue;
obj.DDL2 = DropDownList2.SelectedValue;
obj.Lst1 = ListBox1.SelectedValue;
this.Data.Add(obj);

this.GetData();
}



}
[Serializable()]
public class Row
{

private string _txt1;
private string _ddl1;
private string _ddl2;
private string _Lst1;

public string Lst1
{
get { return _Lst1; }
set { _Lst1 = value; }
}


public string DDL2
{
get { return _ddl2; }
set { _ddl2 = value; }
}


public string DDL1
{
get { return _ddl1; }
set { _ddl1 = value; }
}

public string Txt1
{
get { return _txt1; }
set { _txt1 = value; }
}

}

Comments

  1. Nice code. very useful. thank u.

    ReplyDelete
  2. Dude you rocks thanks

    its very good tutorial

    ReplyDelete
  3. thanx u a lot... Useful code, but command button didn't work in this gridview, i need deleting and ediing function...

    ReplyDelete
  4. Thanx u a lot. Useful code but command field (link or button) didn't work in this gridview. I need deleting and editing functions... wait your answer...

    ReplyDelete
  5. Thanks very much, it is just the starting point I needed.

    ReplyDelete
  6. hi ,can u please help on the above code.

    if i want to update or delete the row/rows

    Thanks hope u reply

    ReplyDelete
  7. Thanks is running very smooth

    ReplyDelete

Post a Comment

Popular posts from this blog