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

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. This feedback helps us gauge our progress.

Help me Grow.

I set myself some growth ambitions I desire to achieve by this year's end regarding this website and my youtube channel. Am halfway. Help me reach them by:




Recommendations


What do You Think

Dear readers drop us your comments below. We are building a community of students and learners. Start by dropping us your suggestions below. What tutorials do you want us to do for example? Where can we improve? What are some awesome resources out there? Do you have any code you want to share with us?
By the way that example or snippet you have lying in your computer can really help beginner programmers. We can share it here with other students.

Previous Post Next Post