我正试图弄清楚如何在我的数据表中循环.任何建议

本文关键字:数据表 我的 循环 任何建 弄清楚 | 更新日期: 2023-09-27 18:28:27

在循环通过之前,我不想绑定它。我这里有一个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();

            }

    }

我正试图弄清楚如何在我的数据表中循环.任何建议

试试这个:

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

首先,获取您的数据;

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

您的问题并不十分清楚。你在问如何在数据绑定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 GridViewDataTable的最佳方式。我建议使用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,就会触发此事件:

 csvReaderGv.DataBind();

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