Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

DataGridView has no columns

This is c# windows forms.

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.

The class looks like this:

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());

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;

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.

like image 723
hondaman2003 Avatar asked Oct 16 '25 15:10

hondaman2003


1 Answers

I guess (I don't understand why exactly) you try to access Columns that do not exist yet.

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;
like image 132
Chris Avatar answered Oct 18 '25 07:10

Chris