dataGridView读写文本

1年前 (2022) 程序员胖胖胖虎阿
119 0 0

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();
        }
    }
}
版权声明:程序员胖胖胖虎阿 发表于 2022年11月24日 下午7:40。
转载请注明:dataGridView读写文本 | 胖虎的工具箱-编程导航

相关文章

暂无评论

暂无评论...