DataGridView中没有列 [英] DataGridView has no columns
问题描述
这是C#Windows窗体。
This is c# windows forms.
我有应该显示两列,文件名,和修改日期数据网格图。说我这样做的方式是通过一个任务。我也有仅持有文件和路径名,BindingSource的,和数据网格视图的类。此任务运行所在的班级传递的方法。一旦任务完成,类应该有,我可以再设置到GridView回窗体上的网格视图。
I have a data grid view that is supposed to display two columns, file names, and modified date. The way that I'm doing this is through a task. I also have a class that hold only the file and path name, bindingsource, and data grid view. This task runs a method where the class is passed. Once the task is complete the class should have a grid view that I can then set to a gridview back on the form.
类看起来是这样的:
class GetLogFilesParameters
{
public string FileNameandPath;
public BindingSource BindingSource;
public DataGridView GridView;
public GetLogFilesParameters(string _fileNameAndPath)
{
FileNameandPath = _fileNameAndPath;
BindingSource = new BindingSource();
GridView = new DataGridView();
GridView.DataSource = BindingSource;
}
}
该方法我的任务就是调用这个样子的:
The method my task is calling look like this:
private static void GetLogFilesTest(GetLogFilesParameters FormFields)
{
Cursor.Current = Cursors.WaitCursor;
try
{
//Setup data table
DataTable FileList = new DataTable();
FileList.Clear();
DataColumn FileNameColumn = new DataColumn();
FileNameColumn.ColumnName = "FileName";
FileNameColumn.DataType = System.Type.GetType("System.String");
DataColumn DateColumn = new DataColumn();
DateColumn.ColumnName = "ModifiedDate";
DateColumn.DataType = System.Type.GetType("System.DateTime");
FileList.Columns.Add(FileNameColumn);
FileList.Columns.Add(DateColumn);
//Get a list of files in a directory
string[] files = Directory.GetFiles(FormFields.FileNameandPath, "*.log");
//Loop through the files and fill the data table with a row for each
foreach (string file in files)
{
FileInfo FileInformation = new FileInfo(file);
DataRow row = FileList.NewRow();
row["FileName"] = FileInformation.Name;
row["ModifiedDate"] = FileInformation.LastWriteTime;
FileList.Rows.Add(row);
}
//FormFields.GridView.Columns.Add("FileName", "File Name");
//FormFields.GridView.Columns.Add("ModifiedDate", "Modified Date");
FormFields.GridView.AutoGenerateColumns = true;
//Setup the binding source
FormFields.BindingSource.DataSource = FileList;
FormFields.BindingSource.Sort = "ModifiedDate DESC";
FormFields.GridView.Columns[0].Width = (FormFields.GridView.Width / 10) * 6;
FormFields.GridView.Columns[1].Width = (FormFields.GridView.Width / 10) * 4;
}
catch (Exception ex)
{
string ErrorText = "Error trying to get the list of log files." + Environment.NewLine + Environment.NewLine;
ExceptionLogger.LogIt(ErrorText, "Exception");
MessageBox.Show(ErrorText + ex.ToString());
}
finally
{
Cursor.Current = Cursors.Default;
}
}
我的任务是这样的:
My task looks like this:
GetLogFilesParameters GetLogFilesParameters = new GetLogFilesParameters(EpicorSenderPath);
Task tGetFiles1 = new Task(() => GetLogFilesTest(GetLogFilesParameters));
tGetFiles1.Start();
tGetFiles1.ContinueWith((antecedent) =>
{
gvEpicorSenderFiles = GetLogFilesParameters.GridView;
}, TaskScheduler.FromCurrentSynchronizationContext());
我得到的错误是System.ArgumentOutOfRangeException:索引超出范围。必须是非负并小于集合的大小。它发生在方法上这一行:
The error I get is System.ArgumentOutOfRangeException: Index was out of range. Must be non-negative and less than the size of the collection. It happens in the method on this line:
FormFields.GridView.Columns[0].Width = (FormFields.GridView.Width / 10) * 6;
这是发生,因为根据调试GridView控件没有任何列。此外,当它确实有行和数据。我不知道什么是错的。
This is happening because the gridview doesn't have any columns according to the debug. Also, when it DOES have rows and data. I don't know what is wrong.
推荐答案
我想(我不明白究竟为什么)您尝试访问尚不存在的列。
I guess (I don't understand why exactly) you try to access Columns that do not exist yet.
尝试移动code在 DataGridView.DataBindingComplete 事件的这些行
Try to move these lines of code in DataGridView.DataBindingComplete event
FormFields.GridView.Columns[0].Width = (FormFields.GridView.Width / 10) * 6;
FormFields.GridView.Columns[1].Width = (FormFields.GridView.Width / 10) * 4;
这篇关于DataGridView中没有列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!