我不想绑定它直到它被循环。我这里有一个上传到gridview的csv文件。我希望在它绑定之前循环它。任何建议都很棒。
protected void btnUpload_Click(object sender, EventArgs e)
{
if (FileUpload1.HasFile)
try
{
FileUpload1.SaveAs(Server.MapPath("") +
FileUpload1.FileName);
Label1.Text = "File name: " +
FileUpload1.PostedFile.FileName + "<br>" +
FileUpload1.PostedFile.ContentLength + " kb<br>" +
"Content type: " +
FileUpload1.PostedFile.ContentType + "<br><b>Uploaded Successfully";
}
catch (Exception ex)
{
Label1.Text = "ERROR: " + ex.Message.ToString();
}
else
{
Label1.Text = "You have not specified a file.";
}
CSVReader reader = new CSVReader(FileUpload1.PostedFile.InputStream);
string[] headers = reader.GetCSVLine();
DataTable dt = new DataTable();
foreach (string strHeader in headers)
dt.Columns.Add(strHeader);
string[] data;
while ((data = reader.GetCSVLine()) != null)
dt.Rows.Add(data);
csvReaderGv.DataSource = dt;
csvReaderGv.DataBind();
}
}
答案 0 :(得分:0)
试试这个:
// Loop through each row in the data table
foreach (DataRow row in dt.Rows)
{
// Loop through each column in row
for (int i = 0; i <= dt.Columns.Count - 1; i++)
{
// Do whatever you want here for each cell
}
}
以下是您的代码应该是什么样的:
protected void btnUpload_Click(object sender, EventArgs e)
{
if (FileUpload1.HasFile)
{
try
{
FileUpload1.SaveAs(Server.MapPath("") + FileUpload1.FileName);
Label1.Text = "File name: " +
FileUpload1.PostedFile.FileName + "<br>" +
FileUpload1.PostedFile.ContentLength + " kb<br>" + "Content type: " +
FileUpload1.PostedFile.ContentType + "<br><b>Uploaded Successfully";
}
catch (Exception ex)
{
Label1.Text = "ERROR: " + ex.Message.ToString();
}
}
else
{
Label1.Text = "You have not specified a file.";
}
CSVReader reader = new CSVReader(FileUpload1.PostedFile.InputStream);
string[] headers = reader.GetCSVLine();
DataTable dt = new DataTable();
foreach (string strHeader in headers)
{
dt.Columns.Add(strHeader);
}
string[] data;
while ((data = reader.GetCSVLine()) != null)
{
dt.Rows.Add(data);
}
// Loop through each row in the data table
foreach (DataRow row in dt.Rows)
{
// Loop through each column in row
for (int i = 0; i <= dt.Columns.Count - 1; i++)
{
// Do whatever you want here for each cell
}
}
csvReaderGv.DataSource = dt;
csvReaderGv.DataBind();
}
答案 1 :(得分:0)
首先,获取您的数据;
CSVReader reader = new CSVReader(FileUpload1.PostedFile.InputStream);
string[] headers = reader.GetCSVLine();
DataTable dt = new DataTable();
foreach (string strHeader in headers)
{
dt.Columns.Add(strHeader);
}
string[] data;
while ((data = reader.GetCSVLine()) != null)
{
dt.Rows.Add(data);
}
其次,按照您想要的方式处理您的数据;
foreach (DataRow row in dt.Rows)
{
// do what you want with it
}
第三,当你没有其他任何与数据有关的东西时,绑定它;
csvReaderGv.DataSource = dt;
csvReaderGv.DataBind();
答案 2 :(得分:0)
你的问题不是很清楚。您在设置数据绑定之前询问如何循环DataTable
。那么是什么阻止你简单地做:
foreach(DataRow row in dt.Rows)
{
// do something with the row and it's fields, e.g.
string field1 = row.Field<string>(0);
// or all columns_
foreach(DataColumn col in dt.Columns)
{
string field = row.Field<string>(col);
}
}
csvReaderGv.DataBind();
但我想你想知道循环绑定到ASP.NET DataTable
的{{1}}的最佳方法是什么。我建议使用GridView
来访问表格中的所有行以及RowDataBound
中的所有行:
GridView
只要您在此行protected void csvReaderGv_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
DataRow row = ((DataRowView) e.Row.DataItem).Row;
string col1 = row.Field<string>(0);
// set first cell in GridViewRow:
e.Row.Cells[0].Text = col1;
}
}
向DataBind
移动此行,就会触发此事件:
DataSource
所以这是最便宜的&#34;环。
请注意,仅当您 csvReaderGv.DataBind();
时才触发它,因此不一定每次回发都会触发它。如果您需要在每次回发时访问DataBind
(例如在网格中创建动态控件),则应使用GridViewRows
。