Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Bind ArrayList to ListView Control in C#

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 1.28k
    Comment on it

    C# consists a ListView control which contains a list of items and the items can have different forms either number,text or images.

    To add columns in listview we can use:

        ListView.Columns.Add("EmpName", 1000);

    To understand it better,we will create a small windows application.

    STEP1:

    In Design: Form1.cs[Design]

    First add a button and a ListView control from your toolbox.Refer screenshot for design part;

    STEP-2:

    Now create a button click event in code behind file and write the following code:Here we are creating an arraylist for three fields empid,empname and empsalary and add these three items in listview.

    private void ShowListbutton_Click(object sender, EventArgs e)
            {
                string[,] ArrayList ={
                                    {"1","Shubham","70000"},
                                    {"2","Mayank","50000"},
                                    {"3","Deepika","10000"},
                                    {"4","Shikha","12000"},
                                    {"5","Mayuri","14000"}
                                     };
              BindlistView.Items.Clear();
    
             for (int i = 0; i < 5; i++)
                {
                    ListViewItem Bindlv = new ListViewItem();
                    Bindlv.Text = ArrayList[i, 0];
                    Bindlv.SubItems.Add(ArrayList[i, 1]);
                    Bindlv.SubItems.Add(ArrayList[i, 2]);
    
    
                    BindlistView.Items.Add(Bindlv);
    
                }
            }

    STEP-3:

    Now create a form_load event in code behind file and write the following code in that event:

    private void ListForm_Load(object sender, EventArgs e)
            {
                BindlistView.GridLines = true;
                BindlistView.View = View.Details;
    
                //Add Column Header
    
                BindlistView.Columns.Add("EmployeeID",100);
                BindlistView.Columns.Add("EmpName", 100);
                BindlistView.Columns.Add("EmpSalary", 100);
            }

    Now run the program,output will be: refer screenshot:

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: