C#用DataGirdViewCell添加行出现Bug。求winform大牛、老司机帮忙解决下。

问题button事件源码,IDE提示:集合已属于 DataGridView 控件。此操作不再有效
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("文件夹所有文件已输出");
}
图片说明

1个回答

不能设置绑定数据源为sqlite数据库,因为要每行多定义一列复选框。添加行时既添加一个复选框又要刷新绑定数据源会报错的(datagridview被绑定时不能手动添加)

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问