根据列名获取数据

时间:2013-02-20 02:44:36

标签: c# for-loop

如何使用Datarow根据列名检索数据?我试图从我的第一个循环

循环数据库数据
//Trying to get data
DataRow dr = dsResult.Tables[1].Rows[0];
//trying to get data successful 

//what i trying to achieve is to retrieve data from database based on rows index and 
     column name
for(int i =0; i <datagridview.Rows.Count ; i++){

   string a = dr['ColumnName'].['RowsIndex'].toString(); //Failed

}

3 个答案:

答案 0 :(得分:1)

        for (int i = 0; i < dataTable.Rows.Count; i++)
        {
            DataRow dr = dataTable.Rows[i]; //Where the RowIndex
            string a = dr[0].ToString();    //Where the ColumnIndex or ColumnName
        }

答案 1 :(得分:1)

试试?

dr.Rows[RowsIndex]['ColumnName'].ToString()

答案 2 :(得分:0)

您是否尝试删除'columnName'规范后的句点,并放置rows[i]["columnName"]而不是[columnName][rows]