网格视图中的输入字符串格式不正确

时间:2015-05-26 10:59:54

标签: c# asp.net gridview

绑定gridview时,我想获取单元格控件值,但在我的代码中抛出错误。如下所示,我将gridview与编辑,删除和页脚行绑定。

<asp:GridView ID="GridView1" runat="server"  Width = "100%" 
        OnDataBound = "OnDataBound" OnRowCreated = "OnRowCreated"
AutoGenerateColumns = "False" Font-Names = "Arial"
Font-Size = "11pt" AlternatingRowStyle-BackColor = "#C2D69B" 
HeaderStyle-BackColor = "green" AllowSorting="True" AllowPaging ="True"  ShowFooter = "True" 
OnPageIndexChanging = "OnPaging" onrowediting="EditCustomer"
onrowupdating="UpdateCustomer" onrowcancelingedit="CancelEdit" PageSize = "30">
<Columns>
<asp:TemplateField ItemStyle-Width = "100px" HeaderText="S.NO">
  <ItemTemplate><%#Container.DataItemIndex + 1%>                                                
  </ItemTemplate>
    <ItemStyle Width="100px" />
</asp:TemplateField>

    <asp:TemplateField ItemStyle-Width = "100px"  HeaderText = "Item Code">
    <FooterTemplate>
        <asp:Label ID="lblAddItemCode" runat="server"
                Text='<%# Eval("ItemId")%>'></asp:Label>
    </FooterTemplate>
    <ItemTemplate>
        <asp:Label ID="lblViewItemCode" runat="server"
                Text='<%# Eval("ItemId")%>'></asp:Label>
    </ItemTemplate>
    <EditItemTemplate>
         <asp:Label ID="lblEditItemCode" runat="server"
                Text='<%# Eval("ItemId")%>'></asp:Label>
    </EditItemTemplate> 
    <ItemStyle Width="100px" />
</asp:TemplateField>

<asp:TemplateField ItemStyle-Width = "100px"  HeaderText = "Item Name">
    <EditItemTemplate>
       <asp:Label ID="lblEditItemName" runat="server"
                Text='<%# Eval("ItemName")%>'></asp:Label>
    </EditItemTemplate>
    <FooterTemplate>
        <asp:DropDownList ID="ddlItemNameGrid" runat="server" 
            DataSourceID="SqlDataSource1" DataTextField="ItemName" 
            DataValueField="ItemId">
        </asp:DropDownList>
        <asp:SqlDataSource ID="SqlDataSource1" runat="server" 
            ConnectionString="<%$ ConnectionStrings:crmConnectionString3 %>" 
            SelectCommand="SELECT [ItemId], [ItemName] FROM [ItemMaster] WHERE ([Company_Id] = @Company_Id)">
            <SelectParameters>
                <asp:SessionParameter DefaultValue="1014" Name="Company_Id" 
                    SessionField="RegistrationId" Type="Int32" />
            </SelectParameters>
        </asp:SqlDataSource>
    </FooterTemplate>
    <ItemTemplate>
        <asp:Label ID="lblViewItemName" runat="server"
                Text='<%# Eval("ItemName")%>'></asp:Label>
    </ItemTemplate>
    <ItemStyle Width="100px" />
</asp:TemplateField>

<asp:TemplateField ItemStyle-Width = "150px"  HeaderText = "Unit">
    <ItemTemplate>
        <asp:Label ID="lblViewUnit" runat="server"
            Text='<%# Eval("Unit")%>'></asp:Label>
    </ItemTemplate>
    <EditItemTemplate>
        <asp:TextBox ID="txtEditUnit" runat="server" Text='<%# Eval("Unit")%>' ValidationGroup="EditValidations"></asp:TextBox>
            <asp:RequiredFieldValidator ID="reqtxtEditUnit" CssClass="Validators" Display="Dynamic" ControlToValidate="txtEditUnit" runat="server" 
    ErrorMessage="Unit is required." ForeColor="Red" ValidationGroup="EditValidations"></asp:RequiredFieldValidator>
    </EditItemTemplate> 
    <FooterTemplate>
        <asp:TextBox ID="txtAddUnit" runat="server" ValidationGroup="AddValidations"></asp:TextBox>
        <asp:RequiredFieldValidator ID="reqtxtAddUnit" CssClass="Validators" Display="Dynamic" ControlToValidate="txtAddUnit" runat="server" 
    ErrorMessage="Unit is required." ForeColor="Red" ValidationGroup="AddValidations"></asp:RequiredFieldValidator>
    </FooterTemplate>
    <ItemStyle Width="150px" />
</asp:TemplateField>

<asp:TemplateField ItemStyle-Width = "150px"  HeaderText = "Quantity">
    <ItemTemplate>
        <asp:Label ID="lblViewQuantity" runat="server"
            Text='<%# Eval("Quantity")%>'></asp:Label>
    </ItemTemplate>
    <EditItemTemplate>
        <asp:TextBox ID="txtEditQuantity" runat="server"
            Text='<%# Eval("Quantity")%>'></asp:TextBox>
    </EditItemTemplate> 
    <FooterTemplate>
        <asp:TextBox ID="txtAddQuantity" runat="server"></asp:TextBox>
        <asp:RequiredFieldValidator ID="reqtxtAddQuantity" CssClass="Validators" Display="Dynamic" ControlToValidate="txtAddQuantity" runat="server" 
    ErrorMessage="Quantity is required." ForeColor="Red" ValidationGroup="AddValidations"></asp:RequiredFieldValidator>
    </FooterTemplate>
    <ItemStyle Width="150px" />
</asp:TemplateField>

<asp:TemplateField ItemStyle-Width = "150px"  HeaderText = "Rate">
    <ItemTemplate>
        <asp:Label ID="lblViewRate" runat="server"
            Text='<%# Eval("Rate")%>'></asp:Label>
    </ItemTemplate>
    <EditItemTemplate>
        <asp:TextBox ID="txtEditRate" runat="server" Text='<%# Eval("Rate")%>'></asp:TextBox>
        <asp:RequiredFieldValidator ID="reqtxtEditRate" CssClass="Validators" Display="Dynamic" ControlToValidate="txtEditRate" runat="server" 
    ErrorMessage="Quantity is required." ForeColor="Red" ValidationGroup="EditValidations"></asp:RequiredFieldValidator>
    </EditItemTemplate> 
    <FooterTemplate>
        <asp:TextBox ID="txtAddRate" runat="server"></asp:TextBox>
        <asp:RequiredFieldValidator ID="reqtxtAddRate" CssClass="Validators" Display="Dynamic" ControlToValidate="txtAddRate" runat="server" 
    ErrorMessage="Rate is required." ForeColor="Red" ValidationGroup="AddValidations"></asp:RequiredFieldValidator>
    </FooterTemplate>
    <ItemStyle Width="150px" />
</asp:TemplateField>

<asp:TemplateField ItemStyle-Width = "150px"  HeaderText = "Total">
    <ItemTemplate>
        <asp:Label ID="lblViewTotal" runat="server"
            Text='<%# Eval("Total")%>'></asp:Label>
    </ItemTemplate>
    <EditItemTemplate>
        <asp:TextBox ID="txtEditTotal" runat="server" Text='<%# Eval("Total")%>'></asp:TextBox>
        <asp:RequiredFieldValidator ID="reqtxtEditTotal" CssClass="Validators" Display="Dynamic" ControlToValidate="txtEditTotal" runat="server" 
    ErrorMessage="Total is required." ForeColor="Red" ValidationGroup="EditValidations"></asp:RequiredFieldValidator>
    </EditItemTemplate> 
    <FooterTemplate>
        <asp:TextBox ID="txtAddTotal" runat="server"></asp:TextBox>
         <asp:RequiredFieldValidator ID="reqtxtAddTotal" CssClass="Validators" Display="Dynamic" ControlToValidate="txtAddTotal" runat="server" 
    ErrorMessage="Total is required." ForeColor="Red" ValidationGroup="AddValidations"></asp:RequiredFieldValidator>
    </FooterTemplate>
    <ItemStyle Width="150px" />
</asp:TemplateField>

<asp:TemplateField ItemStyle-Width="80px" HeaderText="Add Item">
    <ItemTemplate>
    <%--<span onclick="return confirm('Are you sure to Delete the record?')">
    <asp:LinkButton ID="lnkB" runat="Server" Text="Delete" CommandName="ItemDelete"></asp:LinkButton>
     </span>--%>
    <%--<asp:LinkButton ID="lnkRemove" runat="server" OnClick="lnkRemove_Click">Delete</asp:LinkButton>--%>
        <asp:LinkButton ID="lnkRemove" runat="server"
            CommandArgument='<%#Eval("ItemDetailId")%>' Text="Delete"  OnClientClick = "return confirm('Do you want to delete?')"
            onclick="DeleteRecord"></asp:LinkButton>
    </ItemTemplate>
    <FooterTemplate>
        <asp:Button ID="btnAdd" runat="server" Text="Add" ValidationGroup="AddValidations"
            OnClick = "InsertItemDetails" />
    </FooterTemplate>
    <ItemStyle Width="80px" />
</asp:TemplateField>
<%--<asp:CommandField ShowDeleteButton="True" ButtonType="Button" HeaderText="Delete Item" />--%>

<asp:CommandField  ShowEditButton="True" ValidationGroup="EditValidations" HeaderText="Edit Item" />
</Columns>
</asp:GridView>

下面我正在检索&#39; Total&#39; coloumn的标签控制值。

protected void OnRowCreated(object sender, GridViewRowEventArgs e)
        {
            subTotal = 0;
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                DataTable dt = (e.Row.DataItem as DataRowView).DataView.Table;
                int itemId = Convert.ToInt32(dt.Rows[e.Row.RowIndex]["ItemId"]);
                total += Convert.ToDouble(dt.Rows[e.Row.RowIndex]["Total"]);
                if (itemId != currentId)
                {
                    if (e.Row.RowIndex > 0)
                    {
                        for (int i = subTotalRowIndex; i < e.Row.RowIndex; i++)
                        {
                            Label lblViewTotal = ((Label)e.Row.Cells[6].FindControl("lblViewTotal")) as Label;
                            if (lblViewTotal != null && lblViewTotal.Text != string.Empty) //Check whether it is null or not 
                            {
                                subTotal += Convert.ToDouble(lblViewTotal.Text);
                            }
                        }
                        this.AddTotalRow("Sub Total", subTotal.ToString("N7"));
                        subTotalRowIndex = e.Row.RowIndex;
                    }
                    currentId = itemId;
                }
            }
        }

2 个答案:

答案 0 :(得分:0)

我通过处理空条件对您的代码进行了一些修改。请看看。

 protected void OnRowCreated(object sender, GridViewRowEventArgs e)
 {
            subTotal = 0;
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                DataTable dt = (e.Row.DataItem as DataRowView).DataView.Table;
                int itemId = Convert.ToInt32(dt.Rows[e.Row.RowIndex]["ItemId"]);
                total += Convert.ToDecimal(dt.Rows[e.Row.RowIndex]["Total"]);
                if (itemId != currentId)
                {
                    if (e.Row.RowIndex > 0)
                    {
                        for (int i = subTotalRowIndex; i < e.Row.RowIndex; i++)
                        {
                           Label lblViewTotal = (Label)e.Row.FindControl("lblViewTotal");//First Get label
                            if (lblViewTotal != null && lblViewTotal.Text != string.Empty) //Check whether it is null or not 
                            {
                                subTotal += Convert.ToDecimal(lblViewTotal.Text);
                            }

                        }
                        this.AddTotalRow("Sub Total", subTotal.ToString("N7"));
                        subTotalRowIndex = e.Row.RowIndex;
                    }
                    currentId = itemId;
                }
            }
  }

答案 1 :(得分:0)

你确定在OnDataBound之后OnRowCreated触发了吗? 当您进行小计时,可能是在阶段没有价值。

尝试使用RowDataBound事件吗?

(自从我做网页表单工作已经有一段时间了,所以这是来自内存,如果我不正确就道歉)