zhy934453950
zhy934453950
2017-12-07 03:34

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

  • winform
  • c#
  • 控件
  • datagridview
  • datagridviewcell

问题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条回答