C# ListView MultiColumn - Add Items Dynamically,Set Selected to TextBox

| Page Views: 629 c# crud

A C# ListView tutorial here.Here we see how to add items to a multicolumn listview from textboxes.And set the selected items to a textboxes.

using System;
using System.Windows.Forms;

namespace csharp_ListView_With_ImagesNText
{
  public partial class Form1 : Form
  {
    public Form1()
    {
      InitializeComponent();
    }

    private void Form1_Load(object sender, EventArgs e)
    {
      //SET LV PROPIETIES
      listView1.View = View.Details;
      listView1.FullRowSelect = true;

      //ADD COLUMNS
      listView1.Columns.Add("Name", 150);
      listView1.Columns.Add("Position", 150);
      listView1.Columns.Add("Team", 150);
    }

    //ADD ROW
    private void add(string name,String pos,String team)
    {
      //ARRAY TO REP A ROW
      String[] row = { name, pos, team };

      ListViewItem item = new ListViewItem(row);

      //ADD ITEMS
      listView1.Items.Add(item);
    }

    private void addBtn_Click(object sender, EventArgs e)
    {
      add(nameTxt.Text, posTxt.Text, teamtxt.Text);

      //CLEAR TXT
      nameTxt.Text="";
      posTxt.Text="";
      teamtxt.Text = "";
    }
    //CLEAR
    private void clearBtn_Click(object sender, EventArgs e)
    {
      listView1.Items.Clear();

      //CLEAR TXT
      nameTxt.Text = "";
      posTxt.Text = "";
      teamtxt.Text = "";

    }
    //GET SELECTED ITEMS

    private void listView1_MouseClick_1(object sender, MouseEventArgs e)
    {
      String name = listView1.SelectedItems[0].SubItems[0].Text;
      String pos = listView1.SelectedItems[0].SubItems[1].Text;
      String team = listView1.SelectedItems[0].SubItems[2].Text;

      nameTxt.Text = name;
      posTxt.Text = pos;
      teamtxt.Text = team;
    }

  }
}

Best Regards.

How do You Feel after reading this?

According to scientists, we humans have 8 primary innate emotions: joy, acceptance, fear, surprise, sadness, disgust, anger, and anticipation. Feel free to tell us how you feel about this article using these emotes or via the comment section.

About Me.

After completing his Software Engineering bachelors program, Oclemy(Clement Ochieng) these days is a man of two lives. At day he works for a startup in Nairobi, Kenya. At night he works tirelessly on building ProgrammingWizards TV, a tv channel for student coders and this website to help share the source code. In between he practices Meditation and Self actualization to help him keep balance. He also likes going for long solo walks to connect more with nature.




Recommendations


What do You Think


Previous Post Next Post