【问题标题】:How to compare two rows from datagrid?如何比较数据网格中的两行?
【发布时间】:2019-03-11 15:02:31
【问题描述】:

我必须在第一个表中的表 de 用户选择要添加的行,并在每行中添加一个复选框,然后按下一个按钮在第一个表下添加行。但是我不希望已经添加的行被删除并再次添加。我只想添加尚未添加的行。这是我的代码

    protected void GetSelectedRecords(object sender, EventArgs e)
    {
        DataTable dt = new DataTable();

        dt.Columns.AddRange(new DataColumn[10] { new DataColumn("Marca"), new DataColumn("Designacion"), new DataColumn("Tipo"),
                                                new DataColumn("Referencia"), new DataColumn("Plazo"),new DataColumn("nombre_proveedor"),
                                                new DataColumn("cantidad_requerida"),new DataColumn("cantidad_pedida"), new DataColumn("cantidad_entregada"),
                                                new DataColumn("precio_unitario")});
        foreach (GridViewRow row in gvPurchases.Rows)
        {
            if (row.RowType == DataControlRowType.DataRow)
            {
                CheckBox chkRow = (row.Cells[2].FindControl("chkRow") as CheckBox);
                if (chkRow.Checked)
                {
                    string brand = (row.Cells[1].FindControl("lblMarca") as Label).Text;
                    string designation = (row.Cells[1].FindControl("lblDesignacion") as Label).Text;
                    string type = (row.Cells[1].FindControl("lblType") as Label).Text;
                    string reference = (row.Cells[1].FindControl("lblReference") as Label).Text;
                    string paymentDeadLine = (row.Cells[1].FindControl("lblPaymentDeadline") as Label).Text;
                    string supplier = drSupplier.SelectedItem.Text; 
                    string requiredQuantity = (row.Cells[1].FindControl("lblrequiredQuantity") as Label).Text; 
                    string requestedQuantity = (row.Cells[1].FindControl("lblRequestedQuantity") as Label).Text;
                    string deliveredQuantity = (row.Cells[1].FindControl("lblDeliveredQuantity") as Label).Text;
                    string unitPrice = (row.Cells[1].FindControl("lblUnitPrice") as Label).Text;

                    dt.Rows.Add(brand, designation, type, reference, paymentDeadLine, supplier, requestedQuantity, deliveredQuantity, deliveredQuantity, unitPrice);
                }
            }
        }
        gvPurchasesSelected.DataSource = dt;
        gvPurchasesSelected.DataBind();
    }

【问题讨论】:

    标签: asp.net datagrid


    【解决方案1】:

    您可以在执行 dt.Rows.Add 之前检查使用 Linq 或执行 foreach 或您喜欢的任何迭代方法而不是 gvPurchasesSelected.Rows 并检查品牌、名称和类型是否相同(或组成的字段行的唯一标识符)。

    我可能会这样做:

        if ( gvPurchasesSelected.Rows.Cast<DataGridViewRow>().Any(row => row.type == type && 
                                                 row.brand == brand && 
                                                 row.designation == designation))
            continue;
        else
            dt.Rows.Add(....)
    

    希望这会有所帮助!

    【讨论】:

    • 当我尝试添加您的代码时,我收到以下错误 GridviewRowCollection 不包含“任何”的定义。对于行,我得到以下错误“行”不能在此范围内声明,因为该名称在封闭的本地范围中使用我能够通过简单地将其重命名为 rowSlected 来修复行错误但是我不知道如何修复错误我得到任何
    • Ups,抱歉,我忘了你需要转换,因为 Rows 不是 IEnumerable。在 Any 之前添加它,它应该可以工作: .Cast()
    • 嗯...我无法复制该错误。您能否告诉我 gvPurchasesSelected 的类型,以便确定。小心 Lamba 函数,你写了 rowSelected => 然后你使用 row 而不是 rowSelected,它们必须匹配。
    • 好的,尝试将 DataGridViewRow 更改为 GridViewRow,看看是否可行。我的测试项目实际上是一个桌面应用程序而不是 Web,所以我使用的是 DataGridView 而不是 GridView。让我知道它是否适用于最后的更改,以便我编辑答案,您可以将其标记为已解决。
    • 因为您没有更改 rowSelected for row。看看上面我在箭头函数中所说的几个 cmets,第一个变量必须与您在箭头右侧使用的变量相匹配。应该是这样的: if (gvPurchasesSelected.Rows.Cast().Any(rowSelected => rowSelected .type == type && rowSelected .brand == brand && rowSelected .designation == designation))
    猜你喜欢
    • 1970-01-01
    • 2020-06-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-09-03
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多