如何动态显示GridView中的CheckBox?

时间:2017-05-18 10:05:32

标签: asp.net vb.net gridview checkbox

我有一个GridView我希望CheckBox只显示状态(列名称)='已发送'的行,而不是{。}}。

ASPX

<asp:GridView ID="gvPOItems" runat="server" AutoGenerateColumns="False" 
Width="100%" role="grid" aria-describedby="example_info">
 <Columns>
 <asp:BoundField DataField="ItemNumber" HeaderText="Row Number" ItemStyle-CssClass="hide"
  HeaderStyle-CssClass="hide" />
<asp:TemplateField HeaderText="Cancel SO Line Item">
  <ItemTemplate>
 <asp:checkbox ID="cbSOCan" runat="server"></asp:checkbox>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Description">
 <ItemTemplate>
<asp:TextBox ID="txtDesc" CssClass="form-control grid-textbox-big" MaxLength="200"
runat="server"></asp:TextBox>
 </ItemTemplate>
 </asp:TemplateField>
<asp:TemplateField HeaderText="Quantity">
 <ItemTemplate>
<asp:TextBox ID="txtQuantity" CssClass="form-control grid-textbox-small" runat="server"></asp:TextBox>
</ItemTemplate>
 </asp:TemplateField>
 <asp:TemplateField HeaderText="Unit $">
<ItemTemplate>
<asp:TextBox ID="txtUnitPrice" CssClass="form-control grid-textbox-small" runat="server"></asp:TextBox>
 </ItemTemplate>
</asp:TemplateField>
 <asp:TemplateField HeaderText="Total $">
   <ItemTemplate>
 <asp:TextBox ID="txtTotal" Enabled="false" CssClass="form-control grid-textbox-small" runat="server"></asp:TextBox>
  </ItemTemplate>
 </asp:TemplateField>
<asp:TemplateField HeaderText="Account Code">
  <ItemTemplate>
 <div class="grid-code-tbl">
 <asp:Label ID="lblAccountCode" Text='<%# Eval("ACCTNUM") %>' Visible="false" runat="server"></asp:Label>
 <asp:Label ID="lblACCTNUMDesc" Text='<%# Eval("ACCTNUMDesc") %>' CssClass="grid-code-cell form-control"  runat="server"></asp:Label>
<asp:Label ID="lblIsCAPEXJOBCODE" runat="server" Text='<%# Eval("IsCAPEXJOBCODE") %>' Visible="false"></asp:Label>
 <div class="grid-code-cell grid-code-icon" runat="server" id="AccountCodeDiv">
<asp:LinkButton ID="lnkAccountCode" CssClass="btn btn-default" CommandName="SelectAccountCode" runat="server"><i class="fa fa-th-list"></i></asp:LinkButton></div>
 </div>
 </ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Tax ID">
<ItemTemplate>
 <asp:DropDownList ID="ddlTaxID" runat="server" CssClass="form-control grid-textbox-small">
  </asp:DropDownList>
 </ItemTemplate>
  </asp:TemplateField>
 <asp:TemplateField HeaderText="Status">
    <ItemTemplate>
  <asp:checkbox ID="cbSOCan" CssClass="gridCB" runat="server" Visible='<%# (Eval("StatusFieldName").ToString() == "Sent")%>'></asp:checkbox></asp:Label>
 </ItemTemplate>
 </asp:TemplateField>
   <asp:TemplateField HeaderText="From Date">
  <ItemTemplate>
  <div class="input-group grid-datebox">
  <asp:TextBox ID="txtFromDate" runat="server" MaxLength="10" placeholder="DD/MM/YYYY" CssClass="form-control"></asp:TextBox>
<span class="input-group-addon calendar-icon" id="FromCalSpan"><i class="fa fa-calendar">
 </i></span>
   </div>
 </ItemTemplate>
 </asp:TemplateField>
   <asp:TemplateField HeaderText="To Date">
   <ItemTemplate>
<div class="input-group grid-datebox">
  <asp:TextBox ID="txtToDate" runat="server" MaxLength="10" placeholder="DD/MM/YYYY"  CssClass="form-control"></asp:TextBox>
  <span class="input-group-addon calendar-icon" id="ToCalSpan"><i class="fa fa-calendar"> </i></span>
 </div>
 </ItemTemplate>
 </asp:TemplateField>
<asp:TemplateField HeaderText="Capital Project (Optional)">
  <ItemTemplate>
 <div class="grid-code-tbl">
  <asp:Label ID="lblJobCode" CssClass="grid-code-cell form-control" Text='<%# Eval("JOBCODE") %>'runat="server"></asp:Label>
 <div class="grid-code-cell grid-code-icon" runat="server" id="JobCodeDiv">
<asp:LinkButton ID="lnkJobCode" CommandName="SelectJobCode" runat="server" class="btn btn-default"><i class="fa fa-th-list"></i></asp:LinkButton></div>
 </div>
   </ItemTemplate>
  </asp:TemplateField>
<asp:TemplateField HeaderText="Invoice Number">
 <ItemTemplate>
  <asp:TextBox CssClass="form-control hiddenElm" ID="txtInvoiceNumber" onblur="SetInvoiceNumber(this.value);"
Text='<%# Eval("InvoiceNumber") %>' runat="server"></asp:TextBox>
 </ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="HISTSTATUS" HeaderText="Payment Status" />
 <asp:BoundField DataField="HISTCHECKDT" HeaderText="Date Paid" DataFormatString="{0:dd/MM/yyyy}" />
  <asp:BoundField DataField="HISTFRIDNO" HeaderText="Funds Request#" />
 <asp:BoundField DataField="HISTFRIDNO" HeaderText="Attachment" />
</Columns>
 </asp:GridView>

代码

Protected Sub btnCancelItem_Click(sender As Object, e As EventArgs) Handles btnCancelItem.Click 
    For Each gvrow As GridViewRow In gvPOItems.Rows
        Dim chkdelte As CheckBox = DirectCast(gvrow.FindControl("cbSOCan"), CheckBox)
        If chkdelte.Checked Then
            ' gvAdditionalArea.Rows(rowIndex).Cells(0).Text()
            Dim ItemNumber As Int32 = Convert.ToInt32(gvrow.Cells(0).Text())
            Queries.CancelSOlineItem(ItemNumber, txtPONumber.Text)
            gvrow.Cells(0).Text() = "Cancelled"
        End If
    Next
    ' gvPOItems.DataBind()
End Sub

 Protected Sub gvPOItems_RowDataBound(sender As Object, e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles gvPOItems.RowDataBound
    Dim lblStatus As Label = DirectCast(e.Row.FindControl("lblStatus"), Label)
    If lblStatus.Text = "Sent" Then  
        e.Row.Cells(1).Visible = True
    Else
        e.Row.Cells(1).Visible = False
    End If
End Sub

grid

1 个答案:

答案 0 :(得分:0)

您可以使用已有的值来填充表格。

<asp:templatefield HeaderText="Cancel SO Line Item">
    <itemtemplate>
        <asp:checkbox ID="cbSOCan" CssClass="gridCB" runat="server" Visible='<%# (Eval("StatusFieldName").ToString() == "Sent")%>'></asp:checkbox>
    </itemtemplate>
</asp:templatefield>

希望这有帮助!

修改....

当我输入StatusFieldName时,我需要该字段的名称。

<asp:GridView ID="gvPOItems" runat="server" AutoGenerateColumns="False" Width="100%" role="grid" aria-describedby="example_info">
<Columns>
    <asp:BoundField DataField="ItemNumber" HeaderText="Row Number" ItemStyle-CssClass="hide" HeaderStyle-CssClass="hide" />
    <asp:TemplateField HeaderText="Cancel SO Line Item">
        <ItemTemplate>
            <asp:checkbox ID="cbSOCan" runat="server"></asp:checkbox>
        </ItemTemplate>
    </asp:TemplateField>
    <asp:TemplateField HeaderText="Description">
        <ItemTemplate>
            <asp:TextBox ID="txtDesc" CssClass="form-control grid-textbox-big" MaxLength="200" runat="server"></asp:TextBox>
        </ItemTemplate>
    </asp:TemplateField>
    <asp:TemplateField HeaderText="Quantity">
        <ItemTemplate>
            <asp:TextBox ID="txtQuantity" CssClass="form-control grid-textbox-small" runat="server"></asp:TextBox>
        </ItemTemplate>
    </asp:TemplateField>
    <asp:TemplateField HeaderText="Unit $">
        <ItemTemplate>
            <asp:TextBox ID="txtUnitPrice" CssClass="form-control grid-textbox-small" runat="server"></asp:TextBox>
        </ItemTemplate>
    </asp:TemplateField>
    <asp:TemplateField HeaderText="Total $">
        <ItemTemplate>
            <asp:TextBox ID="txtTotal" Enabled="false" CssClass="form-control grid-textbox-small" runat="server"></asp:TextBox>
        </ItemTemplate>
    </asp:TemplateField>
    <asp:TemplateField HeaderText="Account Code">
        <ItemTemplate>
            <div class="grid-code-tbl">
                <asp:Label ID="lblAccountCode" Text='<%# Eval("ACCTNUM") %>' Visible="false" runat="server"></asp:Label>
                <asp:Label ID="lblACCTNUMDesc" Text='<%# Eval("ACCTNUMDesc") %>' CssClass="grid-code-cell form-control"  runat="server"></asp:Label>
                <asp:Label ID="lblIsCAPEXJOBCODE" runat="server" Text='<%# Eval("IsCAPEXJOBCODE") %>' Visible="false"></asp:Label>
                <div class="grid-code-cell grid-code-icon" runat="server" id="AccountCodeDiv">
                    <asp:LinkButton ID="lnkAccountCode" CssClass="btn btn-default" CommandName="SelectAccountCode" runat="server"><i class="fa fa-th-list"></i></asp:LinkButton>
                </div>
            </div>
        </ItemTemplate>
    </asp:TemplateField>
    <asp:TemplateField HeaderText="Tax ID">
        <ItemTemplate>
            <asp:DropDownList ID="ddlTaxID" runat="server" CssClass="form-control grid-textbox-small"></asp:DropDownList>
         </ItemTemplate>
    </asp:TemplateField>
    <asp:TemplateField HeaderText="Status">
        <ItemTemplate>
            <asp:checkbox ID="cbSOCan" CssClass="gridCB" runat="server" Visible='<%# (Eval("HISTSTATUS").ToString() IS "Sent")%>'></asp:checkbox></asp:Label>
        </ItemTemplate>
    </asp:TemplateField>
    <asp:TemplateField HeaderText="From Date">
        <ItemTemplate>
            <div class="input-group grid-datebox">
                <asp:TextBox ID="txtFromDate" runat="server" MaxLength="10" placeholder="DD/MM/YYYY" CssClass="form-control"></asp:TextBox>
                <span class="input-group-addon calendar-icon" id="FromCalSpan"><i class="fa fa-calendar"></i></span>
            </div>
        </ItemTemplate>
    </asp:TemplateField>
    <asp:TemplateField HeaderText="To Date">
        <ItemTemplate>
            <div class="input-group grid-datebox">
                <asp:TextBox ID="txtToDate" runat="server" MaxLength="10" placeholder="DD/MM/YYYY"  CssClass="form-control"></asp:TextBox>
                <span class="input-group-addon calendar-icon" id="ToCalSpan"><i class="fa fa-calendar"> </i></span>
            </div>
        </ItemTemplate>
    </asp:TemplateField>
    <asp:TemplateField HeaderText="Capital Project (Optional)">
        <ItemTemplate>
            <div class="grid-code-tbl">
                <asp:Label ID="lblJobCode" CssClass="grid-code-cell form-control" Text='<%# Eval("JOBCODE") %>'runat="server"></asp:Label>
                <div class="grid-code-cell grid-code-icon" runat="server" id="JobCodeDiv">
                    <asp:LinkButton ID="lnkJobCode" CommandName="SelectJobCode" runat="server" class="btn btn-default"><i class="fa fa-th-list"></i></asp:LinkButton>
                </div>
            </div>
        </ItemTemplate>
    </asp:TemplateField>
    <asp:TemplateField HeaderText="Invoice Number">
        <ItemTemplate>
            <asp:TextBox CssClass="form-control hiddenElm" ID="txtInvoiceNumber" onblur="SetInvoiceNumber(this.value);" Text='<%# Eval("InvoiceNumber") %>' runat="server"></asp:TextBox>
        </ItemTemplate>
    </asp:TemplateField>
    <asp:BoundField DataField="HISTSTATUS" HeaderText="Payment Status" />
    <asp:BoundField DataField="HISTCHECKDT" HeaderText="Date Paid" DataFormatString="{0:dd/MM/yyyy}" />
    <asp:BoundField DataField="HISTFRIDNO" HeaderText="Funds Request#" />
    <asp:BoundField DataField="HISTFRIDNO" HeaderText="Attachment" />
</Columns>