DataGridView没有列

时间:2013-06-07 13:31:56

标签: c# datagridview

这是c#windows表单。

我有一个数据网格视图,应该显示两列,文件名和修改日期。我这样做的方式是通过一项任务。我还有一个只包含文件和路径名,bindingsource和数据网格视图的类。此任务运行一个传递类的方法。一旦任务完成,该类应该有一个网格视图,然后我可以在窗体上设置为gridview。

该课程如下:

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;
    }
}

我的任务调用的方法如下:

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;
        }
    }

我的任务如下:

GetLogFilesParameters GetLogFilesParameters = new GetLogFilesParameters(EpicorSenderPath);
            Task tGetFiles1 = new Task(() => GetLogFilesTest(GetLogFilesParameters));
            tGetFiles1.Start();
            tGetFiles1.ContinueWith((antecedent) =>
                {
                    gvEpicorSenderFiles = GetLogFilesParameters.GridView;
                }, TaskScheduler.FromCurrentSynchronizationContext());

我得到的错误是System.ArgumentOutOfRangeException:索引超出范围。必须是非负数且小于集合的大小。它发生在这一行的方法中:

FormFields.GridView.Columns[0].Width = (FormFields.GridView.Width / 10) * 6;

这种情况正在发生,因为gridview根据调试没有任何列。此外,当它有行和数据时。我不知道出了什么问题。

1 个答案:

答案 0 :(得分:2)

我猜(我完全不明白为什么)您尝试访问尚不存在的列。

尝试在DataGridView.DataBindingComplete事件

中移动这些代码行
FormFields.GridView.Columns[0].Width = (FormFields.GridView.Width / 10) * 6; 
FormFields.GridView.Columns[1].Width = (FormFields.GridView.Width / 10) * 4;