我想通过我的数据表,以弄清楚如何循环。什么建议吗? [英] I am trying to figure out how to loop through my datatable. Any Suggestion?

查看:139
本文介绍了我想通过我的数据表,以弄清楚如何循环。什么建议吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不希望绑定它,直到它被循环通过。我这里是上传到gridview.I csv文件想通过它循环势必之前。任何意见是很大的。

 保护无效btnUpload_Click(对象发件人,EventArgs的发送)
     {
         如果(FileUpload1.HasFile)
        尝试
        {
            FileUpload1.SaveAs(使用Server.Mappath()+
                 FileUpload1.FileName);
            Label1.Text =文件名:+
                 FileUpload1.PostedFile.FileName +< BR>中+
                 FileUpload1.PostedFile.ContentLength +KB< BR>中+
                 内容类型:+
                 FileUpload1.PostedFile.ContentType +< BR>< B>上传成功;
        }
        赶上(异常前)
        {
            Label1.Text =错误:+ ex.Message.ToString();
        }
    其他
    {
        Label1.Text =您还没有指定的文件。
    }        CSVReader读卡器=新CSVReader(FileUpload1.PostedFile.InputStream);
        字符串[] =头reader.GetCSVLine();
        DataTable的DT =新的DataTable();        的foreach(在头字符串strHeader)
            dt.Columns.Add(strHeader);
        字符串[]的数据;
        而((数据= reader.GetCSVLine())!= NULL)
            dt.Rows.Add(数据);
        csvReaderGv.DataSource = DT;        csvReaderGv.DataBind();
            }    }


解决方案

您的问题是不是真的清楚。你问如何循环一个数据表数据绑定你之前。那么,什么prevents你简单地做:

 的foreach(在dt.Rows的DataRow行)
{
    //做该行的东西,它的领域,例如
    串FIELD1 = row.Field&所述;串GT;(0);
    //或全部列_
    的foreach(在dt.Columns的DataColumn COL)
    {
        串场= row.Field<串GT;(COL);
    }
}
csvReaderGv.DataBind();

但我相信,你想知道什么是循环的最佳方式数据表绑定到ASP.NET GridView控件。我建议使用的RowDataBound 使来访问 GridView控件表中的所有行和所有行:

 保护无效csvReaderGv_RowDataBound(对象发件人,GridViewRowEventArgs E)
{
    如果(e.Row.RowType == DataControlRowType.DataRow)
    {
        行的DataRow =((DataRowView的)e.Row.DataItem).Row;
        串的col1 = row.Field&所述;串GT;(0);
        //设置第一个单元格中的GridViewRow:
        e.Row.Cells [0] =。文本COL1;
    }
}

此事件,只要你的DataBind 它到它的触发网格中的每一行数据源反正在该行:

  csvReaderGv.DataBind();

所以它是最廉价的循环。
需要注意的是它的触发只有当你的DataBind ,所以不一定在每一个回发。如果您需要访问 GridViewRows 在每次回发(例如创建网格动态控件),你应该使用 RowCreated 来代替。

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


            }



    }

解决方案

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.

这篇关于我想通过我的数据表,以弄清楚如何循环。什么建议吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆