In this article we will learn about How to Bind Array to GridView in ASP.Net. Here we discuss how we can bind array collections data inside GridView in ASP.Net.
In this example we will learn in how many ways we can bind arrays in GridView.
Binding GridView using One dimensional Array
C#
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//One Dimensional Array | |
string[] arr1D = { "Ram", "John", "Stewart", "Sham", "Tod", "Bryan" }; | |
gridView1.DataSource = arr1D; | |
gridView1.DataBind(); |
GridView
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<asp:GridView ID="gridView1" runat="server" CssClass="table-striped" | |
AutoGenerateColumns="true" AllowPaging="true" | |
PageSize="10" Caption="One Dimensional Array"> | |
</asp:GridView> |
Here Binding GridView using One dimensional Array is pretty much easy, we set GridView propert as AutoGenerateColumns="true" and in C# code created a sample Array.
Output
Binding GridView using Two dimensional Array
C#
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//Tow Dimensional Array | |
string[,] arr2D = { | |
{ "Ram", "India" }, | |
{ "John", "USA" }, | |
{ "Stewart", "UK" }, | |
{ "Sham", "India"}, | |
{ "Tod","USA" }, | |
{ "Bryan","Russia" } | |
}; | |
ArrayList arrList = new ArrayList(); | |
for (int i = 0; i < 5; i++) | |
{ | |
arrList.Add(new ListItem(arr2D[i, 0], arr2D[i, 1])); | |
} | |
gridView2.DataSource = arrList; | |
gridView2.DataBind(); |
GridView
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<asp:GridView ID="gridView2" runat="server" CssClass="table-striped" | |
AutoGenerateColumns="false" AllowPaging="true" | |
PageSize="10" Caption="Two Dimensional Array"> | |
<Columns> | |
<asp:BoundField ItemStyle-Width="180px" | |
DataField="Text" HeaderText="Name" /> | |
<asp:BoundField ItemStyle-Width="180px" | |
DataField="Value" HeaderText="Country" /> | |
</Columns> | |
</asp:GridView> |
Output
Binding GridView using Multi dimensional Array
C#
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//Multi Dimensional Array | |
string[,] arrMultiD = { | |
{ "Ram","Delhi", "India" }, | |
{ "John", "WS DC","USA" }, | |
{ "Stewart","London", "UK" }, | |
{ "Sham","Mumbai", "India"}, | |
{ "Tod","Florida","USA" }, | |
{ "Bryan","Kitet","Russia" } | |
}; | |
DataTable dt = new DataTable(); | |
dt.Columns.Add("Name", Type.GetType("System.String")); | |
dt.Columns.Add("City", Type.GetType("System.String")); | |
dt.Columns.Add("Country", Type.GetType("System.String")); | |
for (int i = 0; i < 6; i++) | |
{ | |
dt.Rows.Add(); | |
dt.Rows[dt.Rows.Count - 1]["Name"] = arrMultiD[i, 0]; | |
dt.Rows[dt.Rows.Count - 1]["City"] = arrMultiD[i, 1]; | |
dt.Rows[dt.Rows.Count - 1]["Country"] = arrMultiD[i, 2]; | |
} | |
gridView3.DataSource = dt; | |
gridView3.DataBind(); |
GridView
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<asp:GridView ID="gridView3" runat="server" CssClass="table-striped" | |
AutoGenerateColumns="false" AllowPaging="true" | |
PageSize="10" Caption="Multi Dimensional Array"> | |
<Columns> | |
<asp:BoundField ItemStyle-Width="150px" | |
DataField="Name" HeaderText="Name" /> | |
<asp:BoundField ItemStyle-Width="150px" | |
DataField="City" HeaderText="City" /> | |
<asp:BoundField ItemStyle-Width="150px" | |
DataField="Country" HeaderText="Country" /> | |
</Columns> | |
</asp:GridView> |
Output
</> Find the Source Code in Github
Summary