本文介绍了在asp.net中使用TemplateField添加或删除行时,数据表为空的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要删除所有BoundField列并保留TemplateField列的每一列都有两列。我按照此网站上的说明进行操作



这是我的桌子的样子

 < 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_RowCancelingColumn UpdowdRespondingEdit b $ bOnb = 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 =添加新 Width = 20px Height = 20px />
< / FooterTemplate>
< / asp:TemplateField>

< asp:BoundField DataField = compras_id HeaderText =比较ID HtmlEncode = false />
< asp:BoundField DataField = numero_contrato HeaderText =与Numero相反 Ht​​mlEncode = 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 = Cedidad pedida HtmlEncode = false />

< asp:TemplateField HeaderText =比较ID SortExpression = compras_id可见= 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反对>
< 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

 私有DataTable AddRow(GridViewRow gvRow,DataTable dt)
{
// gvRow.Cells [1]。文本
DataRow [] dr = dt.Select( compras_id =' + gvPrimaryGrid.Rows [0] .Cells [1] .Text +');
if(dr.Length {
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;
}

私有DataTable RemoveRow(GridViewRow gvRow,DataTable dt)
{
DataRow [] dr = dt.Select( compras_id =' + gvRow.Cells [1]。文字+');
if(dr.Length> 0)
{
dt.Rows.Remove(dr [0]);
dt.AcceptChanges();
}
return dt;
}


解决方案

我在添加标签时使用了标签

  private DataTable AddRow(GridViewRow gvRow,数据表dt)
{
字符串PurchasesID = gvRow.Cells [1] .Text.ToString();

DataRow [] dr = dt.Select( compras_id =’ + PurchasesID +’);
if(dr.Length {

Label brand = gvRow.FindControl( lblBrand)作为Label;
Label numero_contrato = gvRow.FindControl( lblContractNumber)作为Label;
Label designacion = gvRow.FindControl( lblDesignacion)作为Label;
标签类型= gvRow.FindControl( lblType)作为标签;
标签参考= gvRow.FindControl( lblReference)作为标签;
标签paymentDeadline = gvRow.FindControl( lblPaymentDeadline)作为标签;
标签供应商= gvRow.FindControl( lblSupplier)作为标签;
Label deliveryQuantity = gvRow.FindControl( lblDeliveredQuantity)作为Label;
Label requiredQuantity = gvRow.FindControl( lblRequestedQuantity)作为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] = provider.Text;
dt.Rows [dt.Rows.Count-1] [ cantidad_pedida] = requestQuantity.Text;
dt.Rows [dt.Rows.Count-1] [ cantidad_entregada] = deliveryQuantity.Text;
dt.AcceptChanges();
}
return dt;
}


I have two columns for every columns I want to remove All the BoundField columns and keep the TemplateField columns. I followed the instruction on this website https://www.aspsnippets.com/Articles/GridView-with-CheckBox-Get-Selected-Rows-in-ASPNet.aspx however I alredy had a table but with TemplateField instand of BoundField. I want to be able to use my old table and remove the boundField from my table.

Here is a image of what I want to remove in red.

Here is where my table looks like

<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>    

I want to change the AddRow and Remove row so it works with templatefield instead of the 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;
    }
解决方案

I used labels to in add method instead of gvRow.Cells[i++].Text and the code works as I wanted.

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

这篇关于在asp.net中使用TemplateField添加或删除行时,数据表为空的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-30 22:12