【问题标题】:I am trying to figure out how to loop through my datatable. Any Suggestion?我想弄清楚如何遍历我的数据表。有什么建议吗?
【发布时间】:2013-08-23 00:01:30
【问题描述】:

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


            }



    }

【问题讨论】:

  • 是什么阻止你做foreach(DataRow row in dt.Rows)...
  • 我把它放在 csvReaderGv.DataSource = dt; csvReaderGv.DataBind();或之后。它让我感到困惑。
  • 是的,因为您的要求是:“我想在绑定之前循环遍历它”。这个问题有点奇怪吧?
  • 不觉得这个问题很奇怪。抱歉,我真的想尽可能清楚。

标签: c# asp.net data-binding datagridview datasource


【解决方案1】:

试试这个:

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

【讨论】:

  • 我是在绑定之前还是之后把它放在哪里让我感到困惑。
【解决方案2】:

首先,获取您的数据;

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

【讨论】:

    【解决方案3】:

    你的问题不是很清楚。您在询问如何在数据绑定之前循环 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

    【讨论】:

    • 非常感谢,这是否也允许我只显示我想要显示的任何列。
    • @Jay:我必须承认我没有理解你的评论。如果要隐藏列,请使用GridView.Columns[index].Visible = false;。如果您想隐藏单元格(或执行其他操作,例如应用 CSS 或更改 ColumnSpan),请使用 e.Row.Cells[index].Visible=false;
    • 对不起,我要问的是在我做了数据绑定之后是否可以使用
    • 那是 aspx 不是代码隐藏。因此,您可以使用BoundFieldsTemplateFields 并从代码隐藏中访问它们(或修改它们的Text)。但目前还不清楚你真正想要达到的目标。
    • 我从所有帮助中得到了我需要的东西,非常感谢。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2023-03-25
    • 1970-01-01
    • 2018-01-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多