簡體   English   中英

使用DataTable中的分頁在GridView中顯示

[英]using paging in DataTable for showing in GridView

我想要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條記錄。

謝謝,

請定義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();
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM