C# 如何在绑定gridview时修复性能问题,而无需数据库交互?

C# 如何在绑定gridview时修复性能问题,而无需数据库交互?,c#,asp.net,C#,Asp.net,我正在向gridview添加一些数据,而不需要数据库交互。在向gridview添加数据的过程中,有一段时间它看起来很好。在添加了20多行之后,所花费的时间比预期的要多,而且也比预期的要多。当然,完成加法需要几秒钟。如果有人帮我找出解释中的问题,那会很有帮助的 我使用Viewstate来存储数据。在寻找解决方案之后。我已经了解了ViewState的一些缺点,因此返回会话来存储数据。但这也帮不了我 这是aspx页面 <asp:GridView ID="gvPayFrmAccount" runa

我正在向gridview添加一些数据,而不需要数据库交互。在向gridview添加数据的过程中,有一段时间它看起来很好。在添加了20多行之后,所花费的时间比预期的要多,而且也比预期的要多。当然,完成加法需要几秒钟。如果有人帮我找出解释中的问题,那会很有帮助的

我使用Viewstate来存储数据。在寻找解决方案之后。我已经了解了ViewState的一些缺点,因此返回会话来存储数据。但这也帮不了我

这是aspx页面

<asp:GridView ID="gvPayFrmAccount" runat="server" Width="100%" CssClass="table table-striped table-bordered table-condensed"
RowStyle-CssClass="zero" AutoGenerateColumns="false" OnRowDataBound="GvPayFrmAccount_RowDataBound" OnRowDeleting="GvPayFrmAccountOnRowDeleting">
    <HeaderStyle CssClass="headerStyle"></HeaderStyle>
    <RowStyle CssClass="rowStyle"></RowStyle>
    <Columns>
        <asp:BoundField DataField="CrAccCode" ItemStyle-HorizontalAlign="Center" HeaderText="Account Code" />
        <asp:BoundField DataField="CrAccAccount" ItemStyle-HorizontalAlign="Center" HeaderText="Allocate Account" />
        <asp:BoundField DataField="CrAmount" ItemStyle-HorizontalAlign="Center" HeaderText="Amount"></asp:BoundField>
        <asp:TemplateField ItemStyle-HorizontalAlign="Center" HeaderText="Delete">
        <ItemTemplate>
        <asp:Button ID="ButtonDelete" runat="server" CommandName="Delete" Text="X" />
        </ItemTemplate>
        </asp:TemplateField>
    </Columns>
</asp:GridView>


这就是背后的代码=====================

private void BindPayfrmAcc()
{
    DataRow dr1;
    DataColumn CrAccCode, CrAccAccount, CrAmount;
    CrAccCode = new DataColumn("CrAccCode", Type.GetType("System.String"));
    CrAccAccount = new DataColumn("CrAccAccount", Type.GetType("System.String"));
    CrAmount = new DataColumn("CrAmount", Type.GetType("System.String"));
    Current.Columns.Add(CrAccCode);
    Current.Columns.Add(CrAccAccount);
    Current.Columns.Add(CrAmount);
    int temp = 0;
    txtCrAmount.Text = Math.Round(Convert.ToDouble(txtCrAmount.Text), 4).ToString(".0000");
    if (btnCrSave.Text == "Add")
    {
        if (Session["Current"] != null)
        {
            Current = (DataTable)Session["Current"];
            for (int j = 0; j < Current.Rows.Count; j++)
            {
                if (Current.Rows[j]["CrAccCode"].ToString() == txtPaidAccCode.Text)
                {
                    ScriptManager.RegisterStartupScript(this, GetType(), "Save",
                    "alert('This Charge Head has already been added...');", true);
                    temp++;
                    break;
                }
            }
        if (temp == 0)
        {
            dr1 = Current.NewRow();
            dr1[0] = txtPaidAccCode.Text;
            dr1[1] = ddlPfrmAcc.SelectedItem.Text;
            dr1[2] = txtCrAmount.Text;
            Current.Rows.Add(dr1);
            double x = Math.Truncate(Convert.ToDouble(txtCrAmount.Text) * 10000) / 10000;
            lblCrTotalAmount.InnerText = (Convert.ToDouble(lblCrTotalAmount.InnerText) + x).ToString();
        }
    }
    else
    {
        dr1 = Current.NewRow();
        dr1[0] = txtPaidAccCode.Text;
        dr1[1] = ddlPfrmAcc.SelectedItem.Text;
        dr1[2] = txtCrAmount.Text;
        Current.Rows.Add(dr1);
        lblCrTotalAmount.InnerText = "0";
        lblCrTotalAmount.InnerText = txtCrAmount.Text;
        double x = Math.Truncate(Convert.ToDouble(txtCrAmount.Text) * 10000) / 10000;
        lblCrTotalAmount.InnerText = x.ToString();
        lblCrTotalAmount.InnerText = (Math.Truncate(Convert.ToDouble(lblCrTotalAmount.InnerText) * 10000) / 10000).ToString();
    }
}
if (Session["Current"] != null)
{
    gvPayFrmAccount.DataSource = Current;
    gvPayFrmAccount.DataBind();
    if (btnSave.Text == "Save")
    ClearData();
}
else
{
    gvPayFrmAccount.DataSource = Current;
    gvPayFrmAccount.DataBind();
    if (btnSave.Text == "Save")
    ClearData();
}
    Session["Current"] = Current;
}
private void BindPayfrmAcc()
{
数据行dr1;
数据列CrAccCode、cracccount、CrAmount;
craccode=newdatacolumn(“craccode”,Type.GetType(“System.String”);
cracaccount=newdatacolumn(“cracaccount”,Type.GetType(“System.String”);
CrAmount=newdatacolumn(“CrAmount”,Type.GetType(“System.String”);
当前.Columns.Add(CRACCODE);
当前.Columns.Add(cracAccount);
当前.Columns.Add(CrAmount);
内部温度=0;
txtCrAmount.Text=Math.Round(Convert.ToDouble(txtCrAmount.Text),4.ToString(“.0000”);
如果(btnCrSave.Text==“添加”)
{
如果(会话[“当前”]!=null)
{
当前=(数据表)会话[“当前”];
对于(int j=0;j
ViewState
Session
InProc
是臭名昭著的资源消耗者时。您可以通过将以下内容添加到
web.config
system.web
部分)来提高性能


然后,当
InProc
是臭名昭著的资源消耗者时,将
DataTable
保存在
ViewState

ViewState
Session
中。您可以通过将以下内容添加到
web.config
system.web
部分)来提高性能


然后将您的
数据表
保持在
视图状态

为什么没有数据库?这只会使您的代码更复杂,更容易出错。例如,如果重新启动或回收web应用程序,则会丢失所有数据。此部分允许用户添加项目并在以后保存。然后数据库开始运行。为什么没有数据库?这只会使您的代码更复杂,更容易出错。例如,如果重新启动或回收web应用程序,则会丢失所有数据。此部分允许用户添加项目并在以后保存。然后数据库开始运行。谢谢您的回复。我加上去了。但这没用。我在这个页面上发现了另一个gridview,它提出了性能问题。我认为分页可以有所帮助。虽然默认分页根本没有帮助。谢谢你的回复。我加上去了。但这没用。我在这个页面上发现了另一个gridview,它提出了性能问题。我认为分页可以有所帮助。尽管默认分页根本没有帮助。
<sessionState mode="StateServer" stateConnectionString="tcpip=localhost:42424" timeout="60"/>
Private _persister As PageStatePersister = Nothing
Protected Overrides ReadOnly Property PageStatePersister As PageStatePersister
    Get
        If _persister Is Nothing Then
            _persister = New SessionPageStatePersister(Me)
        End If
        Return _persister
    End Get
End Property