保存表单元素
如果您有一个包含 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.
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.
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以利用 DataSet 对象的一些内置功能,将网格数据保存到 XML 文件中,并在再次启动应用程序时将其读回到网格中。
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.