我要删除所有BoundField列并保留TemplateField列的每一列都有两列。我按照该网站https://www.aspsnippets.com/Articles/GridView-with-CheckBox-Get-Selected-Rows-in-ASPNet.aspx上的说明进行操作,但是我确实有一张桌子,但是TemplateField代表BoundField。我希望能够使用旧表并从表中删除boundField。
这是我的桌子的样子
<asp:GridView ID="gvPrimaryGrid" runat="server" AutoGenerateColumns="false" ShowFooter="true" onrowdatabound="gvPrimaryGrid_RowDataBound"
ShowHeaderWhenEmpty="true" AllowPaging="True" OnPageIndexChanging="gridView_PageIndexChanging" DataKeyNames="compras_id"
OnRowCommand="gvPrimaryGrid_RowCommand" OnRowEditing="gvPrimaryGrid_RowEditing" OnRowCancelingEdit="gvPrimaryGrid_RowCancelingEdit"
OnRowUpdating="gvPrimaryGrid_RowUpdating" CellPadding="3" AllowUserToResizeColumns="True" PageSize="5" AllowSorting="true" onsorting="gvPrimaryGrid_Sorting" GridLines="None" CssClass="mGrid" PagerStyle-CssClass="pgr" AlternatingRowStyle-CssClass="alt">
<PagerSettings Mode="Numeric" />
<Columns>
<asp:TemplateField>
<ItemTemplate>
<%--<asp:CheckBox ID="CheckBox1" AutoPostBack="true" EnableViewState="true" runat="server" onclick="Check_Click(this)" OnCheckedChanged="chkRow_CheckChanged" />--%>
<asp:CheckBox ID="chkRow" AutoPostBack="true" runat="server" OnCheckedChanged="chkRow_CheckChanged" />
<%--<asp:CheckBox ID="chkRow" runat="server" />--%>
<asp:ImageButton ImageUrl="~/Images/edit.png" runat="server" CommandName="Edit" ToolTip="Edit" Width="20px" Height="20px"/>
<asp:ImageButton ImageUrl="~/Images/delete.png" runat="server" CommandName="Delete" ToolTip="Delete" Width="20px" Height="20px"/>
</ItemTemplate>
<EditItemTemplate>
<asp:ImageButton ImageUrl="~/Images/save.png" runat="server" CommandName="Update" ToolTip="Update" Width="20px" Height="20px"/>
<asp:ImageButton ImageUrl="~/Images/cancel.png" runat="server" CommandName="Cancel" ToolTip="Cancel" Width="20px" Height="20px"/>
</EditItemTemplate>
<FooterTemplate>
<asp:ImageButton ImageUrl="~/Images/addnew.png" runat="server" CommandName="AddNew" ToolTip="Add New" Width="20px" Height="20px"/>
</FooterTemplate>
</asp:TemplateField>
<asp:BoundField DataField = "compras_id" HeaderText="Compras ID" HtmlEncode="false" />
<asp:BoundField DataField = "numero_contrato" HeaderText = "Numero contrato" HtmlEncode="false" />
<asp:BoundField DataField="marca" HeaderText="Marca" HtmlEncode="false" />
<asp:BoundField DataField="designacion" HeaderText="Designacion" HtmlEncode="false" />
<asp:BoundField DataField="tipo" HeaderText="Tipo" HtmlEncode="false" />
<asp:BoundField DataField="referencia" HeaderText="Referencia" HtmlEncode="false" />
<asp:BoundField DataField="plazo" HeaderText="Plazo" HtmlEncode="false" />
<asp:BoundField DataField="nombre_proveedor" HeaderText="Proveedor" HtmlEncode="false" />
<asp:BoundField DataField="cantidad_requerida" HeaderText="Cantidad requerida" HtmlEncode="false"/>
<asp:BoundField DataField="cantidad_pedida" HeaderText="Cantidad pedida" HtmlEncode="false"/>
<asp:TemplateField HeaderText="Compras ID" SortExpression="compras_id" Visible="false">
<ItemTemplate>
<asp:Label DataField="compras_id" HtmlEncode="false" ID="lblPurschaseID" CssClass="gridTextbox" Text='<%# Eval("compras_id") %>' runat="server" />
</ItemTemplate>
<%--<EditItemTemplate>
<asp:TextBox CssClass="gridTextbox" ID="txtType" Text='<%# Eval("tipo") %>' runat="server" />
</EditItemTemplate>--%>
<FooterTemplate>
<asp:TextBox CssClass="gridTextbox" ID="txtPurschaseIDFooter" runat="server" />
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Numero contrato" >
<ItemTemplate>
<asp:Label DataField="numero_contrato" HtmlEncode="false" ID="lblContractNumber" CssClass="gridTextbox" Text='<%# Eval("numero_contrato") %>' runat="server" />
</ItemTemplate>
<EditItemTemplate>
<asp:DropDownList ID="drGridContractID" Runat="server" AutoPostBack="false" OnSelectedIndexChanged="DropDown_SelectedIndexChanged" />
<%-- <asp:TextBox CssClass="gridTextbox" ID="txtSupplier" Text='<%# Eval("nombre_proveedor") %>' runat="server" />--%>
</EditItemTemplate>
<FooterTemplate>
<asp:TextBox CssClass="gridTextbox" ID="txtContractNumberFooter" runat="server" />
</FooterTemplate>
</asp:TemplateField>
我想更改AddRow和Remove行,以便它与templatefield一起使用,而不是BoundField
private DataTable AddRow(GridViewRow gvRow, DataTable dt)
{
// gvRow.Cells[1].Text
DataRow[] dr = dt.Select("compras_id = '" + gvPrimaryGrid.Rows[0].Cells[1].Text+ "'");
if (dr.Length <= 0)
{
int i = 1;
dt.Rows.Add();
dt.Rows[dt.Rows.Count - 1]["compras_id"] = gvRow.Cells[i++].Text;
dt.Rows[dt.Rows.Count - 1]["numero_contrato"] = gvRow.Cells[i++].Text;
dt.Rows[dt.Rows.Count - 1]["marca"] = gvRow.Cells[i++].Text;
dt.Rows[dt.Rows.Count - 1]["designacion"] = gvRow.Cells[i++].Text;
dt.Rows[dt.Rows.Count - 1]["tipo"] = gvRow.Cells[i++].Text;
dt.Rows[dt.Rows.Count - 1]["referencia"] = gvRow.Cells[i++].Text;
dt.Rows[dt.Rows.Count - 1]["plazo"] = gvRow.Cells[i++].Text;
dt.Rows[dt.Rows.Count - 1]["nombre_proveedor"] = gvRow.Cells[i++].Text;
//dt.Rows[dt.Rows.Count - 1]["cantidad_requerida"] = gvRow.Cells[i++].Text;
dt.Rows[dt.Rows.Count - 1]["cantidad_pedida"] = gvRow.Cells[i++].Text;
dt.Rows[dt.Rows.Count - 1]["cantidad_entregada"] = gvRow.Cells[i++].Text;
dt.AcceptChanges();
}
return dt;
}
private DataTable RemoveRow(GridViewRow gvRow, DataTable dt)
{
DataRow[] dr = dt.Select("compras_id = '" + gvRow.Cells[1].Text + "'");
if (dr.Length > 0)
{
dt.Rows.Remove(dr[0]);
dt.AcceptChanges();
}
return dt;
}
答案 0 :(得分:0)
我在add方法中使用了标签,而不是gvRow.Cells [i ++]。文本和代码按我的意愿工作。
private DataTable AddRow(GridViewRow gvRow, DataTable dt)
{
String purchasesID = gvRow.Cells[1].Text.ToString();
DataRow[] dr = dt.Select("compras_id = '" + purchasesID + "'");
if (dr.Length <= 0)
{
Label brand = gvRow.FindControl("lblBrand") as Label;
Label numero_contrato = gvRow.FindControl("lblContractNumber") as Label;
Label designacion = gvRow.FindControl("lblDesignacion") as Label;
Label type = gvRow.FindControl("lblType") as Label;
Label reference = gvRow.FindControl("lblReference") as Label;
Label paymentDeadline = gvRow.FindControl("lblPaymentDeadline") as Label;
Label supplier = gvRow.FindControl("lblSupplier") as Label;
Label deliveredQuantity = gvRow.FindControl("lblDeliveredQuantity") as Label;
Label requestedQuantity = gvRow.FindControl("lblRequestedQuantity") as Label;
dt.Rows.Add();
dt.Rows[dt.Rows.Count - 1]["compras_id"] = purchasesID;
dt.Rows[dt.Rows.Count - 1]["numero_contrato"] = numero_contrato.Text;
dt.Rows[dt.Rows.Count - 1]["marca"] = brand.Text;
dt.Rows[dt.Rows.Count - 1]["designacion"] = designacion.Text;
dt.Rows[dt.Rows.Count - 1]["tipo"] = type.Text;
dt.Rows[dt.Rows.Count - 1]["referencia"] = reference.Text; ;
dt.Rows[dt.Rows.Count - 1]["plazo"] = paymentDeadline.Text;
dt.Rows[dt.Rows.Count - 1]["nombre_proveedor"] = supplier.Text;
dt.Rows[dt.Rows.Count - 1]["cantidad_pedida"] = requestedQuantity.Text;
dt.Rows[dt.Rows.Count - 1]["cantidad_entregada"] = deliveredQuantity.Text;
dt.AcceptChanges();
}
return dt;
}