【问题标题】:Save elements of Form保存表单元素
【发布时间】:2011-07-28 23:33:05
【问题描述】:

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

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

我已经设法将第一列和第二列的元素保存在两个文本文件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);
}

谢谢,
乔治

【问题讨论】:

    标签: c# winforms save


    【解决方案1】:

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

    【讨论】:

    • 无法理解dataGridView1.Columns["NameColumn1"].DataPropertyName = "Name"; dataGridView1.Columns["TextColumn1"].DataPropertyName = "Text"; gridData = settings.Tables[0]; 行是做什么的.....请帮忙!
    • dataGridView1.Columns["NameColumn1"].DataPropertyName ="Name";告诉网格列要链接到哪个数据表列。 gridData = settings.Tables[0];从 XML 读取的数据集中获取第一个表,并将其分配给绑定到网格的数据表
    • 我收到错误消息“对象引用未设置为对象的实例。”
    • 您需要将对象名称更改为您在代码中使用的名称。
    • 无论您如何命名列,您也可以只使用列索引 dataGridView1.Columns[1].DataPropertyName = "Text"
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-07-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-11-13
    • 2017-01-14
    相关资源
    最近更新 更多