保存表单元素

时间:2011-07-28 23:33:05

标签: c# winforms save

我已经创建了一个程序,如果你有一个带有2列的DataGridView。第一列是只读文本框(用户无法更改它)。第二列在每行中具有相同的组合框。

the program before opening a file

如果用户更改了组合框,然后关闭程序,我希望保存元素,以便下次打开程序时,组合框将被选择为他的选择。

the program after opening a file

我已经设法将第一列和第二列的元素保存在两个文本文件example1.txt和example2.txt中,但我不知道如何在程序中将保存的元素再次放在datagridview中打开。

此外,txt文件保存在csv文件所在的路径中。我希望它在exe路径上保存。

这是我到目前为止所做的:

private void button1_Click(object sender, EventArgs e)
            {
                string filename = "";
                DialogResult result = openFileDialog1.ShowDialog();
                if (result == DialogResult.OK)
                {
                    filename = openFileDialog1.FileName;
                textBox1.Text = filename;
                string line;
            // Read the file and display it line by line.
             System.IO.StreamReader file = new System.IO.StreamReader(textBox1.Text);

            stringforData = file.ReadLine();      
            while ((line = file.ReadLine()) != null)
            {

                fileList.Add(line.Split(';'));
            }

            file.Close();



            this.ToDataGrid();
        }
    }

private void button2_Click(object sender, EventArgs e)
        {

    //*************  COLUMN 2 TO STRING[]  ************************************
            string[] colB = new string[dataGridView1.Rows.Count];



            for (int i = 0; i < dataGridView1.Rows.Count; i++)
                {

                    colB[i] = Convert.ToString(dataGridView1.Rows[i].Cells[1].Value);
                   File.WriteAllLines("settings2.txt", colB);
                }
        //*************************************************************************
}
     public void ToDataGrid()
        {
            string[] split = stringforData.Split(';');


        foreach (string item in split)
        {
            dataGridView1.Rows.Add(item);
        }
        File.WriteAllLines("settings1.txt", split);
}

谢谢,
乔治

1 个答案:

答案 0 :(得分:2)

您可以利用DataSet对象的一些内置功能,并将网格数据保存到XML文件,并在再次启动应用程序时将其读回网格。

        //note that this will just save it in the bin folder
        //you'll want to use a better path
        string settingsFile = "GridSettings.xml";
        DataTable gridData = null;

        public FormSaveFoo()
        {
            InitializeComponent();
            PrepareSettingsDataSource();
            SetUpDataSourceBindings();

        }

        private void PrepareSettingsDataSource()
        {
            //see if have a settings file
            if (File.Exists(settingsFile))
            {
                //load up the settings 
                DataSet settings = new DataSet();
                settings.ReadXml(settingsFile);
                if (settings.Tables.Count > 0)
                {
                    gridData = settings.Tables[0];
                }
            }
            else
            {
                CreateSettingsTable();
            }
        }

        private void CreateSettingsTable()
        {
            gridData = new DataTable();
            gridData.Columns.Add(new DataColumn("Name"));
            gridData.Columns.Add(new DataColumn("Text"));
        }

        private void SetUpDataSourceBindings()
        {

            dataGridView1.Columns["NameColumn1"].DataPropertyName = "Name";
            dataGridView1.Columns["TextColumn1"].DataPropertyName = "Text";
            dataGridView1.DataSource = gridData;
        }


        private void button1_Click(object sender, EventArgs e)
        {
            //add the grid data to a dataset and then write it to a file
            DataSet persistSettings = new DataSet();
            persistSettings.Tables.Add(gridData);
            persistSettings.WriteXml(settingsFile);
        }