我必须在第一个表中的表上,由用户选择要添加的行,并在每行中添加一个复选框,然后按按钮在第一个表下添加行。但是我不希望已经添加的行被删除并再次添加。我只想添加尚未添加的行。这是我的代码
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();
}
答案 0 :(得分: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(....)
希望这会有所帮助!