none
C# 無法儲存ListView中的SubItems資料 (以txt儲存) RRS feed

  • 問題

  • 本人原意 : 
                   listView1 : (ListView) 有三行資料行 , 第三行是想儲存的SubItems資料


    下乃code :

            int v;
            string u;
    
                            #region ListView reading & saving
    
                            u = System.Convert.ToString(v);
    
                            if (listView1.Items[u].SubItems[2].Text == null)
                            {
                            }
                            else
                            {
    
                                if (sf.ShowDialog() == DialogResult.OK)
                                {
                                    String filepath = sf.FileName;
                                    StreamWriter sw = new StreamWriter(filepath, true, System.Text.Encoding.GetEncoding("UTF-8"));
                 
    
                                    sw.WriteLine(listView1.Items[u].SubItems[2].Text);
                                    sw.Close();
                                    MessageBox.Show("SAVED");
    
                                    v = System.Convert.ToInt32(u);
                                    v++;
                                }
    
                                else
                                {
                                    MessageBox.Show("FALL");
                                }
                            }
    
                            #endregion



    請指教
    2009年9月29日 下午 01:24

解答

  • using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Text;
    using System.Windows.Forms;
    using System.IO;
    
    namespace WindowsFormsApplication2
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }
    
            private void Form1_Load(object sender, EventArgs e)
            {
                // Set the view to show details.
                listView1.View = View.Details;
                // Allow the user to edit item text.
                listView1.LabelEdit = true;
                // Allow the user to rearrange columns.
                listView1.AllowColumnReorder = true;
                // Select the item and subitems when selection is made.
                listView1.FullRowSelect = true;
                // Display grid lines.
                listView1.GridLines = true;
                // Sort the items in the list in ascending order.
                listView1.Sorting = SortOrder.Ascending;
    
                // Create three items and three sets of subitems for each item.
                ListViewItem item1 = new ListViewItem("1");
                // Place a check mark next to the item.
                item1.SubItems.Add("1");
                item1.SubItems.Add("2");
                ListViewItem item2 = new ListViewItem("2");
                item2.SubItems.Add("4");
                item2.SubItems.Add("3");
                ListViewItem item3 = new ListViewItem("3");
                // Place a check mark next to the item.
                item3.SubItems.Add("7");
                item3.SubItems.Add("8");
    
                // Create columns for the items and subitems.
                listView1.Columns.Add("Column 0", -2, HorizontalAlignment.Left);
                listView1.Columns.Add("Column 1", -2, HorizontalAlignment.Left);
                listView1.Columns.Add("Column 2", -2, HorizontalAlignment.Left);
    
                //Add the items to the ListView.
                listView1.Items.AddRange(new ListViewItem[] { item1, item2, item3 });
            }
    
            private void button1_Click(object sender, EventArgs e)
            {
                SaveFileDialog sf = new SaveFileDialog();
                if (sf.ShowDialog() == DialogResult.OK)
                {
                    String filepath = sf.FileName;
                    StreamWriter sw = new StreamWriter(filepath, true, System.Text.Encoding.GetEncoding("UTF-8"));
    
                    for (int i = 0; i < listView1.Items.Count; i++)
                    {
                        // MessageBox.Show(listView1.Items[i].SubItems[2].Text);
                        sw.WriteLine(listView1.Items[i].SubItems[2].Text);
    
                    }
                    sw.Close();
                    MessageBox.Show("SAVED");
                }
                else
                {
                    MessageBox.Show("FALL");
                }
            }
        }
    }
    

    .NET菜鳥自救會 http://www.dotblogs.com.tw/chou/
    • 已標示為解答 ricoyup 2009年9月29日 下午 02:08
    2009年9月29日 下午 01:49
    版主

所有回覆

  • using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Text;
    using System.Windows.Forms;
    using System.IO;
    
    namespace WindowsFormsApplication2
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }
    
            private void Form1_Load(object sender, EventArgs e)
            {
                // Set the view to show details.
                listView1.View = View.Details;
                // Allow the user to edit item text.
                listView1.LabelEdit = true;
                // Allow the user to rearrange columns.
                listView1.AllowColumnReorder = true;
                // Select the item and subitems when selection is made.
                listView1.FullRowSelect = true;
                // Display grid lines.
                listView1.GridLines = true;
                // Sort the items in the list in ascending order.
                listView1.Sorting = SortOrder.Ascending;
    
                // Create three items and three sets of subitems for each item.
                ListViewItem item1 = new ListViewItem("1");
                // Place a check mark next to the item.
                item1.SubItems.Add("1");
                item1.SubItems.Add("2");
                ListViewItem item2 = new ListViewItem("2");
                item2.SubItems.Add("4");
                item2.SubItems.Add("3");
                ListViewItem item3 = new ListViewItem("3");
                // Place a check mark next to the item.
                item3.SubItems.Add("7");
                item3.SubItems.Add("8");
    
                // Create columns for the items and subitems.
                listView1.Columns.Add("Column 0", -2, HorizontalAlignment.Left);
                listView1.Columns.Add("Column 1", -2, HorizontalAlignment.Left);
                listView1.Columns.Add("Column 2", -2, HorizontalAlignment.Left);
    
                //Add the items to the ListView.
                listView1.Items.AddRange(new ListViewItem[] { item1, item2, item3 });
            }
    
            private void button1_Click(object sender, EventArgs e)
            {
                SaveFileDialog sf = new SaveFileDialog();
                if (sf.ShowDialog() == DialogResult.OK)
                {
                    String filepath = sf.FileName;
                    StreamWriter sw = new StreamWriter(filepath, true, System.Text.Encoding.GetEncoding("UTF-8"));
    
                    for (int i = 0; i < listView1.Items.Count; i++)
                    {
                        // MessageBox.Show(listView1.Items[i].SubItems[2].Text);
                        sw.WriteLine(listView1.Items[i].SubItems[2].Text);
    
                    }
                    sw.Close();
                    MessageBox.Show("SAVED");
                }
                else
                {
                    MessageBox.Show("FALL");
                }
            }
        }
    }
    

    .NET菜鳥自救會 http://www.dotblogs.com.tw/chou/
    • 已標示為解答 ricoyup 2009年9月29日 下午 02:08
    2009年9月29日 下午 01:49
    版主
  • Ricoyup:
      我發現你最近都在研究ListView
      其實MSDN文件庫有很多相關的文件可以參考
      [ System.Windows.Controls.ListView 類別 ] (Windows Form用的)
      [System.Web.UI.WebControls.ListView 類別 ] (Web Form用的)

      這兩個頁面還有相當多相關的連結, 應該有助於你瞭解ListView
    學而不思則罔, 思而不學則殆.
    如果你一直都看不懂、不想學習看懂、抗拒看懂MSDN Library的話,那你最好放棄想要寫好程式這件事
    2009年9月29日 下午 02:45
    版主