0

我不想绑定它,直到它被循环通过。我这里有一个上传到 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();


            }



    }
4

3 回答 3

0

你的问题不是很清楚。您在询问如何DataTable在数据绑定之前循环 a 。那么是什么阻止你简单地做:

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();

但我假设您想知道循环DataTable绑定到 ASP.NET的最佳方法是什么GridView。我建议使用RowDataBoundwhich 可以访问表中的所有行以及所有行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

 csvReaderGv.DataBind();

所以这是“最便宜”的循环。请注意,它仅在您触发时触发DataBind,因此不一定在每次回发时触发。如果您需要GridViewRows在每次回发时访问(例如在网格中创建动态控件),您应该使用RowCreated

于 2013-08-20T19:43:12.670 回答
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();
于 2013-08-20T19:41:41.923 回答
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();
}
于 2013-08-20T19:34:06.527 回答