简体   繁体   English

数据源未正确绑定到DataGridView

[英]Datasource not binding properly to DataGridView

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: 我想了一个名为DataGridView中的数据源设置FileGridView到一个List<object>称为filesAndDirectories ,其中包含一系列DirectoryProperties对象和FileProperties对象,变量在如下所示:

DirectoryProperties DirectoryProperties

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

FileProperties 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. 将此列表作为FileGridView的数据源FileGridView时,不会显示任何数据。 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属性

// 
            // 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 https://dl.dropboxusercontent.com/u/14954037/SharpManager.rar

This: 这个:

private string _filename;

is a private field. 是一个私人领域。 You have to make them public properties in order for the DataGridView to use them properly: 您必须使它们成为公共属性,以便DataGridView正确使用它们:

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: 如果您的列已存在于DataGridView控件中,则必须在设置网格的DataSource属性之前分配class属性:

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

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. 如果AutoGenerateColumns为true,DataGridView将自动生成列,默认情况下,它只会为公共属性生成列,而不是为您的私有字段生成列。

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. 此外,列表中的FileProperties和DirectoryProperties最初都可以工作,但是当您向下滚动以实际显示directoryproperties时会崩溃。 You should make a base-class or interface that both your classes inherit from / implement, that contains what you need to show them. 您应该创建一个基类或接口,您的类继承/实现,包含您需要显示它们的内容。

List does not implement IBindingList so the grid does not know about your new items. List没有实现IBindingList,因此网格不知道您的新项目。

Bind your DataGridView to a BindingList instead. 将DataGridView绑定到BindingList。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM