【发布时间】: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);
}
谢谢,
乔治
【问题讨论】: