开发者

Save elements of Form

开发者 https://www.devze.com 2023-03-23 12:38 出处:网络
I have made a program were you have a DataGridView with 2 columns. The first column is a read-only textbox (the user can not change it). The second column has the same comboboxes in every row.

I have made a program were you have a DataGridView with 2 columns. The first column is a read-only textbox (the user can not change it). The second column has the same comboboxes in every row.

Save elements of Form

If the user changes a combobox, then closes the program, I want the elements to be saved so the next time he opens the program the combobox will be choosen to his selection.

Save elements of Form

I have managed to save the elements of the first and second column in two text files,example1.txt and example2.txt, but I don't know how to make the saved elements be placed again in the datagridview when the program opens.

Also, the txt files are saved in the path where the csv file is. I want it to be save at the exe path.

Here is what I have made so far:

private void button1_Click(object sender, EventArgs e)
            {
                string filename = "";
               开发者_StackOverflow 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);
}

Thanks,

George


You can take advantage of some of the built in goodies of the DataSet object and save your grid data to an XML file and read it back in to the grid when you start the app again.

        //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);
        }
0

精彩评论

暂无评论...
验证码 换一张
取 消