Showing posts with label Array. Show all posts
Showing posts with label 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





Array in Asp.Net C# Examples

Array in Asp.Net C#

An array is used to store a collection of data, but it is often more useful to think of an array as a collection of variables of the same type stored at contiguous memory locations.


All arrays consist of contiguous memory locations. The lowest address corresponds to the first element and the highest address to the last element.

Array Types

Arrays can be divided into the following four categories.

       Single-dimensional arrays
       Multidimensional arrays or rectangular arrays
       Jagged arrays
       Mixed arrays.


Single - Dimensional Array


//---- Integer Datatype Array ----//



      int[] num = new int[5];

        num[0] = 0;
        num[1] = 1;
        num[2] = 2;
        num[3] = 3;
        num[4] = 4;

      Response.Write(num[3].ToString());


 OUTPUT



      ////   -------  String Datatype Array  ----- /////


        string[] week = new string[3] { "Sun", "Mon", "Tus" };
        for (int i = 0; i < week.Length; i++)
        {
            Response.Write(week[i] + ",");
        }



OUTPUT

                      
////   ------- For Loop   Integer Datatype Array   ----- /////


        int[] array = new int[4] { 10, 20, 30, 40 };

        array[0] = 10;
        array[1] = 20;
        array[2] = 30;
        array[3] = 40;

        for (int i = 0; i < array.Length; i++)
        {
            Response.Write(array[i] + ",");
        }



OUTPUT

 
////   -------  Boolean Datatype Array  ----- /////


        Array a = Array.CreateInstance(typeof(int), 5);

        a.SetValue(100, 0);
        a.SetValue(109, 1);
        a.SetValue(150, 2);
        a.SetValue(250, 3);
        a.SetValue(300, 4);

        int b = 5;

        Response.Write(Convert.ToBoolean(a.Length.Equals(b).ToString()));


           OUTPUT

 


         /// --- Sorting Array   ---  ///

        int[] numbers = { 9, 1, 5, 0, 9 };

        Array.Sort(numbers);

        foreach (int i in numbers)

            Response.Write (i+",");



            OUTPUT


 


        //  Create an Array with different data types //

        object[] Mix = new object[4];
        Mix[0] = 10;
        Mix[1] = "Dotnetdrizzles";
        Mix[2] = DateTime.Now;
        Mix[3] = DateTime.Now.AddDays(1).ToString();
        for (int i = 0; i < Mix.Length; i++)
        {
            Response.Write(Mix[i] + " , ");
        }


        
             OUTPUT


 
 ////   -------  Calculation in Integer Datatype Array  ----- /////


        int[] array11 = { 10, 20, 30 };

        Response.Write(array11[1] * 5);



             OUTPUT

                  
////   ------- Index  Find  String Datatype Array  ----- /////



        string[] array2 = new string[] { "A", "B", "C" };

        int Getindex = Array.IndexOf(array2, "B");

        Response.Write(Getindex.ToString());


        OUTPUT


             

            
////   -------   Array  Index Length ----- /////


        string[] array3 = new string[] { "A", "B", "C" };

        Response.Write(array3.Length + " "); // Empty Last Index Display

        Response.Write(array3.Length - 1); // Correct Index Length Display


         OUTPUT


             

 ////   -------  Resize Array Index Char Datatype Array  ----- /////


        char[] array4 = new char[5] { 'A', 'B', 'C', 'D', 'E' };
       
        Array.Resize(ref array4, 3);

        for (int i = 0; i < array4.Length; i++)
        {
            Response.Write(array4[i].ToString()+",");
        }

           
                 OUTPUT


                
////   -------  Foreach in Integer Datatype Array  ----- /////


        int[] array5 = new int[3] { 10, 30, 50 }; //array declaration
        foreach (int element in array5)
        {
            Response.Write(element);
        }


               OUTPUT

                
 ////   ------- Finding  Array  ----- /////


        string Check = "Drizzles";
        string[] stringArray = { "Dot", "Net", "Drizzles" };
        foreach (string x in stringArray)
        {
            if (x.Equals(Check))
            {
                Response.Write("Finding = " + x);
            }
             else
            {
                Response.Write("Finding = Not Equal");           
            }
        }


               OUTPUT