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

不能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("文件夹所有文件已输出");

}

图片描述

回答:

DataGridView 改变一下`Id`呢、

可能多个`Id`造成了报错

以上是 求教C#用DataGirdViewCell添加行出现Bug 集合已属于 DataGridView 控件(列已定义)该怎么解决? 的全部内容, 来源链接: utcz.com/p/189952.html

回到顶部