Posted by : Anwar Hossain
Category : How to use grid view in asp.net c#

How to bind Single-Dimensional Arrays Data to gird view in asp.net using C#

Dear Viewers I will show how bind array data to grid view .In this case first I have created string type Single-Dimensional Arrays. Then I have also assigned the values to arrays. A data table is used to create virtual table .After creating data table I have passed the arrays values to the data table using for loop. Finally I have assigned the data table value to the grid view.

Bind array values to grid view in asp.net c#

HTML

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
  <title>Single dimension array example</title>
 
</head>
<body>
  <form id="form1" runat="server">
  <div>
  <asp:GridView ID="gvSingDimensionalArray" runat="server" Width="500px" AutoGenerateColumns="False"
  CellPadding="4" ForeColor="#333333" GridLines="None">
  <AlternatingRowStyle BackColor="White" />
  <Columns>
  <asp:BoundField DataField="StudentName" HeaderText="StudentName" ItemStyle-HorizontalAlign="Center">
  <ItemStyle HorizontalAlign="Center"></ItemStyle>
  </asp:BoundField>
  </Columns>
  <EditRowStyle BackColor="#2461BF" />
  <FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
  <HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
  <PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" />
  <RowStyle BackColor="#EFF3FB" />
  <SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" />
  <SortedAscendingCellStyle BackColor="#F5F7FB" />
  <SortedAscendingHeaderStyle BackColor="#6D95E1" />
  <SortedDescendingCellStyle BackColor="#E9EBEF" />
  <SortedDescendingHeaderStyle BackColor="#4870BE" />
  </asp:GridView>
  </div>
  </form>
</body>
</html>

CODE BEHIND

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
 
public partial class TestDesign_SingleDimensionArray : System.Web.UI.Page
{
  protected void Page_Load(object sender, EventArgs e)
  {
  if (!IsPostBack)
  {
 
  BindGridview();
 
 
  }
 
  }
 
 
  private void BindGridview()
  {
 
  //Declaring single dimensional array
  string[] StudentName = new string[5];
  StudentName[0] = "Fuzlul Roman";
  StudentName[1] = "Iqbal Hossain";
  StudentName[2] = "Jabed Hossain";
  StudentName[3] = "Noor Mohammad";
  StudentName[4] = "Khorshed Alam";
 
 
  DataTable dt = new DataTable();
  dt.Columns.Add("StudentName");
  for (int i = 0; i < StudentName.Count(); i++)
  {
  dt.Rows.Add();
 
  dt.Rows[i]["StudentName"] = StudentName[i].ToString();
  }
  gvSingDimensionalArray.DataSource = dt;
  gvSingDimensionalArray.DataBind();
  }
}
 

Out Put

single-dimensionarray-to-gridview



Realted Article Headline

Gridview datetime format example in ASP.Net c#
Displaying total column value in the Gridview Footer using asp.net c#.
How to create grid view paging in asp.net using c#
How to bind Multi-Dimensional Arrays Data to gird view in asp.net using C#
How to bind Two-Dimensional Arrays Data to gird view in asp.net using C#
How to bind Single-Dimensional Arrays Data to gird view in asp.net using C#
how to export gridview data to pdf in asp.net c#
how to export gridview data to word in asp.net using c#
how to export gridview data to excel in c#
How to delete row from grid view showing confirmation JavaScript using asp.net c#
How to delete data from grid view using row deleting event in asp.net c#
How to bind data into gridview using code behind in asp.net c#
How to scroll gird view overflow data in asp.net c#
How to Bind data into grid view control using asp.net c#

Article Category

How to create asp.net control dynamically
Learn HTML for beginner
DataList example in C Sharp
Mail sending in asp.net c#
State Management in ASP C #
Basic sql tutorial for Beginner
DataTable example in ASP.Net C#
How to use LINQ in ASP.NET C#
asp.net c # basic tutorial
How to use ajax toolkit in asp.net C#
How to use different types of validation control using asp.net c#
How to use grid view in asp.net c#
Protected by Copyscape Online Plagiarism Detection