I am trying to figure out how to loop through my datatable. Any Suggestion?

风格不统一 提交于 2019-12-12 05:18:09

问题


I do not want to bind it till its been looped through. What I have here is a csv file uploaded to a gridview.I would like to loop through it before it is bound. Any Advice is great.

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


            }



    }

回答1:


Try this:

// 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
    }
}

Here is what your code should look like:

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:


Firstly, get your data;

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

Secondly, process your data what ever way you want;

foreach (DataRow row in dt.Rows) 
{
    // do what you want with it
}

Thirdly, when you have nothing else left to do with the data, bind it;

csvReaderGv.DataSource = dt;
csvReaderGv.DataBind();



回答3:


Your question is not really clear. You're asking how to loop a DataTable before you databind it. So what prevents you from simply doing:

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

But i assume that you want to know what is the best way to loop a DataTable that is bound to an ASP.NET GridView. I suggest to use the RowDataBound which enables to access all rows in the table and also all rows in the 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;
    }
}

This event is triggered for every row in the grid as soon as you DataBind it to it's DataSource anyway at this line:

 csvReaderGv.DataBind();

So it is the "cheapest" loop. Note that it's triggered only if you DataBind it, so not necessarily on every postback. If you need to access the GridViewRows on every postback(for example to create dynamic controls in the grid) you should use RowCreated instead.



来源:https://stackoverflow.com/questions/18343467/i-am-trying-to-figure-out-how-to-loop-through-my-datatable-any-suggestion

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!