我正在尝试将列表绑定到datagridview。 我这样做:
public void seedatagrid(List<myClass> liste2)
{
dgv_TraceItems.DataSource = new BindingList<myClass>(liste2.ToList());
}
并且datagridview有数据,图片中的数据如何,但它没有显示任何内容。
你帮我吗?我怎么解决这个问题? 谢谢
public enum TYPE
{
normal= 1,
especial= 3,
low= 6,
high= 7,
}
public class myClass : INotifyPropertyChanged
{
private byte number;
private TYPE type;
private string file;
private bool isselected;
public event PropertyChangedEventHandler PropertyChanged;
public byte Number
{
get
{
return this.number;
}
set
{
this.number= value;
this.OnPropertyChanged("Number");
}
}
public TYPE Type
{
get
{
return this.type;
}
set
{
this.type = value;
this.OnPropertyChanged("Type");
}
}
public string File
{
get
{
return this.file;
}
set
{
this.file = value;
this.OnPropertyChanged("File");
}
}
public bool IsSelected
{
get
{
return this.isselected;
}
set
{
this.isselected = value;
this.OnPropertyChanged("IsSelected");
}
}
public myClass(UInt32 Data, string Text)
{
this.number = (byte)((Data & 0x0000FF00) >> 8);
this.type = (TYPE)((Data & 0x00FF0000) >> 16);
this.file = Text;
}
private void OnPropertyChanged(string propertyName)
{
if (this.PropertyChanged != null)
{
this.PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
答案 0 :(得分:0)
之后
dgv_TraceItems.DataSource = new BindingList<BLF_InfoClass>(liste2.ToList());
尝试添加
dgv_TraceItems.DataBind();
答案 1 :(得分:0)
使用受尊重的BLF_InfoClass属性设置每个列的“DataPropertyName”属性。您必须为数据网格创建数据列。
dgv_TraceItems.AutoGenerateColumns = false;
dgv_TraceItems.Columns.Add(CreateComboBoxWithEnums());
// Initialize and add a text box column.
DataGridViewColumn column = new DataGridViewTextBoxColumn();
column.DataPropertyName = "number";
column.Name = "number";
dataGridView1.Columns.Add(column);
// Initialize and add a text box column.
column = new DataGridViewTextBoxColumn();
column.DataPropertyName = "file";
column.Name = "file";
dataGridView1.Columns.Add(column);
// Initialize and add a check box column.
column = new DataGridViewCheckBoxColumn();
column.DataPropertyName = "isselected";
column.Name = "isselected";
dataGridView1.Columns.Add(column);
// Initialize the form.
this.Controls.Add(dataGridView1);
this.AutoSize = true;
List<myClass> bindingData = new List<myClass>();
for (int i = 0; i < 5; i++)
{
myClass testObj = new myClass();
testObj.File = "test" + i;
testObj.IsSelected = true;
testObj.Type = TYPE.high;
bindingData.Add(testObj);
}
dgv_TraceItems.DataSource = bindingData;
}
DataGridViewComboBoxColumn CreateComboBoxWithEnums()
{
DataGridViewComboBoxColumn combo = new DataGridViewComboBoxColumn();
combo.DataSource = Enum.GetValues(typeof(TYPE));
combo.DataPropertyName = "Type";
combo.Name = "Type";
return combo;
}