当前网络请求的执行。对象不能从 DBNull 转换为其他类型

execution of the current web request. Object cannot be cast from DBNull to other types

请不要投反对票。我是这个网站的新手。 我自己试过,但反复出现同样的错误。

protected void gvDepPayment_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            Label lbl = (Label)e.Row.FindControl("lblDeptName");
            DataTable dT = (DataTable)ViewState["DataBounded"];
            DataRow[] drows = dT.Select("DepartmentName = '" + lbl.Text + "'");
            int Count = 0;
            foreach (DataRow item in drows)
            {
                Count += Convert.ToInt32(item["PaidAmount"]);
            }
            Label lblPaidAmount = (Label)e.Row.FindControl("lblPaidAmount");
            lblPaidAmount.Text = Count.ToString();
        }
    }
<asp:GridView ID="gvDepPayment" runat="server" AutoGenerateColumns="false" 
                                    OnRowDataBound="gvDepPayment_RowDataBound">
                                    <Columns>
                                        <asp:TemplateField HeaderText="Department Name">
                                            <ItemTemplate>
                                                <asp:Label ID="lblDeptName" runat="server" Text='<%#Eval("DepartmentName") %>'></asp:Label>
                                            </ItemTemplate>
                                        </asp:TemplateField>
                                        <asp:TemplateField HeaderText="Collection">
                                            <ItemTemplate>
                                                <asp:Label ID="lblPaidAmount" runat="server" Text="0"></asp:Label>
                                            </ItemTemplate>
                                        </asp:TemplateField>
                                    </Columns>
                                </asp:GridView>

我想找到付款列表并像部门一样在网格视图中显示它

此错误消息是由于您在名为 PaidAmount 的列读取 DataRow 的内容引起的,可能在您的查询返回的行集中有一些空值。在这种情况下,您可以将 DataRow class 的 IsNull 方法与条件运算符一起使用。在 DBNull.Value 的情况下,您希望将零添加到计数中。

Count += item.IsNull("PaidAmount") ? 0 : Convert.ToInt32(item["PaidAmount"]);