c#,使用数据框来填充datagridview和filter行
我是Winform的新手,我正在尝试使用DataGridView来显示我的班级中的一些信息。 我使用以下代码显示我的信息:
public class Dataclass
{
public string FileName {get; set}
public string FieldsTotal {get; set}
public string Quantity{get; set}
public string SelectionColumnTexte {get; set}
public Dataclass(string fileName, string fieldsTotal, string quantity, string selectionColumnTexte)
{
FileName = fileName;
FieldsTotal = fieldsTotal;
Quantity= quantity;
SelectionColumnTexte = selectionColumnTexte;
}
}
public static DataTable dataTable = new DataTable();
public static DatagridView dgv = this.Mydatagridview;
public static BindingSource bindingSource = new BindingSource();
public static List<Dataclass> MyDataclass = new List<Dataclass>();
MyDataclass.Add(new Dataclass("Name1","10","10","Select"));
MyDataclass.Add(new Dataclass("Name2","20","20","Select"));
MyDataclass.Add(new Dataclass("Name3","30","30","Select"));
public static void InitializeDataGridView()
{
FillBindingSourceWithClass();
ModifyDatagridViewParameters();
CreateDifferentColumnAndFillItWithData();
}
public static void FillBindingSourceWithClass()
{
foreach (Dataclass Myclass in MyDataclass)
{
bindingSource.Add(Myclass);
}
}
public static void ModifyDatagridViewParameters()
{
dgv.AutoGenerateColumns = false;
dgv.AutoSize = true;
dgv.DataSource = bindingSource;
}
public static void CreateDifferentColumnAndFillItWithData()
{
DataGridViewColumn FileNameColumn = new DataGridViewTextBoxColumn();
FileNameColumn.Name = "File Name";
FileNameColumn.DataPropertyName = "FileName";
dgv.Columns.Add(FileNameColumn);
DataGridViewColumn FieldsTotalColumn = new DataGridViewTextBoxColumn();
FieldsTotalColumn.Name = "FieldsTotal";
FieldsTotalColumn.DataPropertyName = "FieldsTotalInFile";
dgv.Columns.Add(FieldsTotalColumn);
DataGridViewColumn QuantityColumn = new DataGridViewTextBoxColumn();
QuantityColumn.Name = "Quantity";
QuantityColumn.DataPropertyName = "Quantity";
dgv.Columns.Add(QuantityColumn);
DataGridViewColumn SelectionColumn = new DataGridViewButtonColumn();
SelectionColumn.Name = "File Selection (Click to multiselect)";
SelectionColumn.DataPropertyName = "SelectionColumnTexte";
dgv.Columns.Add(SelectionColumn );
}
此时,我的dataGridView正确生成了,并且我的所有数据都可见。 现在,我想通过文件名列过滤行,我不知道该怎么做。我有另一个变量(包含组合ox)可以选择要过滤的文件名。 我尝试使用bindingsource.filter,但没有成功。 有人可以帮助我做到这一点,还是向我解释DataGridView结构太糟糕了?我没有数据词(我不了解它的有用性。)
I am new to winForm and I am trying to use a DatagridView to display some information from my class.
I use the following code to display my information:
public class Dataclass
{
public string FileName {get; set}
public string FieldsTotal {get; set}
public string Quantity{get; set}
public string SelectionColumnTexte {get; set}
public Dataclass(string fileName, string fieldsTotal, string quantity, string selectionColumnTexte)
{
FileName = fileName;
FieldsTotal = fieldsTotal;
Quantity= quantity;
SelectionColumnTexte = selectionColumnTexte;
}
}
public static DataTable dataTable = new DataTable();
public static DatagridView dgv = this.Mydatagridview;
public static BindingSource bindingSource = new BindingSource();
public static List<Dataclass> MyDataclass = new List<Dataclass>();
MyDataclass.Add(new Dataclass("Name1","10","10","Select"));
MyDataclass.Add(new Dataclass("Name2","20","20","Select"));
MyDataclass.Add(new Dataclass("Name3","30","30","Select"));
public static void InitializeDataGridView()
{
FillBindingSourceWithClass();
ModifyDatagridViewParameters();
CreateDifferentColumnAndFillItWithData();
}
public static void FillBindingSourceWithClass()
{
foreach (Dataclass Myclass in MyDataclass)
{
bindingSource.Add(Myclass);
}
}
public static void ModifyDatagridViewParameters()
{
dgv.AutoGenerateColumns = false;
dgv.AutoSize = true;
dgv.DataSource = bindingSource;
}
public static void CreateDifferentColumnAndFillItWithData()
{
DataGridViewColumn FileNameColumn = new DataGridViewTextBoxColumn();
FileNameColumn.Name = "File Name";
FileNameColumn.DataPropertyName = "FileName";
dgv.Columns.Add(FileNameColumn);
DataGridViewColumn FieldsTotalColumn = new DataGridViewTextBoxColumn();
FieldsTotalColumn.Name = "FieldsTotal";
FieldsTotalColumn.DataPropertyName = "FieldsTotalInFile";
dgv.Columns.Add(FieldsTotalColumn);
DataGridViewColumn QuantityColumn = new DataGridViewTextBoxColumn();
QuantityColumn.Name = "Quantity";
QuantityColumn.DataPropertyName = "Quantity";
dgv.Columns.Add(QuantityColumn);
DataGridViewColumn SelectionColumn = new DataGridViewButtonColumn();
SelectionColumn.Name = "File Selection (Click to multiselect)";
SelectionColumn.DataPropertyName = "SelectionColumnTexte";
dgv.Columns.Add(SelectionColumn );
}
At this point my datagridview generate correctly and all my data is visible.
Now I want to filter lines by the File Name Columns and I don't know how I can do that. I have another variable (containing a combobox) to select the file name I want to filter by.
I try to use the BindingSource.Filter but with no success.
Can someone help me with that or explain to me if datagridview structure is too bad? I haven't DataTable (I did not understand its usefulness of it.)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
好吧……使用
list&lt; myClass&gt;
与bindingsource
的主要问题是list&lt;&gt;
不实现<<代码> iBindingListView 接口。dataTable
确实实现了此接口,在大多数示例中,您会看到dataTable
所使用的,而不是list&lt;&gt;
。除非您特别需要使用
bindingsource
简单地使用linq过滤list&lt; myClass&gt;
并将其用作网格的数据源可能会更容易。注意:使用bindingsource
并根据您的需求有很多优点>。我将在这个简单的示例中假设您要使用
list&lt; dataclass&gt;
及以下是如何过滤它的示例。您当前的代码有很多错别字,这很好奇为什么您不复制/粘贴原始代码,而不是像您看起来已经完成的那样重新订阅它。首先,看来代码不必要地将“按钮”文本添加到数据源…? …“选择”文本添加到数据源中,例如…
? …这很奇怪,似乎没有理由在每个
dataClass
对象的数据源中使用字符串“选择”。因此,我已经从dataclass
类中删除了该字段。相反,您可以将
dataGridViewButtonColumn
添加到所有按钮中显示的“选择”文本。当前代码的问题是代码行…网格按钮列有两个(2)个属性,我们需要设置为我们想要的“选择”文本……但是上面的代码是
selectioncolumn 到左侧的通用
dataGridViewColumn
……因此,我们需要的属性将无法提供。因此,我们需要将其更改为dataGridViewButtonColumn
以获取所需的属性。下面的事情应该可以工作……这应该消除将“选择”文本添加到数据源的需求。
下面的代码将
bindingsource
删除,并将list&lt; dataclass&gt;
用作dataSource
到网格。我将代码简化为两种方法,大多数工作都是在表单加载事件中完成的。我们将首先将
列表&lt; dataclass&gt;
作为全局变量,但是您可以在需要时避免使用。另外,下面的代码行是奇怪的,尚不清楚它的位置,但是您将在代码行中遇到问题……实际上没有必要……
myDatagridView
已经暴露于代码和无需为其“创建”一个新变量。因此,我所做的就是简单地将表单上的网格重命名为dgv
。这消除了我需要在当前代码中更改所有DGV
引用。尝试下面的代码,它应该以更简单的方式产生相同的结果……
首先,已更改的
dataclass
…。删除选择ColumnTexte
属性。然后,代码将数据填充数据……
最后,我在表单中添加了
按钮
,以说明如何在网格中过滤数据……。然后另一个
按钮
将网格滤网。我希望这有意义并有所帮助。
Well… the main problem you will have with using a
List<myClass>
with aBindingSource
is thatList<>
does not implement theIBindingListView
interface. ADataTable
does implement this interface and in most examples, you will see aDataTable
used and not aList<>
.Unless you specifically need to use the
BindingSource
it may be easier to simply use LINQ to filter theList<myClass>
and use it as a data source to the grid. NOTE: there are many advantages to using aBindingSource
and depending on your needs, you may want to change from aList<myClass>
to aDataTable
.I will assume for this simple example that you want to use a
List<Dataclass>
and below is an example of how you can filter it. Your current code has many typos and it is curious why you did not copy/paste the original code as opposed to re-typing it as it appears you have done.To start, it appears the code is unnecessarily adding the “Button” text to the data source…? … The “Select” text is added to the DATA SOURCE like…
? … This is very odd and there appears to be no reason to have the string “Select” INSIDE the data source for each
Dataclass
object. So, I have removed that field from theDataclass
Class.Instead, you can add the
DataGridViewButtonColumn
with the “Select” text displayed in all the buttons. The problem with the current code is the line of code…The grids button column has two (2) properties we need to set to set up the “Select” text as we want… however the code above is setting
SelectionColumn
to a genericDataGridViewColumn
on the left side… so, the properties we need will not available. Therefore, we need to change it to aDataGridViewBUTTONCOLUMN
to get the properties we need. Something like below should work…This should eliminate the need to add the “Select” text to the data source.
The code below drops the
BindingSource
and uses theList<Dataclass>
as aDataSource
to the grid. I simplified the code to just two methods and most of the work is done in the forms load event.We will first set the
List<Dataclass>
as a global variable, however you could avoid this if needed. Also, the line of code below is odd and it is unclear where it is called, however you will have a problem with the line of code…There is really NO need for this…
Mydatagridview
is ALREADY exposed to the code and there is not need to “create” a new variable for it. Therefore, is what I did is simply renamed the grid on the form todgv
. This eliminated my need to change all thedgv
references in the current code.Try the code below and it should produce the same results in a simpler fashion…
First the altered
Dataclass
…. With the removedSelectionColumnTexte
property.Then the code to fill the grid with data…
And finally, I added a
Button
to the form to demonstrate how you could filter the data in the grid….Then another
Button
to UN-filter the grid.I hope this makes sense and helps.