如何过滤/选择性地将值从一个DataGridView复制到另一个DataGridView [英] How to filter / selectively copy values from one DataGridView to another DataGridView

查看:192
本文介绍了如何过滤/选择性地将值从一个DataGridView复制到另一个DataGridView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有2个DataGridViews: productsDataGridView promotionsDataGridView

I have 2 DataGridViews: productsDataGridView and promotionsDataGridView.

第一个 productsDataGridView ,它使用以下方法从文件读取全部值:

The first one, productsDataGridView, which reads ALL values from a file with this method:

public static List<Products> LoadUserListFromFile(string filePath)
{
    var loadProductsData = new List<Products>();

    foreach (var line in File.ReadAllLines(filePath))
    {
        var columns = line.Split('\t');
        loadProductsData.Add(new Products
        {
            InventoryID = "BG" + columns[0],
            Brand = columns[1],
            Category = columns[2],
            Description = columns[3],
            Promotions = Convert.ToInt32(columns[4]),
            Quantity = Convert.ToInt32(columns[5]),
            Price = Convert.ToDouble(columns[6])
        });
    }

    return loadProductsData;
}

第一个DataGridView( productsDataGridView )正确填写与所有的值。现在,在我的 productsDataGridView 中,我设置了一个名为Promotion的复选框列(列从文件中读取整数值):如果值为0 - 该框不被选中,如果大于1:被检查。
现在我想做的是将 productsDataGridView promotionsDataGridView 之间的 FILTER / MOVE / strong>我们在复选框列(促销)中有一个> 0的值。

The first DataGridView (productsDataGridView) is filled correctly with ALL the values. Now in my productsDataGridView I have set-up a check-box column called "Promotion" (column Promotion reads integer values from the file): if it has value of 0 - the box is not checked, if greater than 1: is checked. Now what I WANT to do is to FILTER/MOVE (I don't care which of both exactly) the values from productsDataGridView to promotionsDataGridView where we have a >0 value in the check-box column (promotions).

示例:
如果productsDataGridView有25个总产品,其中8个是促销产品(在复选框列中的值> 0),promotionsDataGridView应该填充8个值,这些值被复制/移动/过滤/从DataGridView中任何一个。

Example: If productsDataGridView has 25 total products, from which 8 are promotional products (have value >0 in the check-box column), promotionsDataGridView should be filled with 8 values, which are copied/moved/filtered/whatever from DataGridView.

到目前为止,我只能使用以下代码将数据从第一个DataGridView复制到第二个:

So far I can only copy the data from the first DataGridView to the second one with the following code:

public void Experimental2()
{
    dataGridView1.DataSource = Products.LoadUserListFromFile(filePath);
    //Bind datagridview to linq 
    var dg1 =
        (from a in productsDataGridView.Rows.Cast<DataGridViewRow>()
         select new { Column1 = a.Cells["Column1"].Value.ToString() }).ToList();

    //loop dg1 and save it to datagridview2
    foreach (var b in dg1)
    {
        dataGridView1.Rows.Add(b.Column1);
    }
}

我做了一些可怜的尝试插入IF检查,这将为我做的工作(仅复制IF columnt [4]> 0),但我真的是新的DataGridView,所以我只是不能写任何甚至甚至可以编译所有...

I made few pitiful attempts to insert an IF check, which would do the job for me (copy only IF columnt[4] > 0) but I am really new to DataGridView so I just couldn't write anything which would even compile at all...

请帮帮我!

推荐答案

如果两个网格都有相同的模式(我假设他们有),那么我们要查找哪些行被检查,产品绑定到给定行,创建新的结果列表并将其绑定到下一个网格。

If both grids have same schema (and I assume they have) then we are going to find which rows are checked, get product bound to given row, create new result list and bind it to the next grid.

var results = new List<Products>(); //our new data source with only checked items

foreach (DataGridViewRow row in productsDataGridView.Rows)
{
    var item = row.DataBoundItem as Products; //get product from row (only when grid is databound!)

    if (item.Promotions > 0)
    {
        results.Add(item);        
    }
}

promotionsDataGridView.DataSource = results; 

如果要删除已检查的第一个网格中的行,请创建临时列表,添加到它检查行,并在结束时迭代它们,并逐个删除。希望能帮助你:)

If you want to delete rows from first grid that are checked then create temporary list of rows, add to it checked rows and at the end iterate over them and remove one by one. Hope that help You out :)

这篇关于如何过滤/选择性地将值从一个DataGridView复制到另一个DataGridView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆