1
votes

I'm trying to set the datasource of a DataGridView called FileGridView to a List<object> called filesAndDirectories, which contains a list of DirectoryProperties objects and FileProperties objects, the variables in which are shown below:

DirectoryProperties

private string _directoryname;
private string _directorytype;
private string _directorysize;
private string _dateCreated;
private string _dateModified;

FileProperties

private string _filename;
private string _filetype;
private string _filesize;
private string _dateCreated;
private string _dateModified;

Binding list to datasource

FileGridView.DataSource = files;

When this list is passed to be the datasource of FileGridView however, none of the data shows up. Oddly enough, however, the correct number of rows and columns to display the data ARE being shown.

Any ideas as to what I'm doing wrong? The list is obviously having some impact, I'm just not sure why the grid isn't updating.

FileGridView Properties

// 
            // FileGridView
            // 
            this.FileGridView.AllowUserToAddRows = false;
            this.FileGridView.AllowUserToDeleteRows = false;
            this.FileGridView.AllowUserToResizeRows = false;
            this.FileGridView.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize;
            this.FileGridView.Columns.AddRange(new System.Windows.Forms.DataGridViewColumn[] {
            this.ColName,
            this.ColType,
            this.ColSize,
            this.ColDateCreated,
            this.ColDateModified});
            this.FileGridView.EditMode = System.Windows.Forms.DataGridViewEditMode.EditProgrammatically;
            this.FileGridView.Location = new System.Drawing.Point(13, 38);
            this.FileGridView.MultiSelect = false;
            this.FileGridView.Name = "FileGridView";
            this.FileGridView.RowHeadersVisible = false;
            this.FileGridView.ShowEditingIcon = false;
            this.FileGridView.Size = new System.Drawing.Size(503, 376);
            this.FileGridView.TabIndex = 4;
            this.FileGridView.CellContentDoubleClick += new System.Windows.Forms.DataGridViewCellEventHandler(this.FileGridView_CellContentDoubleClick);

Link to project

https://dl.dropboxusercontent.com/u/14954037/SharpManager.rar

3
Have you called FileGridView.DataBind();Vishal
@Vishal : that doesn't seem to be a method within FileGridViewdantdj
Sorry, I have not used winforms for long time. Currently I work on WPF as well as ASP.Net. In ASP.Net GridView has a method called DataBind(). So, I thought there will be same method in Winforms. Sorry......Vishal
Please keep a breakpoint on this line : FileGridView.DataSource = files; and run your program. Check if your files variable has some data in it?Vishal
@Vishal : Yeah, it shows that the data is in there. Curious as to why it doesn't actually appear in the DataGridView...dantdj

3 Answers

2
votes

This:

private string _filename;

is a private field. You have to make them public properties in order for the DataGridView to use them properly:

public string Filename {get; set; }

If your columns already exist in the DataGridView control, then you have to assign the class property before you set the grid's DataSource property:

dataGridView1.Columns[0].DataPropertyName = "Filename";
1
votes

The DataGridView will generate columns automatically if AutoGenerateColumns is true, which it is by default, it will only generate columns for public properties though, not for your private fields.

This would explain why your control knows your number of elements, but not how to show them.

edit:

Okay I managed to get some data showing. Having public properties seems necessary,

public string _directoryname { get; set; } 

works,

private string _directoryname; 

does not. Also, having both FileProperties and DirectoryProperties in your list works initially but crashes when you scroll down to actually show the directoryproperties. You should make a base-class or interface that both your classes inherit from / implement, that contains what you need to show them.

0
votes

List does not implement IBindingList so the grid does not know about your new items.

Bind your DataGridView to a BindingList instead.

var blist = new BindingList<object>(your_file_list);
myGrid.DataSource = blist;