如何绑定列表到dataGridView? [英] How to bind list to dataGridView?
问题描述
我似乎在圈子里跑来跑去,在最后几个小时里一直这样做。
我想从一个字符串数组中填充一个datagridview。我已经看到它不可能直接,我需要创建一个自定义类型,将该字符串作为一个公共属性。所以我做了一个类:
public class FileName
{
private string _value;
public FileName(string pValue)
{
_value = pValue;
}
public string Value
{
get
{
return _value;
}
set {_value = value; }
}
}
这是容器类,它只有具有字符串值的属性。我现在想要的是,当我将其数据源绑定到列表时,该字符串将出现在datagridview中。
另外我有这个方法,BindGrid(),我想填充datagridview。这里是:
private void BindGrid()
{
gvFilesOnServer.AutoGenerateColumns = false;
//以编程方式创建列
DataGridViewTextBoxColumn colFileName = new DataGridViewTextBoxColumn();
DataGridViewCell cell = new DataGridViewTextBoxCell();
colFileName.CellTemplate = cell; colFileName.Name =Value;
colFileName.HeaderText =文件名;
colFileName.ValueType = typeof(FileName);
//将列添加到datagridview
gvFilesOnServer.Columns.Add(colFileName);
//填充字符串数组
string [] filelist = GetFileListOnWebServer();
//尝试制作列表< FileName>从该数组
列表< FileName> filenamesList = new List< FileName>(filelist.Length); (int i = 0; i< filelist.Length; i ++)
{
filenamesList.Add(new FileName(filelist [i] .ToString()));
}
//尝试制作一个绑定源
BindingSource bs = new BindingSource();
bs.DataSource = typeof(FileName);
foreach(FileName fn in filenamesList)
{
bs.Add(fn);
}
gvFilesOnServer.DataSource = bs;
}
最后,问题:字符串数组填满,列表创建好,但我在datagridview中得到一个空列。我也直接尝试了datasource = list<>,而不是= bindingsource,仍然没有。
我真的很感激一个建议,这一直使我疯狂。 >
谢谢
使用 BindingList 并设置 DataPropertyName - 列的属性。
尝试以下操作: / p>
...
private void BindGrid()
{
gvFilesOnServer.AutoGenerateColumns = false;
//以编程方式创建列
DataGridViewCell cell = new DataGridViewTextBoxCell();
DataGridViewTextBoxColumn colFileName = new DataGridViewTextBoxColumn()
{
CellTemplate = cell,
Name =Value,
HeaderText =File Name,
DataPropertyName =Value//告诉列哪个FileName的属性应该使用
};
gvFilesOnServer.Columns.Add(colFileName);
var filelist = GetFileListOnWebServer()。ToList();
var filenamesList = new BindingList< FileName>(filelist); //< - BindingList
//将BindingList直接绑定到DataGrid,不需要BindingSource
gvFilesOnServer.DataSource = filenamesList
}
I seem to be running around in circles and have been doing so in the last hours.
I want to populate a datagridview from an array of strings. I've read its not possible directly, and that I need to create a custom type that holds the string as a public property. So I made a class:
public class FileName
{
private string _value;
public FileName(string pValue)
{
_value = pValue;
}
public string Value
{
get
{
return _value;
}
set { _value = value; }
}
}
this is the container class, and it simply has a property with the value of the string. All I want now is that string to appear in the datagridview, when I bind its datasource to a List.
Also I have this method, BindGrid() which I want to fill the datagridview with. Here it is:
private void BindGrid()
{
gvFilesOnServer.AutoGenerateColumns = false;
//create the column programatically
DataGridViewTextBoxColumn colFileName = new DataGridViewTextBoxColumn();
DataGridViewCell cell = new DataGridViewTextBoxCell();
colFileName.CellTemplate = cell; colFileName.Name = "Value";
colFileName.HeaderText = "File Name";
colFileName.ValueType = typeof(FileName);
//add the column to the datagridview
gvFilesOnServer.Columns.Add(colFileName);
//fill the string array
string[] filelist = GetFileListOnWebServer();
//try making a List<FileName> from that array
List<FileName> filenamesList = new List<FileName>(filelist.Length);
for (int i = 0; i < filelist.Length; i++)
{
filenamesList.Add(new FileName(filelist[i].ToString()));
}
//try making a bindingsource
BindingSource bs = new BindingSource();
bs.DataSource = typeof(FileName);
foreach (FileName fn in filenamesList)
{
bs.Add(fn);
}
gvFilesOnServer.DataSource = bs;
}
Finally, the problem: the string array fills ok, the list is created ok, but I get an empty column in the datagridview. I also tried datasource= list<> directly, instead of = bindingsource, still nothing.
I would really appreciate an advice, this has been driving me crazy.
Thank you
Use a BindingList and set the DataPropertyName-Property of the column.
Try the following:
...
private void BindGrid()
{
gvFilesOnServer.AutoGenerateColumns = false;
//create the column programatically
DataGridViewCell cell = new DataGridViewTextBoxCell();
DataGridViewTextBoxColumn colFileName = new DataGridViewTextBoxColumn()
{
CellTemplate = cell,
Name = "Value",
HeaderText = "File Name",
DataPropertyName = "Value" // Tell the column which property of FileName it should use
};
gvFilesOnServer.Columns.Add(colFileName);
var filelist = GetFileListOnWebServer().ToList();
var filenamesList = new BindingList<FileName>(filelist); // <-- BindingList
//Bind BindingList directly to the DataGrid, no need of BindingSource
gvFilesOnServer.DataSource = filenamesList
}
这篇关于如何绑定列表到dataGridView?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!