2012-01-10 8 views
0

ASP.NETページにグリッドがあり、エクスポートボタンがあります。 [エクスポート]ボタンをクリックすると、以下のメソッドが実行され、Excelドキュメントにグリッドのデータが入力されます。しかし、それはページ1にあるデータだけで満たされます。私が100アイテムと10ページを持っているなら、それは1ページだけです。私はそれが表示されたものだけでなくすべてを行うと思うだろう。なぜそれがこれを行うだろう任意のアイデア?すべてのデータのエクスポート表示されていないデータ

おかげ

private void fillDocument() 
     { 
     string lFilename = Leads.xls"; 
     string lDistributorFolder = Server.MapPath(".") + "\\Portals\\0\\Distributors\\" + _currentUser.UserID.ToString() + "\\"; 
     string lTemplateFolder = System.Configuration.ConfigurationManager.AppSettings["Templates"]; 
     System.IO.Directory.CreateDirectory(lDistributorFolder); 

     File.Copy(lTemplateFolder + lFilename, lDistributorFolder + lFilename, true); 
     string lConnectionString = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + lDistributorFolder + "\\" + lFilename + ";Extended Properties=\"Excel 8.0;HDR=YES;\""; 
     DbProviderFactory lFactory = DbProviderFactories.GetFactory("System.Data.OleDb"); 
     int lSequence = 0; 

     using (DbConnection lConnection = lFactory.CreateConnection()) 
     { 
      lConnection.ConnectionString = lConnectionString; 
      lConnection.Open(); 

     foreach (GridDataItem lItem in grdLeadList.Items) 
      { 
      lSequence++; 

      using (DbCommand lCommand = lConnection.CreateCommand()) 
       { 
        lCommand.CommandText = "INSERT INTO [Leads$] "; 
        lCommand.CommandText += "(F1,F2,F3,F4,F5,F6,F7,F8,F9,F10,F11,F12,F13,F14,F15,F16,F17,F18,F19,F20,F21) "; 
        lCommand.CommandText += "VALUES("; 
        lCommand.CommandText += "\"" + lSequence.ToString() + "\","; 
        lCommand.CommandText += "\"" + lItem.Cells[_gLeadNumber].Text.Replace(" ", " ") + "\","; 
        lCommand.CommandText += "\"" + lItem.Cells[_gSource].Text.Replace(" ", " ") + "\","; 
        lCommand.CommandText += "\"" + lItem.Cells[_gAccountName].Text.Replace(" ", " ") + "\","; 
        lCommand.CommandText += "\"" + lItem.Cells[_gCreatedOn].Text.Replace(" ", " ") + "\","; 
        lCommand.CommandText += "\"" + lItem.Cells[_gContactFullName].Text.Replace(" ", " ") + "\","; 
        lCommand.CommandText += "\"" + lItem.Cells[_gPriority].Text.Replace(" ", " ") + "\","; 
        lCommand.CommandText += "\"" + lItem.Cells[_gStreet1].Text.Replace(" ", " ") + "\","; 
        lCommand.CommandText += "\"" + lItem.Cells[_gStreet2].Text.Replace(" ", " ") + "\","; 
        lCommand.CommandText += "\"" + lItem.Cells[_gZIP].Text.Replace(" ", " ") + "\","; 
        lCommand.CommandText += "\"" + lItem.Cells[_gCity].Text.Replace(" ", " ") + "\","; 
        lCommand.CommandText += "\"" + lItem.Cells[_gState].Text.Replace(" ", " ") + "\","; 
        lCommand.CommandText += ")"; 
        lCommand.ExecuteNonQuery(); 
       } 
       } 

      lConnection.Close(); 
      } 
} 

答えて

2

は、どのような方法でデータを編集したり、操作するために使用されているグリッドですか?一般的に、グリッド自体からではなく、グリッドが配置されたデータから値をエクスポートするほうがよいでしょう。

グリッドにページングがある場合、実際に表示されるグリッドには1ページのデータしか含まれません。しかし、データが入っているデータソースにはすべてが含まれています。

データソースからエクスポートし、UIコントロールからエクスポートしないでください。

+0

ありがとうございます。私は、データをプルダウンし、varに割り当てられているLINQクエリを持っています。しかし、それはPage_Loadにあります。私はそれにアクセスするにはどうしたらいいですか?私は、データを輸出するためにそれをやり遂げることができると思うだろう。 –

+2

@Justin:LINQクエリを独自のメソッドに抽象化し、そのメソッドを 'Page_Load'と' fillDocument'の両方で呼び出す方法があります。これは、データアクセスをUIから分離するための小さなステップです(これは非常に良いことです)。 – David

1

グリッドがバインドされている場合、表示されているアイテムだけが実際にグリッドにバインドされます。ページングを使用する場合は、通常、基になるデータソースへの別の呼び出しが行われます(通常、ページングでこれを処理する必要があります)。

すべてのデータを表示する場合は、あなたのデータソースをリロードし、データソース全体からのエクスポートをベースにしてください。

関連する問題