Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/matlab/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Asp.net 如何比较datagrid中的两行?_Asp.net_Datagrid - Fatal编程技术网

Asp.net 如何比较datagrid中的两行?

Asp.net 如何比较datagrid中的两行?,asp.net,datagrid,Asp.net,Datagrid,我必须在第一个表中的表中取消用户选择要添加的行,并在每行中添加复选框,然后按下按钮在第一个表下添加行。但是,我不希望删除并再次添加已添加的行。我只想添加尚未添加的行。这是我的密码 protected void GetSelectedRecords(object sender, EventArgs e) { DataTable dt = new DataTable(); dt.Columns.AddRange(new DataColumn[10]

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

    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();
    }

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

我可能会这样做:

    if ( gvPurchasesSelected.Rows.Cast<DataGridViewRow>().Any(row => row.type == type && 
                                             row.brand == brand && 
                                             row.designation == designation))
        continue;
    else
        dt.Rows.Add(....)
if(gvPurchasesSelected.Rows.Cast().Any(row=>row.type==type&&
row.brand==品牌和
行名称==名称)
继续;
其他的
dt.行。添加(…)

希望这有帮助

当我尝试添加代码时,会出现以下错误GridviewRowCollection不包含“Any”的定义。对于行,我得到以下错误“row”不能在此范围内声明,因为该名称在封闭的本地范围内使用。我可以通过简单地将其重命名为rowSlected来修复row错误。但是,我不知道如何修复我在anyUps中得到的错误,很抱歉,我忘记了您需要强制转换,因为行是不可数的。将此添加到Any之前,它应该可以工作:.Cast()Mmmmm。。。我不能重复那个错误。您能告诉我您选择的GVPURCHASES类型是为了确定吗。小心使用Lamba函数,您编写了rowSelected=>然后使用row而不是rowSelected,这些必须匹配。好的,尝试将DataGridViewRow更改为GridViewRow,看看是否有效。我的tester项目实际上是一个桌面应用程序而不是web,所以我使用的是DataGridView而不是GridView。让我知道它是否适用于最后的更改,以便我编辑答案,您可以将其标记为已解决。因为您没有更改为行选择的行。请看上面我在arrow函数中说过的几个注释,第一个变量必须与箭头右侧使用的变量相匹配。它应该是这样的:if(gvPurchasesSelected.Rows.Cast().Any(rowSelected=>rowSelected.type==type&&rowSelected.brand==brand&&rowSelected.designation==designation))