Here I will explain how to bind arraylist elements to gridview using asp.net.
Description:
In my application we bind some of elements to arraylist items after that we need to bind that elements to gridview upto that I don’t have idea to bind arraylist to gridview. I searched many websites and I found good materials regarding how to bind arraylist items to our gridview.
In arrays we have different types
1. 1) Single dimensional array
2. 2) Two dimensional array
3. 3) Multi dimensional array
Now I will explain each one how to bind all these arrays into gridview
Single Dimensional array
Design aspx page like this
<html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <title>Grdview with Arraylist</title> <style type="text/css"> .Gridview { font-family:Verdana; font-size:10pt; font-weight:normal; color:black; width:200px; } </style> </head> <body> <form id="form1" runat="server"> <div> <asp:GridView ID="gvarray" runat="server" CssClass="Gridview" AutoGenerateColumns="false" HeaderStyle-BackColor="#7779AF" HeaderStyle-ForeColor="White" Caption="Single-Dimensional Array"> <Columns> <asp:BoundField DataField="Name" HeaderText="Name"/> </Columns> </asp:GridView> </div> </form> </body> </html> |
In codebehind write the following code
protected void Page_Load(object sender, EventArgs e) { if(!IsPostBack) { BindGridview(); } } private void BindGridview() { string[] arrlist = { "aspdotnet", "Suresh", "Dasari","India","AndhraPradesh","Guntur" }; DataTable dt = new DataTable(); dt.Columns.Add("Name"); for (int i = 0; i < arrlist.Count();i++) { dt.Rows.Add(); dt.Rows[i]["Name"] = arrlist[i].ToString(); } gvarray.DataSource = dt; gvarray.DataBind(); } |
Demo
Use same aspx page but change gridview code in your aspx page
<div> <asp:GridView ID="gvarray" runat="server" CssClass="Gridview" AutoGenerateColumns="false" HeaderStyle-BackColor="#7779AF" HeaderStyle-ForeColor="White" Caption="Two-Dimensional Array"> <Columns> <asp:BoundField DataField="Name" HeaderText="Name"/> <asp:BoundField DataField ="Education" HeaderText="Education" /> </Columns> </asp:GridView> </div> |
After that write the following code in code behind for two dimensional arrays
protected void Page_Load(object sender, EventArgs e) { if(!IsPostBack) { BindGridview(); } } private void BindGridview() { string[,] arrlist = { {"Suresh", "B.Tech"}, {"Nagaraju","MCA"}, {"Mahesh","MBA"}, {"Mahendra","B.Tech"} }; DataTable dt = new DataTable(); dt.Columns.Add("Name"); dt.Columns.Add("Education"); for (int i = 0; i < arrlist.GetLength(0);i++) { dt.Rows.Add(); dt.Rows[i]["Name"] = arrlist[i,0].ToString(); dt.Rows[i]["Education"] = arrlist[i,1].ToString(); } gvarray.DataSource = dt; gvarray.DataBind(); } |
Demo
Now we will discuss about multi dimensional arrays code for multi demensional arrays same like our two dimensional arrays.
Multi-Dimensional arrays
Use same aspx page but change gridview code in your aspx page
<div> <asp:GridView ID="gvarray" runat="server" CssClass="Gridview" AutoGenerateColumns="false" HeaderStyle-BackColor="#7779AF" HeaderStyle-ForeColor="White" Caption="Multi-Dimensional Array"> <Columns> <asp:BoundField DataField="Name" HeaderText="Name"/> <asp:BoundField DataField ="Education" HeaderText="Education" /> <asp:BoundField DataField="Age" HeaderText="Age" /> <asp:BoundField DataField="Designation" HeaderText="Designation" /> </Columns> </asp:GridView> </div> |
After that write the following code in code behind for Multi dimensional arrays
protected void Page_Load(object sender, EventArgs e) { if(!IsPostBack) { BindGridview(); } } private void BindGridview() { string[,] arrlist = { {"Suresh", "B.Tech","24","Software Engineer"}, {"Nagaraju","MCA","27","Team Leader"}, {"Mahesh","MBA","22","Marketing Executive"}, {"Mahendra","B.Tech","21","Trainer"} }; DataTable dt = new DataTable(); dt.Columns.Add("Name"); dt.Columns.Add("Education"); dt.Columns.Add("Age"); dt.Columns.Add("Designation"); for (int i = 0; i < arrlist.GetLength(0);i++) { dt.Rows.Add(); dt.Rows[i]["Name"] = arrlist[i,0].ToString(); dt.Rows[i]["Education"] = arrlist[i,1].ToString(); dt.Rows[i]["Age"] = arrlist[i, 2].ToString(); dt.Rows[i]["Designation"] = arrlist[i, 3].ToString(); } gvarray.DataSource = dt; gvarray.DataBind(); } |
Demo
Download sample code attached
If you enjoyed this post, please support the blog below. It's FREE! Get the latest Asp.net, C#.net, VB.NET, jQuery, Plugins & Code Snippets for FREE by subscribing to our Facebook, Twitter, RSS feed, or by email. |
|||
|
|||
17 comments :
this is very simple how use arrays.Thank u great job.
Thanks! :)
A Job Well done ...! Really helped me .....!
Really the way u've explained is worthful....
thanks a lot man..
PERFECT!
Very Easy To Understand
WOW gracias !!!
Simply perfect!
Very nice article. However your example of a 3 dimentional array is actually only a 2 dimentional array with extra columns.
-1 dimensional array has one column and takes 1 parameter (ie. string[3])
-2 dimension has more then 1 column and takes 2 parameters (ie. string[3,2])
-3 dimension is like a cube. It contains more then one 2 dimension array. However the 2 dimensional arrays inside the cube have to be the same size throughout. It takes three parameters to do this (ie. string[3,2,4])
Check out this link that will show you visual
http://msdn.microsoft.com/en-us/library/02e7z943.aspx
Thanks.. simply but most effective
Nice job, but i want to create a gridview from an array
with arrays. The first array in the array contains the columnames
single dimensional always give error because it will not concede more than 6 data item. to solve this use this procedure:
private void BindGridView()
{
string[] array = { "pakistan", "china", "indonasia", "malaysia", "germany", "dutch", "italt" };
DataTable dt= new DataTable();
dt.Columns.Add("names");
int count=0;
for (int i = 0; i <= array.Count(); i++)
{
dt.Rows.Add();
dt.Rows[i]["names"] = array[i].ToString();
count++;
if (count == array.Count())
{
break;
}
}
gvarray.DataSource = dt;
gvarray.DataBind();
}
single dimensional always give error because it will not concede more than 6 data item. to solve this use this procedure:
private void BindGridView()
{
string[] array = { "pakistan", "china", "indonasia", "malaysia", "germany", "dutch", "italt" };
DataTable dt= new DataTable();
dt.Columns.Add("names");
int count=0;
for (int i = 0; i <= array.Count(); i++)
{
dt.Rows.Add();
dt.Rows[i]["names"] = array[i].ToString();
count++;
if (count == array.Count())
{
break;
}
}
gvarray.DataSource = dt;
gvarray.DataBind();
}
This post is truly inspirational.
The problem is I can not use the asp.net.
hey suresh u such a darling guy...i mean u awsome yrr
ur post are great plz keep it up..and save fresher's assess.
thanks buddy...cheers
Easy to Understand...
Note: Only a member of this blog may post a comment.