Showing posts with label Two Dimensional Array. Show all posts
Showing posts with label Two Dimensional Array. Show all posts

Bind Array Value To Dropdownlist Using in Asp.Net C#

Bind Array Value To Dropdownlist

Bind Array & 2D Array Values To Dropdownlist Using in Asp.Net C#.

                                DEMO


                             Download

                          HTML Coding

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>Bind Array Values To Dropdownlist Control Using in Asp.Net C#</title>
</head>
<body>
    <form id="form1" runat="server">
    <div align="center">
   Array <asp:DropDownList ID="DropDownList1" runat="server">
</asp:DropDownList>

    </div>
    </form>
</body>
</html>



                       C# Coding

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class DDLArray : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {

// Array

            string[] arr = new string[6] { "AUSTRIA", "BHUDAN", "CHINA", "EGYPT", "FRANCE", "GHANA" };
            DropDownList1.DataSource = arr;
            DropDownList1.DataBind();


// 2D Array

            string[,] arr = new string[3, 3] { { "AUSTRIA", "BHUDAN", "CHINA" }, { "EGYPT", "FRANCE", "GHANA" }, { "HUNGARY", "INDIA", "JAPAN" } };
            for (int i = 0; i < 2; i++)
            {
                for (int j = 0; j < arr.GetLength(1); j++)
                {
                    DropDownList1.Items.Add(arr[i, j]);
                    DropDownList1.DataBind();
                }
            }
        }
    }

}


First - Add New Web Form - Select Dropdownlist 

From toolbox





Next - Add Array Values To  Bind Dropdownlist  - 2D Array Values Add from Array Length








Two Dimensional Array or 2D Array in C#

Two Dimensional Array or 2D Array


First We Use Two Dimensional Array First is Row &  Second is Column i.e[2,3] Using in C#.

              C# Coding

Row Length


using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class Default3 : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        string[,] array = new string [2,3] { { "One", "Two","Three" }, { "Four", "Five","Six"} };
      
        for (int i = 0; i < 2; i++)

        {

            for (int j = 0; j <array.GetLength(0); j++)

            {

                Response.Write(array[i,j] + ",\r");

            }
        }
    }

}

 Output








Column Length

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class Default3 : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        string[,] array = new string [2,3] { { "One", "Two","Three" }, { "Four", "Five","Six"} };
      
        for (int i = 0; i < 2; i++)

        {

            for (int j = 0; j <array.GetLength(1); j++)

            {

                Response.Write(array[i,j] + ",\r");

            }
        }
    }
}


Output