欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

dataGridView读写文本

程序员文章站 2022-03-20 12:13:44
constant con = new constant(); private void loadlistbox2() { dataGridView1.ColumnCount = 1; string z; if (File.Exists(".//allitems.txt")) { FileStream ......

constant con = new constant();

private void loadlistbox2()
{
datagridview1.columncount = 1;
string z;
if (file.exists(".//allitems.txt"))
{
filestream fs = new filestream(".//allitems.txt", system.io.filemode.open, system.io.fileaccess.read);

streamreader sr = new streamreader(fs, system.text.encoding.getencoding("utf-8"));
try
{
while (true)
{
z = sr.readline();
if (z == null) break;
datagridview1.rows.add(z);
}
}
finally
{
sr.close();
fs.dispose();
fs.close();
}
}
}
string st = "";


private void savetestitem()
{
// datagridview1.columncount = 1;
if (file.exists(".//allitems.txt"))
{
filestream fs = new filestream(".//allitems.txt", system.io.filemode.open, system.io.fileaccess.write);

streamwriter sr = new streamwriter(fs, system.text.encoding.getencoding("utf-8"));
try
{
for (int i = 0; i < 22; i++)
{
for (int j = 0; j < 1; j++)
{
st = st + datagridview1.rows[i].cells[j].value;
}
sr.writeline(st);
st = "";

//for (int i = 0; i < datagridview1.rows.count; i++)
//{
// sr.writeline(datagridview1.rows[i].cells);
// }
}

}
finally
{
sr.close();
fs.dispose();
fs.close();
}
}
}