如果amount列为0或NULL,则隐藏Gridview按钮

时间:2016-11-03 05:09:03

标签: c# asp.net gridview

我有一个Gridview,其中包含一些从数据库中添加的数据。所以我想要的是,如果金额列中的值为0NULL

,我希望隐藏按钮

我尝试了RowDataBound,如下所示

 if (e.Row.Cells[10].Text == "0")
    {
        e.Row.Cells[15].Style["display"] = "none";
    }

但它确实列是""

那么如果值为0

,如何隐藏按钮 gridview的

ASPX: -

<cc1:Grid ID="GridExpInfo" runat="server" FolderStyle="../Styles/Grid/style_12" AutoGenerateColumns="false"
                        AllowAddingRecords="true" AllowColumnResizing="true" Width="100%" ShowFooter="true"
                        ShowHeader="true" OnInsertCommand="GridExpInfo_InsertCommand" OnRebind="GridExpInfo_Rebind"
                        OnRowDataBound="GridExpInfo_RowDataBound" 
                        OnUpdateCommand="GridExpInfo_UpdateCommand">
                        <ClientSideEvents OnClientAdd="GridExpInfo_ClientAdd" OnClientEdit="GridExpInfo_OnClientEdit"
                            OnClientDblClick="GridExpInfo_OnClientDblClick" OnClientInsert="GridExpInfo_ClientInsert"
                            OnClientUpdate="GridExpInfo_ClientUpdate" />
                        <TemplateSettings RowEditTemplateId="tplRowEdit3" />
                        <Columns>
                            <cc1:Column ID="Column19" DataField="MKEY" ReadOnly="true" Width="0%" runat="server"
                                Visible="false">
                                <TemplateSettings TemplateId="gtchkConfirm" HeaderTemplateId="HTConfirm" />
                            </cc1:Column>
                            <cc1:Column Wrap="true" DataField="SR_NO" ReadOnly="true" HeaderText="Sr No" Width="4%"
                                ID="Column31">
                                <TemplateSettings RowEditTemplateControlId="txtsrnoexp" RowEditTemplateControlPropertyName="value" />
                            </cc1:Column>
                            <cc1:Column Wrap="true" DataField="TYPE" HeaderText="Agreement Type" Width="8%" ID="Column51">
                                <TemplateSettings RowEditTemplateControlId="ddlTypeExpData" RowEditTemplateControlPropertyName="value" />
                            </cc1:Column>
                            <cc1:Column Wrap="true" DataField="STAGE" HeaderText="Stage" Width="8%" ID="Column44">
                                <TemplateSettings RowEditTemplateControlId="ddlStageType" RowEditTemplateControlPropertyName="value" />
                            </cc1:Column>
                            <cc1:Column Wrap="true" DataField="EXP_TYPE" HeaderText="Expense Type" Width="8%"
                                ID="Column25">
                                <TemplateSettings RowEditTemplateControlId="ddlexptypExp" RowEditTemplateControlPropertyName="value" />
                            </cc1:Column>
                            <cc1:Column Wrap="true" DataField="EXP_ID" Width="0%" Visible="false" ID="Column43">
                            </cc1:Column>
                            <cc1:Column Wrap="true" DataField="PARTY_NAME" HeaderText="Party Name" Width="8%"
                                ID="Column29">
                                <TemplateSettings RowEditTemplateControlId="txtpartynameexp" RowEditTemplateControlPropertyName="value" />
                            </cc1:Column>
                            <cc1:Column Wrap="true" DataField="AMOUNT" HeaderText="Amount" Width="6%" ID="Column21">
                                <TemplateSettings RowEditTemplateControlId="txtamountexp" RowEditTemplateControlPropertyName="value" />
                            </cc1:Column>
                            <cc1:Column Wrap="true" DataField="REMARKS" HeaderText="Remarks" Width="10%" ID="Column24">
                                <TemplateSettings RowEditTemplateControlId="txtremexp" RowEditTemplateControlPropertyName="value" />
                            </cc1:Column>
                            <cc1:Column Wrap="true" DataField="CHQ_FAVR_NAME" HeaderText="Chq in Favor Name"
                                Width="8%" ID="Column28">
                                <TemplateSettings RowEditTemplateControlId="txtchqfavExp" RowEditTemplateControlPropertyName="value" />
                            </cc1:Column>
                            <cc1:Column Wrap="true" DataField="VOUCHER_NO" HeaderText="Voucher No" Width="6%"
                                ID="Column26">
                                <TemplateSettings RowEditTemplateControlId="txtvouchexp" RowEditTemplateControlPropertyName="value" />
                            </cc1:Column>
                            <cc1:Column Wrap="true" DataField="CHEQUE_NO" HeaderText="Chq No" Width="5%" ID="Column27">
                                <TemplateSettings RowEditTemplateControlId="txtChqnoExp" RowEditTemplateControlPropertyName="value" />
                            </cc1:Column>
                            <cc1:Column Wrap="true" DataField="CHEQUE_DT" DataFormatString="{0:dd-MM-yyyy}" HeaderText="Chq Date"
                                Width="6%" ID="Column30">
                                <TemplateSettings RowEditTemplateControlId="txtchqDtExp" RowEditTemplateControlPropertyName="value" />
                            </cc1:Column>
                            <cc1:Column ID="Column32" DataField="ATTACHMENT" HeaderText="Attachment" runat="server"
                                Width="8%" Wrap="true">
                            </cc1:Column>
                            <cc1:Column ID="Column20" HeaderText="Generate Invoice" runat="server" Width="8%"
                                Wrap="true">
                                <TemplateSettings TemplateId="tplButtonInvoice" />
                            </cc1:Column>
                        </Columns>
                        <Templates>
                            <cc1:GridTemplate runat="server" ID="tplRowEdit3">
                                <Template>
                                    <table class="rowEditTable">
                                        <tr>
                                            <td valign="top">
                                                <fieldset style="width: 750px; height: 310px;">
                                                    <legend>Expense Information</legend>
                                                    <table cellpadding="2" cellspacing="2" border="0">
                                                        <tr>
                                                            <td style="display: none;">
                                                                SR NO :
                                                            </td>
                                                            <td>
                                                                <input type="text" runat="server" id="txtsrnoexp" style="display: none;" class="ob_gEC" />
                                                            </td>
                                                        </tr>
                                                        <tr>
                                                            <td>
                                                                Type :
                                                            </td>
                                                            <td>
                                                                <asp:DropDownList ID="ddlTypeExpData" runat="server" Width="250px" DataSourceID="ds_exp_type"
                                                                    DataTextField="TYPE_DESC" DataValueField="TYPE_DESC">
                                                                    <asp:ListItem Value="0">--Select--</asp:ListItem>
                                                                    <%--<asp:ListItem Value="Agreement Type">Agreement Type</asp:ListItem>
                                                                    <asp:ListItem Value="Expense Type">Expense Type</asp:ListItem>--%>
                                                                </asp:DropDownList>
                                                            </td>
                                                        </tr>
                                                        <tr>
                                                            <td>
                                                                Stage :
                                                            </td>
                                                            <td>
                                                                <asp:DropDownList ID="ddlStageType" runat="server" name="ddlStageTypename" DataSourceID="sd_Type_Stage"
                                                                    DataTextField="STAGE" DataValueField="STAGE" Width="250px">
                                                                </asp:DropDownList>
                                                            </td>
                                                        </tr>
                                                        <tr style="display: none;">
                                                            <td>
                                                                Ref No :
                                                            </td>
                                                            <td>
                                                                <%--<input type="text" id="txtExpRefNo" name="txtrefNovalue" runat="server" style="width: 150px;
                                                                    height: 18px; font-size: 11px;" class="ob_gEC" />--%>
                                                                <asp:TextBox ID="txtExpRefNo" Height="18px" name="txtrefNovalue" runat="server" CssClass="ob_gEC"
                                                                    Width="150px"></asp:TextBox>
                                                            </td>
                                                            <td>
                                                                Ref Date :
                                                            </td>
                                                            <td>
                                                                <%-- <input type="text" id="txtExpRefDt" name="txtrefdatevalue" runat="server" style="width: 150px;
                                                                    height: 18px; font-size: 11px;" class="ob_gEC" />--%>
                                                                <asp:TextBox ID="txtExpRefDt" Height="18px" name="txtExpRefDt" runat="server" CssClass="ob_gEC"
                                                                    Width="150px"></asp:TextBox>
                                                            </td>
                                                        </tr>
                                                        <tr>
                                                            <td>
                                                                Expense Type:
                                                            </td>
                                                            <td>
                                                                <asp:DropDownList ID="ddlexptypExp" runat="server" DataSourceID="sd_Type_EXP" DataTextField="DESCRIPTION"
                                                                    DataValueField="DESCRIPTION" Width="250px">
                                                                    <asp:ListItem Value="--Select--">--Select--</asp:ListItem>
                                                                </asp:DropDownList>
                                                            </td>
                                                        </tr>
                                                        <tr>
                                                            <td>
                                                                Amount:
                                                            </td>
                                                            <td>
                                                                <input type="text" id="txtamountexp" style="width: 150px; height: 18px; font-size: 11px;"
                                                                    class="ob_gEC" />
                                                            </td>
                                                        </tr>
                                                        <tr>
                                                            <td>
                                                                Party Name:
                                                            </td>
                                                            <td>
                                                                <input type="text" id="txtpartynameexp" style="width: 250px; height: 18px; font-size: 11px;"
                                                                    class="ob_gEC" />
                                                            </td>
                                                        </tr>
                                                        <tr>
                                                            <td>
                                                                Expense Details:
                                                            </td>
                                                            <td>
                                                                <asp:TextBox ID="txtremexp" CssClass="ob_gEC" runat="server" Width="250px" Height="50"
                                                                    TextMode="MultiLine" Style="font-size: 11px;"></asp:TextBox>
                                                            </td>
                                                        </tr>
                                                        <tr>
                                                            <td>
                                                                Chq in favor name:
                                                            </td>
                                                            <td>
                                                                <input type="text" id="txtchqfavExp" style="width: 250px; height: 18px; font-size: 11px;"
                                                                    class="ob_gEC" />
                                                            </td>
                                                            <td>
                                                                Voucher No:
                                                            </td>
                                                            <td>
                                                                <input type="text" id="txtvouchexp" style="width: 150px; height: 18px; font-size: 11px;"
                                                                    class="ob_gEC" />
                                                            </td>
                                                        </tr>
                                                        <tr>
                                                            <td>
                                                                Chq No:
                                                            </td>
                                                            <td>
                                                                <input type="text" id="txtChqnoExp" style="width: 150px; height: 18px; font-size: 11px;"
                                                                    class="ob_gEC" />
                                                            </td>
                                                            <td>
                                                                Chq date:
                                                            </td>
                                                            <td>
                                                                <input type="text" id="txtchqDtExp" style="width: 150px; height: 18px; font-size: 11px;"
                                                                    class="ob_gEC" />
                                                            </td>
                                                        </tr>
                                                        <tr>
                                                            <td>
                                                                Attachment:
                                                            </td>
                                                            <td>
                                                                <input type="file" id="FlAttacExp" style="width: 300px; height: 18px; font-size: 11px;"
                                                                    class="ob_gEC" />
                                                            </td>
                                                        </tr>
                                                    </table>
                                                </fieldset>
                                            </td>
                                        </tr>
                                        <tr>
                                            <td colspan="2" align="center">
                                                <input type="button" value="Save" onclick="GridExpInfo.save()" class="button" />
                                                <input type="button" value="Cancel" onclick="GridExpInfo.cancel()" class="button" />
                                            </td>
                                        </tr>
                                    </table>
                                </Template>
                            </cc1:GridTemplate>
                        </Templates>
                        <%-- <Templates>
                            <cc1:GridTemplate runat="server" ID="tplNumbering3">
                                <Template>
                                    <b>
                                        <%# (Container.RecordIndex + 1) %></b>
                                </Template>
                            </cc1:GridTemplate>
                        </Templates>--%>
                        <Templates>
                            <cc1:GridTemplate runat="server" ID="tplButtonInvoice">
                                <Template>
                                    <input id="Button1" type="button" runat="server" value="Invoice" style="width: 80px;" />
                                    <%--<asp:Button Visible='<%# string.IsNullOrEmpty(Container.Value("Invoice").ToString()) || Container.Value("Invoice").ToString() != "0" %>'
                                        runat="server" Text="Generate invoice" ID="Button1" />--%>
                                </Template>
                            </cc1:GridTemplate>
                        </Templates>
                    </cc1:Grid>

3 个答案:

答案 0 :(得分:0)

使用模板列并检查脚本中的条件。根据您的条件显示该列中的数据。请参阅https://msdn.microsoft.com/en-us/library/bb288032.aspx

答案 1 :(得分:0)

是的,可以通过在gridview的模板字段中使用服务器控件来实现

的.aspx

<style>
 .btnHidden {
    display: none !important;
 }
</style> 

和.cs

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{

      if (e.Row.Cells[0].Text == "10537")
      {
         Button btnHidden = e.Row.FindControl("btnHidden") as Button;
         if (btnHidden != null)
         {
             btnHidden.CssClass = "btnHidden";
         }
     }
}

答案 2 :(得分:0)

这个ans可以帮助你。

的.aspx

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="GridView.aspx.cs" Inherits="StackOverflow_Solve.Others.GridView" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
    <style>
        .btnHidden {
            display: none !important;
        }
    </style>
    <script>
        $(function() {
            $('input[type="submit"]').click(function (e) {
                e.preventDefault();
                //Iterate row to do somthing


                var userid = $(this).closest('tr').find('td:eq(0)').text();
                alert(userid);
            });
        })
    </script>
</head>
<body>
    <form id="form1" runat="server">
    <div >
      <input id="Button1" type="button" runat="server" value="Invoice" style="width: 80px;" />
    </div>
    <div>


    </div>
    <div>
        <asp:GridView ID="grdvShowUsers" runat="server" AutoGenerateColumns="False" 
            DataKeyNames="UserID"
            onrowdatabound="grdvShowUsers_RowDataBound">
            <Columns>
                <asp:BoundField DataField="UserID" HeaderText="UserID" />
                <asp:BoundField DataField="UserName" HeaderText="UserName" />
                 <asp:TemplateField>
                      <HeaderTemplate>
                          ColumnName
                      </HeaderTemplate>
                      <ItemTemplate>
                          <asp:Button ID="btnHidden" runat="server" Text="Button" ClientIDMode="Static" />
                      </ItemTemplate>
                 </asp:TemplateField>
            </Columns>
        </asp:GridView>
    </div>
    </form>
</body>
</html>

和.cs

using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace StackOverflow_Solve.Others
{
    public partial class GridView : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                grdvShowUsers.DataSource = GetData();
                grdvShowUsers.DataBind();
            }
        }

        private DataTable GetData()
        {
            DataTable dt=new DataTable();
            String connectionString = ConfigurationManager.ConnectionStrings["WaltonCrmConnectionString"].ConnectionString; ;
            using (var conn = new SqlConnection(connectionString))
            {
                conn.Open();

                string servicePointListQuery = String.Format(@"select UserID,UserName from Users ");
                var command = new SqlCommand(servicePointListQuery, conn);

                SqlDataAdapter dataAdapter=new SqlDataAdapter(command);
                dataAdapter.Fill(dt);
            }

            return dt;
        }
        protected void grdvShowUsers_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                if (e.Row.Cells[0].Text == "11614")
                {
                    Button btnHidden = e.Row.FindControl("btnHidden") as Button;
                    if (btnHidden != null)
                    {
                        btnHidden.CssClass = "btnHidden";
                    }
                }
            }
        }
    }
}

和javascript解决方案并且不要忘记在网格视图中添加ClientIDMode="Static"

$("#grdvShowUsers tr").each(function (index) {
                //Excluding Header
                if (index != 0) {

                    var userid = $(this).find('td:eq(0)').text();
                   // if (userid === '11614') {
                        $(this).find('input[type="submit"]').addClass("btnHidden");
                    //}
                }
            });