关于c#:dataGridView读写文本

1次阅读

共计 1264 个字符,预计需要花费 4 分钟才能阅读完成。

dataGridView

DataGridView 控件是数据表格控件,属于很罕用的控件。

winform DataGridView 属性阐明
① 获得或者批改以后单元格的内容
② 设定单元格只读
③ 不显示最上面的新行
④ 判断新增行
⑤ 行的用户删除操作的自定义
⑥ 行、列的暗藏和删除
⑦ 禁止列或者行的 Resize
⑧ 列宽和行高以及列头的高度和行头的宽度的主动调整
⑨ 解冻列或行
⑩ 列程序的调整
⑪ 行头列头的单元格
⑫ 剪切板的操作
⑬ 单元格的 ToolTip 的设置
⑭ 右键菜单(ContextMenuStrip)的设置
⑮ 单元格的边框、网格线款式的设定
⑯ 单元格示意值的设定
⑰ 用户输出时,单元格输出值的设定
⑱ 设定新加行的默认值

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();}
    }
}
正文完
 0