【问题标题】:using paging in DataTable for showing in GridView在 DataTable 中使用分页在 GridView 中显示
【发布时间】:2013-12-24 13:01:53
【问题描述】:

我想要 GridView 中的 30 个数据。我想读取我文件夹中的所有文件并在 GridView 中显示。我正在使用以下代码。

string folderPath = @"C:\Folder\Folder-2.0\New folder";
DataTable dt = new DataTable();
//Creating DataTable

foreach (string fileName in Directory.EnumerateFiles(folderPath, "*.txt"))
    {
        string contents = File.ReadAllText(fileName);
        string str = string.Empty;
        string s;

        if (File.Exists(fileName))
        {
            StreamReader sr = new StreamReader(fileName);
            String line;               
            int k = 0;
            while ((line = sr.ReadLine()) != null)
            {
                string[] text;

                if (line.Trim() != string.Empty)
                {
                    //text = line.Split(new char[] { '^' }, StringSplitOptions.RemoveEmptyEntries);

                    text = line.Split('^');

                    if (text[0].Contains("START FOOTER"))
                    {
                        break;
                    }
                    else
                    {
                        DataRow dr;
                        dr = dt.NewRow();

                        for (int i = 0; i <= text.Length - 1; i++)
                        {
                            dr[i] = text[i];
                        }

                        dt.Rows.Add(dr);
                        k = k + 1;
                    }
                }

            }
            Response.Write(k);
            //  Response.End;
            GridView1.DataSource = dt;
            GridView1.DataBind();

        }
        else
        {
            s = "File does not exists";
        }
    }

但是上面的代码抛出错误System.OutOfMemoryException。错误指向代码GridView1.DataBind(); 文件夹中有大约 2000 个文件,每个文件的大小在 1 到 2 MB 之间。这就是为什么我想使用 DataTable 使用分页。通过DataTable我想显示30条记录。

谢谢,

【问题讨论】:

  • 我认为您需要重构您的方法并尝试使用索引页面循环并保存最后一个索引。我不确定那是你可能需要用DataColumn 初始化DataTable 的错误
  • 单独分页做什么,一次只绑定几条记录,然后维护索引并处理

标签: c# asp.net gridview datatable


【解决方案1】:

请将PageSize定义为30并实现PageIndexChanging事件

protected void GridView1_PageIndexChanging(object sender, GridView1PageEventArgs e)
{
    // here you need create one method of your above code and call here 
    GridView1.PageIndex = e.NewPageIndex;
    GridView1.DataBind();
}

【讨论】:

  • 感谢您的回复。我不想要这个分页我想在 DataTable 中进行分页,即从数据表中调用前 30 条记录,然后调用接下来的 30 条记录。我不想一次加载整个数据表。
  • @rahulaggarwal 我想建议您将 Id 列添加为数字字段,并根据记录和用户选择 DataTable 的方法增加,即。 DataTable.Select("ID=> 1 AND ID
  • 我认为你需要使用 Directory.EnumerateFiles() 的 Take() 和 Skip() 方法。
猜你喜欢
  • 2013-04-09
  • 1970-01-01
  • 2020-11-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-06-25
相关资源
最近更新 更多