How do I extract data from a DataTable?

前端 未结 7 1589
失恋的感觉
失恋的感觉 2020-12-02 07:08

I have a DataTable that is filled in from an SQL query to a local database, but I don\'t know how to extract data from it. Main method (in test program):

<
7条回答
  •  遥遥无期
    2020-12-02 07:36

    You can set the datatable as a datasource to many elements.

    For eg

    gridView

    repeater

    datalist

    etc etc

    If you need to extract data from each row then you can use

    table.rows[rowindex][columnindex]
    

    or

    if you know the column name

    table.rows[rowindex][columnname]
    

    If you need to iterate the table then you can either use a for loop or a foreach loop like

    for ( int i = 0; i < table.rows.length; i ++ )
    {
        string name = table.rows[i]["columnname"].ToString();
    }
    
    foreach ( DataRow dr in table.Rows )
    {
        string name = dr["columnname"].ToString();
    }
    

提交回复
热议问题