locked
how to delete data array when delete selection listview?

    Question

  • hI, i am new here,..

    i 've ask question for listview.

    i have listvew and array. when i input data to listvew, data also input in array. then i want to delete selection in listvew, data also deleted.

    how can i make that?

    thanks.

    regards.

    Thursday, August 12, 2010 5:00 AM

Answers

  • Hi JhunkM,

    I've try many times to figure out the priblem,

    and I modify your code as follows, it works well:

    public partial class Form1 : Form
      {
        private string[] _strCopy = new string[150];
        private string[] _strnama = new string[150];
        private static int _count = 0;
    
        public Form1()
        {
          InitializeComponent();
        }
    
        private void btnAdd_Click(object sender, EventArgs e)
        {
          if (opFDialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
          {
            //for multiselect
            foreach (string file in opFDialog.FileNames)
            {
              string fileName = Path.GetFileName(file);
              string pathFile = Path.GetDirectoryName(file) + "\\";
    
              _strnama[_count] = file;
              _strCopy[_count] = fileName + ".gnk";
    
              ListViewItem item = new ListViewItem(fileName);
              item.SubItems.Add(new ListViewItem.ListViewSubItem(item, pathFile));
              listView.Items.Add(item);
              _count++;
            }
          }
          opFDialog.FileName = "";
        }
    
        private void btnDelete_Click(object sender, EventArgs e)
        {
          int index = 0;
          List<string> tempList1 = new List<string>(_strnama);
          List<string> tempList2 = new List<string>(_strCopy);
    
          if (listView.Items.Count > 0)
          {
            //Delete Selected Items
            foreach (ListViewItem item in listView.SelectedItems)
            {
              index = listView.Items.Count;
    
              tempList1.Remove((from n in _strnama where n.ToLower() == (item.SubItems[1].Text + item.Text).ToLower() select n).First());
              this._strnama = tempList1.ToArray();
    
              tempList2.Remove((from n in _strCopy where n.ToLower() == (item.Text + ".gnk").ToLower() select n).First());
              this._strCopy = tempList1.ToArray();
    
              item.Remove();
              _count--;
            }
          }
          else
          {
            XtraMessageBox.Show(this,
                      "No selected in table",
                      "warning", MessageBoxButtons.OK, MessageBoxIcon.Warning,
                      MessageBoxDefaultButton.Button1);
          }
        }
      }
    

    CHECK THIS CODE IF IT WORKS~~~

    thx~



    • Marked as answer by JhunkM Monday, August 16, 2010 1:49 PM
    • Edited by DK. Da Sunday, June 10, 2012 2:21 AM
    Monday, August 16, 2010 8:40 AM

All replies

  • Hi JhunkM,

    I think it's easy to do what you want,

    here is a Solution compressed with WinRAR:

    My Solution

     

    just check this out~~~



    • Edited by DK. Da Sunday, June 10, 2012 2:20 AM
    Thursday, August 12, 2010 6:59 AM
  • Hi JhunkM,

    You could follow the following suggestions.

    1, I would recommend that you could write an event, which could control listbox and array. When this event is touched off, listbox and array are updated.

    2, if you would like to enhance the speed, you could consider that you create a datatable to copy data.

     

    Hope this helps you

    Best Regards

    Larcolais Gong

     


    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.
    Thursday, August 12, 2010 9:41 AM
  • hi,

    you can do it by having to handle data items in you list view and depends on that you perform action on your array.


    Thanks and Regards, Shailesh B. Davara
    Thursday, August 12, 2010 1:38 PM
  • Hi JhunkM,

    I think it's easy to do what you want,

    here is a Solution compressed with WinRAR:

    My Solution

     

    just check this out~~~


    小中中的學習筆記

    hi, i try your code to my code, but i ve erorr.

    please fix my code. this my code:

    public partial class Form1 : Form
        {
            private string[] _strCopy = new string[150];
            private string[] _strnama = new string[150];
            private static int _count = 0;

            public Form1()
            {
                InitializeComponent();
            }

            private void btnAdd_Click(object sender, EventArgs e)
            {
                if (opFDialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
                {
    //for multiselect
                    foreach (string file in opFDialog.FileNames)
                    {
                        string fileName = Path.GetFileName(file);
                        string pathFile = Path.GetDirectoryName(file) + "\\";

                        _strnama[_count] = file;
                        _strCopy[_count] = fileName + ".gnk";

                        ListViewItem item = new ListViewItem(fileName);
                        item.SubItems.Add(new ListViewItem.ListViewSubItem(item, pathFile));
                        listView.Items.Add(item);
                        _count++;
                    }
                }
                opFDialog.FileName = "";
            }

            private void btnDelete_Click(object sender, EventArgs e)
            {
                int index = 0;
                List<string> tempList1 = new List<string>(_strnama);
                List<string> tempList2 = new List<string>(_strCopy);

                if (listView.Items.Count > 0)
                {
                    //Delete Selected Items
                    foreach (ListViewItem item in listView.SelectedItems)
                    {
                        index = listView.Items.Count;

                        tempList1.Remove((from n in _strnama where n == item.Text select n).First());
                        this._strnama = tempList1.ToArray();

                        tempList2.Remove((from n in _strCopy where n == item.Text select n).First());
                        this._strCopy = tempList1.ToArray();

                        item.Remove();
                        _count--;
                    }
                }
                else
                {
                    XtraMessageBox.Show(this,
                                        "No selected in table",
                                        "warning", MessageBoxButtons.OK, MessageBoxIcon.Warning,
                                        MessageBoxDefaultButton.Button1);
                }
            }
        }

    thanks

    regards

    Sunday, August 15, 2010 9:29 AM
  • Hi JhunkM,

    could you post the error message here~?

    and at what line the error occured?



    • Edited by DK. Da Sunday, June 10, 2012 2:21 AM
    Sunday, August 15, 2010 10:43 AM
  • this block my error:
    tempList1.Remove((from n in _strnama where n == item.Text select n).First());
    

    and the message is: "sequence contains no elements".

    what is mean that?

    please give me solution.

    thanks.

    regards

    Monday, August 16, 2010 5:33 AM
  • Hello JhunkM,

    I found the problem,

    it's because you assign "full file path + file name" to elements in _strnama, but just "file name" to item's text in listView,

    so, you can just add the full path to the item's text, then it will go fine~~~



    • Edited by DK. Da Sunday, June 10, 2012 2:21 AM
    Monday, August 16, 2010 7:41 AM
  • Hi JhunkM,

    I've try many times to figure out the priblem,

    and I modify your code as follows, it works well:

    public partial class Form1 : Form
      {
        private string[] _strCopy = new string[150];
        private string[] _strnama = new string[150];
        private static int _count = 0;
    
        public Form1()
        {
          InitializeComponent();
        }
    
        private void btnAdd_Click(object sender, EventArgs e)
        {
          if (opFDialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
          {
            //for multiselect
            foreach (string file in opFDialog.FileNames)
            {
              string fileName = Path.GetFileName(file);
              string pathFile = Path.GetDirectoryName(file) + "\\";
    
              _strnama[_count] = file;
              _strCopy[_count] = fileName + ".gnk";
    
              ListViewItem item = new ListViewItem(fileName);
              item.SubItems.Add(new ListViewItem.ListViewSubItem(item, pathFile));
              listView.Items.Add(item);
              _count++;
            }
          }
          opFDialog.FileName = "";
        }
    
        private void btnDelete_Click(object sender, EventArgs e)
        {
          int index = 0;
          List<string> tempList1 = new List<string>(_strnama);
          List<string> tempList2 = new List<string>(_strCopy);
    
          if (listView.Items.Count > 0)
          {
            //Delete Selected Items
            foreach (ListViewItem item in listView.SelectedItems)
            {
              index = listView.Items.Count;
    
              tempList1.Remove((from n in _strnama where n.ToLower() == (item.SubItems[1].Text + item.Text).ToLower() select n).First());
              this._strnama = tempList1.ToArray();
    
              tempList2.Remove((from n in _strCopy where n.ToLower() == (item.Text + ".gnk").ToLower() select n).First());
              this._strCopy = tempList1.ToArray();
    
              item.Remove();
              _count--;
            }
          }
          else
          {
            XtraMessageBox.Show(this,
                      "No selected in table",
                      "warning", MessageBoxButtons.OK, MessageBoxIcon.Warning,
                      MessageBoxDefaultButton.Button1);
          }
        }
      }
    

    CHECK THIS CODE IF IT WORKS~~~

    thx~



    • Marked as answer by JhunkM Monday, August 16, 2010 1:49 PM
    • Edited by DK. Da Sunday, June 10, 2012 2:21 AM
    Monday, August 16, 2010 8:40 AM
  • thanks,.

    it is help me,..

    Monday, August 16, 2010 1:50 PM