HEEN
HEEN

Reputation: 4721

Gridview Row is null or not an object

I have a Gridview in which what I want is, If the amount column is 0 or "". I want to hide the Generate invoice button. So for that what I tried is,

function GridExpInfo_ClientInsert() {
        for (i = 0; i < GridExpInfo.Rows.length; i++) {
            if (GridExpInfo.Rows[i].Cells[7].Value == "" || GridExpInfo.Rows[i].Cells[7].Value == "0") {
                GridExpInfo.Rows[i].Cells[14].style.display = "none";
            }
        }
    }

But I am getting error as

Microsoft JScript runtime error: 'GridExpInfo.Rows[...].Cells.14.style' is null or not an object

Here is my 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" />
    <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;" />
            </Template>
        </cc1:GridTemplate>
    </Templates>
</cc1:Grid>

Upvotes: 0

Views: 510

Answers (2)

VDWWD
VDWWD

Reputation: 35514

JavaScript and C# code do not mix. Doesn't the line GridView1.Rows[i].Cells[14].style.display = "none"; give an error like TableCell does not contain a definition for 'style'... in Visual Studio?

You should use it like this:

GridExpInfo.Rows[i].Cells[14].Visible = false;

However this will remove the cell from the grid and the columns in that row will move to the left. So to the user Cell 14 contains the content from Cell 15, 15 from 16 etc. You can fix this by applying a colspan to Cell 13, but that also means the contents of Cell 13 could overflow.

Even better would be to hide the button based on a certain value in the row.

<asp:Button Visible='<%# string.IsNullOrEmpty(Eval("value").ToString()) || Eval("value").ToString() != "0" %>' runat="server" Text="Generate invoice" ID="Button1" />

UPDATE

It looks OP is using the Grid from Obout. That probably means that "normal" grid function work differently.

<input type="button" value="Cancel" onclick="GridExpInfo.cancel()" class="button" style="display:<%# (Container.Value != "0" ? "block" : "none") %>" />

Upvotes: 1

AT-2017
AT-2017

Reputation: 3149

I've given a solution for ListView to disable button on condition. See the sample below and quite identical to GridView:

Default.aspx:

<asp:ListView ID="ListView1" runat="server" OnItemDataBound="ListView1_ItemDataBound">
      <ItemTemplate>
            <asp:Label ID="label1" runat="server" Text='<%#Eval("ID") %>'></asp:Label>
            <asp:Label ID="label2" runat="server" Text='<%#Eval("name") %>'></asp:Label>
            <asp:Label ID="label3" runat="server" Text='<%#Eval("Status") %>'></asp:Label>
            <asp:Button ID="button1" runat="server" Text="Add" Enabled='<%# Eval("Status").ToString() == "1" %>' />
            <br />
      </ItemTemplate>
</asp:ListView>

Default.aspx.cs: Just paste the below code in the code-behind

protected void Page_Load(object sender, EventArgs e)
    {
        ListView1.DataSource = GetAllUsers();
        ListView1.DataBind();
    }

    public class User
    {
        public int ID { get; set; }
        public string name { get; set; }
        public string Status { get; set; }
    }

    public List<User> GetAllUsers()
    {
        User aUser = new User();

        List<User> Users = new List<User>();

        Users.Add(new User() { ID = 1, name = "AT-2016", Status = "1" } );
        Users.Add(new User() { ID = 2, name = "AT-2014", Status = "0" } );

        return Users;
    }

    protected void ListView1_ItemDataBound(object sender, ListViewItemEventArgs e)
    {
        for (int i = 0; i < ListView1.Items.Count(); i++)
        {
            //Get the Label by row
            Label status = (Label)ListView1.Items[i].FindControl("label3");
            Button button1 = (Button)ListView1.Items[i].FindControl("button1");

            if (status.Text == "1")
            {
                button1.Enabled = true;
            }
            else
            {
                button1.Enabled = false;
            }
        }
    }

Upvotes: 1

Related Questions