1
フォルダからGridViewコントロールへのファイル名の表示が必要です。ASP.NetのGridviewコントロールのフォルダからファイル名を取得するC#
私はディレクトリクラスを使用したと思います。私のデータベースで
私は行ごとに、この値でsFolder列を持っている:
control/Imp/foo
私は、ウェブ上でこのtutorialを試してみましたが、私はにフォルダからファイル名を取得することはできませんGridViewコントロール。
エラーはありませんが、フォルダへのパスが正しい場合でもGridViewは空です。
以下のマイコード。
私を助けることができますか?
は本当に #Edit 01は.cs
dt2 = new DataTable();
ds2 = new DataSet();
sql = @String.Format(" SELECT * FROM tbl_2 WHERE sFolder IN ('control/Imp/foo'); ");
using (OdbcConnection cn =
new OdbcConnection(ConfigurationManager.ConnectionStrings["ConnMySQL"].ConnectionString))
{
using (OdbcCommand cmd =
new OdbcCommand(sql, cn))
{
OdbcDataAdapter adapter =
new OdbcDataAdapter(cmd);
adapter.Fill(ds2);
if (ds2.Tables.Count > 0)
{
dt2 = ds2.Tables[0];
FilePath = Server.MapPath("/myFolder/" + ds2.Tables[0].Rows[0]["sFolder"].ToString().Replace('/', '\\'));
Response.Write(FilePath);
// the response write FilePath is C:\inetpub\wwwroot\aspnet\myFolder\control\Imp\foo //
string[] filesLoc = Directory.GetFiles(FilePath);
List<ListItem> files = new List<ListItem>();
foreach (string file in filesLoc)
{
files.Add(new ListItem(Path.GetFileName(file)));
}
gvDownload.DataSource = files;
gvDownload.DataBind();
}
}
}
return ds2;
.aspxの
<asp:GridView ID="gvDownload" EmptyDataText="Data empty"
runat="server" AutoGenerateColumns="False"
BackColor="White" BorderColor="#999999" BorderStyle="None" BorderWidth="1px" GridLines="Vertical">
<AlternatingRowStyle BackColor="#DCDCDC" />
<Columns>
<asp:BoundField DataField="Text" HeaderText="FileName" />
</Columns>
</asp:GridView>
を高く評価し、任意の助けを事前にありがとう
protected void Page_Load(object sender, EventArgs e) { if (!Page.IsPostBack) { BindData(); } } private void BindData() { RetrieveProductsDowload(); } private DataSet RetrieveProductsDowload() { dt2 = new DataTable(); ds2 = new DataSet(); sql = @String.Format(" SELECT * FROM tbl_2 WHERE sFolder IN ('control/Imp/foo'); "); using (OdbcConnection cn = new OdbcConnection(ConfigurationManager.ConnectionStrings["ConnMySQL"].ConnectionString)) { using (OdbcCommand cmd = new OdbcCommand(sql, cn)) { OdbcDataAdapter adapter = new OdbcDataAdapter(cmd); adapter.Fill(ds2); if (ds2.Tables.Count > 0) { dt2 = ds2.Tables[0]; FilePath = Server.MapPath("/myFolder/" + ds2.Tables[0].Rows[0]["sFolder"].ToString().Replace('/', '\\')); Response.Write(FilePath); // the response write FilePath is C:\inetpub\wwwroot\aspnet\myFolder\control\Imp\foo // string[] filesLoc = Directory.GetFiles(FilePath); List<ListItem> files = new List<ListItem>(); foreach (string file in filesLoc) { files.Add(new ListItem(Path.GetFileName(file))); } gvDownload.DataSource = files; gvDownload.DataBind(); } } } return ds2; }
試してみてください。 'gvDownload.DataSource = Directory.GetFiles(ファイルパス); gvDownload.DataBind(); '、' AutoGenerateColumns = "True" 'を使用し、GridViewから' ... 'を削除します。それは動作しますか? –
Ritesh
@Ritesh返信ありがとうございますが、うまくいきません、GVは空です。 –
このコードを書くには、.csファイルで 'Page_Load'を使いましたか? – Ritesh