我正在尝试构建由两部分组成的datagridview
一部分来自数据库,另一部分是收集有关本地硬盘上文件的信息并将其显示为一部分,但我收到错误消息
无法以编程方式将行添加到datagridview行 控件是数据绑定时的集合
注意:
知道datagridview
中的数据将在以后用于修改oracle数据库上的数据
我上传了图片,以便更好地了解前两列来自数据库
private void ListFileToBePatched()
{
try
{
string connstr = "data source=orcl;user id=user;password=pwd";
string cmdstr = @"SELECT OFFICE_CODE as ""Office Code"",
IP_ADDRESS as ""Office IP""
FROM table";
string[] array = Directory.GetFiles(SBankfilespath, "*.txt");
using (OracleConnection conn = new OracleConnection(connstr))
using (OracleCommand cmd = new OracleCommand(cmdstr, conn))
{
conn.Open();
OracleTransaction trans = conn.BeginTransaction();
OracleDataReader OraReader = cmd.ExecuteReader();
DataTable dt = new DataTable();
DataColumn OfficeCodecolumn = new DataColumn();
OfficeCodecolumn.DataType = System.Type.GetType("System.String");
OfficeCodecolumn.ColumnName = "Office Code";
dt.Columns.Add(OfficeCodecolumn);
DataColumn OfficeIPcolumn = new DataColumn();
OfficeIPcolumn.DataType = System.Type.GetType("System.String");
OfficeIPcolumn.ColumnName = "Office IP Address";
dt.Columns.Add(OfficeIPcolumn);
DataColumn FileNamecolumn = new DataColumn();
FileNamecolumn.DataType = System.Type.GetType("System.String");
FileNamecolumn.ColumnName = "File Name";
dt.Columns.Add(FileNamecolumn);
DataColumn FullFilePathcolumn = new DataColumn();
FullFilePathcolumn.DataType = System.Type.GetType("System.String");
FullFilePathcolumn.ColumnName = "Full File Path";
dt.Columns.Add(FullFilePathcolumn);
DataColumn DateCreatedcolumn = new DataColumn();
DateCreatedcolumn.DataType = System.Type.GetType("System.String");
DateCreatedcolumn.ColumnName = "Date Created";
dt.Columns.Add(DateCreatedcolumn);
DataColumn Datemodifiedcolumn = new DataColumn();
Datemodifiedcolumn.DataType = System.Type.GetType("System.String");
Datemodifiedcolumn.ColumnName = "Date modified";
dt.Columns.Add(Datemodifiedcolumn);
foreach (string FullFilePath in array)
{
DataRow row = dt.NewRow();
row[2] = Path.GetFileName(FullFilePath);
row[3] = FullFilePath;
row[4] = File.GetCreationTime(FullFilePath);
row[5] = File.GetLastWriteTime(FullFilePath);
dt.Rows.Add(row);
}
dt.Load(OraReader);
DataView view = new DataView(dt);
DGV_PatchStatus.DataSource = view;
}
FileCount.Text = "File Count ( " + DGV_PatchStatus.Rows.Count.ToString() + " )";
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
答案 0 :(得分:1)
您可以使用DataTable.NewRow方法将行添加到DataTable
。
foreach (string FullFilePath in array)
{
DataRow r = DT.NewRow();
r[0] = Path.GetFileName(FullFilePath);
r[1] = FullFilePath;
r[2] = File.GetCreationTime(FullFilePath);
r[3] = File.GetLastWriteTime(FullFilePath);
}
然后只需将DT
再次绑定为DataSource
DGV_PatchStatus.DataSource = DT;
修改强>
我真正需要的是每个办公室都有文件列表
如果每个办公室行都有多个文件,那么您应该查看Tree-View-Tutorial这可能对实现所需结果有帮助。
这里也是Nested-DataGrid-Example也可能有所帮助。祝你好运。
答案 1 :(得分:0)
Make class为数据读取数据后,将数据分配给该对象,然后将对象放入List,然后将DGV_PatchStatus.DataSource初始化为该列表,当需要添加新行时,只需将数据对象添加到该列表并重新初始化DGV_PatchStatus DataSource