Click here to Skip to main content
15,892,298 members
Articles / Programming Languages / C#
Tip/Trick

Update Listview Items

Rate me:
Please Sign up or sign in to vote.
4.78/5 (4 votes)
23 Sep 2015CPOL 23.3K   260   6   1
Update Listview items

Introduction

abbListView.Update class provides a way to create a run time form to update listview item.

Background

I was having difficulties to update ListView Items. I solved mostly creating individual forms for every Listview to update Items in the past. It was difficult and took up a lot of my time.

I created a class which creates a run time form and updates Listview Item by itself automatically to save time.

Using the Code

There is a pre-requirement to use abbListview.Update you must consider:

  • Label for Textbox will use your ListView's column Header as Label Text.
  • Column Tags will be TextBox Names, Column Tags mustn't be empty and must be unique.

The using class is simple, you must add the following lines to your Listview's MouseDoubleClick event. I assume your ListView's name is listView1.

C#
listView1 = abb.abbListview.Update(listView1, "Form Header", listView1.SelectedIndices[0]);

Class source code is in the following:

C#
using System;
using System.Windows.Forms;

namespace abb
{
    public class abbListview
    {
        public static ListView Update(ListView lv, string lvHeader, 
        	int itemId, int LocInd=27, int tbWidth=200, int tbHeight = 25)
        {
            Form newForm = new Form();

            newForm.MaximizeBox = false;
            newForm.MinimizeBox = false;
            newForm.ShowInTaskbar = false;
            newForm.FormBorderStyle = FormBorderStyle.FixedDialog;
            newForm.Text = lvHeader + " / Line :" + itemId.ToString();

            for (int i = 0; i < lv.Columns.Count; i++)
            {
                //Label
                Label labelRun = new Label();
                labelRun.Name = "labelRun" + i.ToString();
                labelRun.Location = new System.Drawing.Point(10, 18 + (LocInd * i));
                labelRun.AutoSize = true;
                labelRun.Text = lv.Columns[i].Text;
                newForm.Controls.Add(labelRun);

                //TextBox
                TextBox txtRun = new TextBox();
                txtRun.Name = lv.Columns[i].Tag.ToString();
                txtRun.Location = new System.Drawing.Point(200, 18 + (LocInd * i));
                txtRun.Size = new System.Drawing.Size(tbWidth, tbHeight);
                txtRun.Text = lv.Items[itemId].SubItems[i].Text;
                newForm.Controls.Add(txtRun);
            }
            Button okButtonRun = new Button();
            okButtonRun.Name = "OK";
            okButtonRun.Text = "OK";
            okButtonRun.Location = new System.Drawing.Point(500, 38 + lv.Columns.Count * LocInd);
            okButtonRun.Size = new System.Drawing.Size(75, 23);
            okButtonRun.DialogResult = DialogResult.OK;
            okButtonRun.Click += delegate
            {
                newForm.DialogResult = DialogResult.OK;
                newForm.Close();
            };
            newForm.Controls.Add(okButtonRun);


            Button cancelButtonRun = new Button();
            cancelButtonRun.Name = "Cancel";
            cancelButtonRun.Text = "Cancel";
            cancelButtonRun.Location = new System.Drawing.Point(400, 38 + lv.Columns.Count * LocInd);
            cancelButtonRun.Size = new System.Drawing.Size(75, 23);
            cancelButtonRun.DialogResult = DialogResult.Cancel;
            cancelButtonRun.Click += delegate
            {
                newForm.DialogResult = DialogResult.Cancel;
                newForm.Close();
            };
            newForm.Controls.Add(cancelButtonRun);
            newForm.Size = new System.Drawing.Size(600, 38 + lv.Columns.Count * LocInd+3 + 60);
            newForm.ShowDialog();

            if(newForm.DialogResult==DialogResult.OK)
            {
                for (int i = 0; i < lv.Columns.Count; i++)
                {
                    foreach (Control X in newForm.Controls)
                    {
                        if (X.GetType() == typeof(TextBox))
                        {
                            if (((TextBox)(X)).Name == lv.Columns[i].Tag.ToString())
                            {
                                lv.Items[itemId].SubItems[i].Text = ((TextBox)(X)).Text;
                                break;
                            }
                        }
                    }
                }
            }
            return lv;
        }
    }
}

History

  • 23rd September, 2015: Initial version

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Turkey Turkey
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralMy Vote of 5 Pin
aarif moh shaikh23-Sep-15 19:07
professionalaarif moh shaikh23-Sep-15 19:07 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.