2

我想知道如何访问数据表中的单元格?我以为我可以做类似的事情

  foreach (DataRow siteRow in sites.Rows)
        {
            String siteName = siteRow.Cells[1].Text;
        }

但这似乎不像网格视图那样工作。我应该怎么办?

4

1 回答 1

6

使用索引器

foreach (DataRow siteRow in sites.Rows)
{
    String siteName = siteRow[1].ToString(); // second column
    // or via name:
    siteName = siteRow["SiteName"].ToString; // if column name is SiteName
    // or even better via Field method which is strongly typed 
    // and supports nullable types:
    siteName = siteRow.Field<string>(1); // works also via name
}
于 2013-08-12T20:34:03.793 回答