保存表单元素

发布于 2024-11-27 09:09:58 字数 2062 浏览 0 评论 0原文

如果您有一个包含 2 列的 DataGridView,我已经编写了一个程序。第一列是只读文本框(用户无法更改它)。第二列每行都有相同的组合框。

打开文件之前的程序

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

打开文件后的程序

我已成功将第一列和第二列的元素保存在两个文本文件中,例如1。 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);
}

谢谢,
乔治

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.

the program before opening a file

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.

the program after opening a file

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 = "";
                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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

╄→承喏 2024-12-04 09:09:58

您可以利用 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);
        }

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);
        }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文