Latest

random

How to bind ArrayList element to GridView in C#

C# - Databinding ArrayList to GridView

There are different types of array:
  • Single Dimensional Array
  • Two Dimensional Array
  • Multi Dimensional Array

1. Design .aspx:

<head id="Head1" runat="server">    
    <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>

Code Behind:

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

        // Single Dimensional array
        private void BindGridview()
        {
            string[] arrlist = { "aspdotnet", "kunal", "India", "Delhi", "Gurgaon" };
            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();
        } 

 Output:
how to bind,arraylist element,gridview in c#.net,gridview .net, asp dot net

2. Design .aspx

<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>

Code Behind:
  // 2- Dimensional array
        private void BindGridview()
        {
            string[,] arrlist = {
                    {"kunal", "MCA"},
                    {"sachin","MBA"},
                    {"Nitin","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();
        }

Output:
3. Design .aspx

<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>

Code Behind Code:

  private void BindGridview()
        {
            string[,] arrlist = {
                    {"kunal", "MCA","25","Software Engineer"},
                    {"vineet","MCA","32","Team Leader"},
                    {"sachin","MBA","24","Marketing Executive"}
                   
                    };

            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();
        }

 Output: 

All Rights Reserved by Etechpulse © 2015 - 2016
Powered By Blogger

Contact Form

Name

Email *

Message *

Powered by Blogger.