c#,使用数据框来填充datagridview和filter行

发布于 2025-01-21 13:55:13 字数 3022 浏览 1 评论 0原文

我是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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

笑叹一世浮沉 2025-01-28 13:55:13

好吧……使用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;及以下是如何过滤它的示例。您当前的代码有很多错别字,这很好奇为什么您不复制/粘贴原始代码,而不是像您看起来已经完成的那样重新订阅它。

首先,看来代码不必要地将“按钮”文本添加到数据源…? …“选择”文本添加到数据源中,例如…

MyDataclass.Add(new Dataclass("Name1","10","10","Select"));

? …这很奇怪,似乎没有理由在每个dataClass对象的数据源中使用字符串“选择”。因此,我已经从dataclass类中删除了该字段。

相反,您可以将dataGridViewButtonColumn添加到所有按钮中显示的“选择”文本。当前代码的问题是代码行…

DataGridViewColumn SelectionColumn = new DataGridViewButtonColumn();

网格按钮列有两个(2)个属性,我们需要设置为我们想要的“选择”文本……但是上面的代码是selectioncolumn 到左侧的通用dataGridViewColumn……因此,我们需要的属性将无法提供。因此,我们需要将其更改为dataGridViewButtonColumn以获取所需的属性。下面的事情应该可以工作……

DataGridViewButtonColumn SelectionColumn = new DataGridViewButtonColumn();
SelectionColumn.Name = "Select"; 
SelectionColumn.Text = "Select";
SelectionColumn.UseColumnTextForButtonValue = true;
SelectionColumn.HeaderText = "File Selection (Click to multiselect)";

这应该消除将“选择”文本添加到数据源的需求。

下面的代码将bindingsource删除,并将list&lt; dataclass&gt;用作dataSource到网格。我将代码简化为两种方法,大多数工作都是在表单加载事件中完成的。

我们将首先将列表&lt; dataclass&gt;作为全局变量,但是您可以在需要时避免使用。另外,下面的代码行是奇怪的,尚不清楚它的位置,但是您将在代码行中遇到问题……

public static DatagridView dgv = this.Mydatagridview;

实际上没有必要……myDatagridView已经暴露于代码和无需为其“创建”一个新变量。因此,我所做的就是简单地将表单上的网格重命名为dgv。这消除了我需要在当前代码中更改所有DGV引用。

尝试下面的代码,它应该以更简单的方式产生相同的结果……

首先,已更改的dataclass…。删除选择ColumnTexte属性。

public class Dataclass {
  public string FileName { get; set; }
  public string FieldsTotal { get; set; }
  public string Quantity { get; set; }

  public Dataclass(string fileName, string fieldsTotal, string quantity) {
    FileName = fileName;
    FieldsTotal = fieldsTotal;
    Quantity = quantity;
  }
} 

然后,代码将数据填充数据……

public List<Dataclass> MyDataclass = new List<Dataclass>();

public Form1() {
  InitializeComponent();
}

private void Form1_Load(object sender, EventArgs e) {
  MyDataclass.Add(new Dataclass("Name1", "10", "10"));
  MyDataclass.Add(new Dataclass("Name2", "20", "20"));
  MyDataclass.Add(new Dataclass("Name3", "30", "30"));
  dgv.AutoGenerateColumns = false;
  dgv.AutoSize = true;
  dgv.DataSource = MyDataclass;
  CreateDifferentColumnAndFillItWithData();
}


public 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";
  FieldsTotalColumn.DataPropertyName = "FieldsTotal";
  dgv.Columns.Add(FieldsTotalColumn);
  DataGridViewColumn QuantityColumn = new DataGridViewTextBoxColumn();
  QuantityColumn.Name = "Quantity";
  QuantityColumn.DataPropertyName = "Quantity";
  dgv.Columns.Add(QuantityColumn);
  DataGridViewButtonColumn SelectionColumn = new DataGridViewButtonColumn();
  SelectionColumn.Name = "Select"; 
  SelectionColumn.Text = "Select";
  SelectionColumn.UseColumnTextForButtonValue = true;
  SelectionColumn.HeaderText = "File Selection (Click to multiselect)";
  dgv.Columns.Add(SelectionColumn);
}

最后,我在表单中添加了按钮,以说明如何在网格中过滤数据……。

private void button1_Click(object sender, EventArgs e) {
  var filter = MyDataclass.Where(x => x.FileName == "Name2").ToList();
  dgv.DataSource = filter;
}

然后另一个按钮将网格滤网。

private void button2_Click(object sender, EventArgs e) {
  dgv.DataSource = MyDataclass;
}

我希望这有意义并有所帮助。

Well… the main problem you will have with using a List<myClass> with a BindingSource is that List<> does not implement the IBindingListView interface. A DataTable does implement this interface and in most examples, you will see a DataTable used and not a List<>.

Unless you specifically need to use the BindingSource it may be easier to simply use LINQ to filter the List<myClass> and use it as a data source to the grid. NOTE: there are many advantages to using a BindingSource and depending on your needs, you may want to change from a List<myClass> to a DataTable.

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…

MyDataclass.Add(new Dataclass("Name1","10","10","Select"));

? … 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 the Dataclass 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…

DataGridViewColumn SelectionColumn = new DataGridViewButtonColumn();

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 generic DataGridViewColumn on the left side… so, the properties we need will not available. Therefore, we need to change it to a DataGridViewBUTTONCOLUMN to get the properties we need. Something like below should work…

DataGridViewButtonColumn SelectionColumn = new DataGridViewButtonColumn();
SelectionColumn.Name = "Select"; 
SelectionColumn.Text = "Select";
SelectionColumn.UseColumnTextForButtonValue = true;
SelectionColumn.HeaderText = "File Selection (Click to multiselect)";

This should eliminate the need to add the “Select” text to the data source.

The code below drops the BindingSource and uses the List<Dataclass> as a DataSource 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…

public static DatagridView dgv = this.Mydatagridview;

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 to dgv. This eliminated my need to change all the dgv 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 removed SelectionColumnTexte property.

public class Dataclass {
  public string FileName { get; set; }
  public string FieldsTotal { get; set; }
  public string Quantity { get; set; }

  public Dataclass(string fileName, string fieldsTotal, string quantity) {
    FileName = fileName;
    FieldsTotal = fieldsTotal;
    Quantity = quantity;
  }
} 

Then the code to fill the grid with data…

public List<Dataclass> MyDataclass = new List<Dataclass>();

public Form1() {
  InitializeComponent();
}

private void Form1_Load(object sender, EventArgs e) {
  MyDataclass.Add(new Dataclass("Name1", "10", "10"));
  MyDataclass.Add(new Dataclass("Name2", "20", "20"));
  MyDataclass.Add(new Dataclass("Name3", "30", "30"));
  dgv.AutoGenerateColumns = false;
  dgv.AutoSize = true;
  dgv.DataSource = MyDataclass;
  CreateDifferentColumnAndFillItWithData();
}


public 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";
  FieldsTotalColumn.DataPropertyName = "FieldsTotal";
  dgv.Columns.Add(FieldsTotalColumn);
  DataGridViewColumn QuantityColumn = new DataGridViewTextBoxColumn();
  QuantityColumn.Name = "Quantity";
  QuantityColumn.DataPropertyName = "Quantity";
  dgv.Columns.Add(QuantityColumn);
  DataGridViewButtonColumn SelectionColumn = new DataGridViewButtonColumn();
  SelectionColumn.Name = "Select"; 
  SelectionColumn.Text = "Select";
  SelectionColumn.UseColumnTextForButtonValue = true;
  SelectionColumn.HeaderText = "File Selection (Click to multiselect)";
  dgv.Columns.Add(SelectionColumn);
}

And finally, I added a Button to the form to demonstrate how you could filter the data in the grid….

private void button1_Click(object sender, EventArgs e) {
  var filter = MyDataclass.Where(x => x.FileName == "Name2").ToList();
  dgv.DataSource = filter;
}

Then another Button to UN-filter the grid.

private void button2_Click(object sender, EventArgs e) {
  dgv.DataSource = MyDataclass;
}

I hope this makes sense and helps.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文