求教C#用DataGirdViewCell添加行出现Bug 集合已属于 DataGridView 控件(列已定义)该怎么解决?

发布于 2022-09-06 07:26:40 字数 3889 浏览 15 评论 0

不能datagridview绑定sqlite数据库为数据源,因为需要在每一行数据前要加个复选框绑定数据源并刷新dgv的话就不能手动在每一行里加个列了。
问题button事件源码:

    SQLiteCommand commandInsert = null;
    SQLiteCommand commandSelect = null;
    string dataBaseTableName = "FileValueNote";
    SQLiteDataReader dr = null;
    DirectoryInfo dirInfo = new DirectoryInfo(@"F:\XTLBB3D");
    private void button1_Click(object sender, EventArgs e)
    {
        SQLiteConnection m_dbConnection = new SQLiteConnection("Data Source=CeShiDatabase.sqlite;Version=3;");
        m_dbConnection.Open();
        int i = 0;
        //遍历指定某目录下的所有文件
        foreach (FileInfo NextFile in dirInfo.GetFiles())
        {
            i += 1;
            string sqlInsert = "insert into " + dataBaseTableName + " (Id,FileName,FileAbsolutePath) values ('" + i + "', '" + NextFile.Name + "', '" + NextFile.FullName + "')";
            commandInsert = new SQLiteCommand(sqlInsert, m_dbConnection);
            commandInsert.ExecuteNonQuery();
        }
        //遍历指定某目录下,所有子目录下的所有文件
        foreach (DirectoryInfo NextDirectory in dirInfo.GetDirectories())  // 首先获取遍历子文件夹
        {
            foreach (FileInfo NextFile in NextDirectory.GetFiles())
            {
                i += 1;
                string sqlInsert = "insert into " + dataBaseTableName + " (Id,FileName,FileAbsolutePath) values ('" + i + "', '" + NextFile.Name + "', '" + NextFile.FullName + "')";
                commandInsert = new SQLiteCommand(sqlInsert, m_dbConnection);
                commandInsert.ExecuteNonQuery();
            }
        }
        DataGridViewColumn dgvc1 = new DataGridViewCheckBoxColumn();
        dataGridView2.Columns.Add(dgvc1);
        dgvc1.Name = "dgvc1";
        dgvc1.HeaderText = "是否删除";
        dgvc1.DataGridView.ColumnHeadersDefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter;
        DataGridViewColumn dgvc2 = new DataGridViewTextBoxColumn();
        dataGridView2.Columns.Add(dgvc2);
        dgvc2.Name = "dgvc2";
        dgvc2.HeaderText = "序号";
        dgvc2.DataGridView.ColumnHeadersDefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter;
        DataGridViewColumn dgvc3 = new DataGridViewTextBoxColumn();
        dataGridView2.Columns.Add(dgvc3);
        dgvc3.Name = "dgvc3";
        dgvc3.HeaderText = "文件名";
        dgvc3.DataGridView.ColumnHeadersDefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter;
        DataGridViewColumn dgvc4 = new DataGridViewTextBoxColumn();
        dataGridView2.Columns.Add(dgvc4);
        dgvc4.Name = "dgvc4";
        dgvc4.HeaderText = "文件绝对路径";
        dgvc4.DataGridView.ColumnHeadersDefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter;
        string sqlSelect = "SELECT * FROM " + dataBaseTableName + ";";
        commandSelect = new SQLiteCommand(sqlSelect, m_dbConnection);
        dr = commandSelect.ExecuteReader();
        DataGridViewRow row = new DataGridViewRow();
        int temp = 0;
        while (dr.Read())
        {
            temp += 1;
            DataGridViewCell checkBoxCell = new DataGridViewCheckBoxCell();
            row.Cells.Add(checkBoxCell);
            DataGridViewCell idCell = new DataGridViewTextBoxCell();
            idCell.Value = temp.ToString();
            row.Cells.Add(idCell);
            DataGridViewCell fileNameCell = new DataGridViewTextBoxCell();
            fileNameCell.Value = dr["FileName"].ToString();
            row.Cells.Add(fileNameCell);
            DataGridViewCell absolutePathCell = new DataGridViewTextBoxCell();
            fileNameCell.Value = dr["FileAbsolutePath"].ToString();
            row.Cells.Add(absolutePathCell);
            dataGridView2.Rows.Add(row);
        }
        MessageBox.Show("文件夹所有文件已输出");
    }
    

图片描述

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

左岸枫 2022-09-13 07:26:40
DataGridView 改变一下`Id`呢、
可能多个`Id`造成了报错
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文