The DataRow
class is a core class in the System.Data.DataTable
class that represents a row of data in a DataTable
. It provides a way to access and modify individual cells within a row.
The DataRow
class is a fundamental building block for working with structured data in .NET applications. It's particularly useful when you need to manipulate or filter data based on specific row values.
To use a DataRow
, you first need to access it from a DataTable
using the Rows[]
property. Then, you can use properties like ItemArray
to access the individual cells as DataRowItem
objects, or access cell values directly using the Item[]
property.
DataTable dt = new DataTable();
dt.Columns.Add("ID", typeof(int));
dt.Columns.Add("Name", typeof(string));
DataRow dr = dt.NewRow();
dr["ID"] = 1;
dr["Name"] = "Alice";
dt.Rows.Add(dr);
// Accessing the row
foreach(DataRow row in dt.Rows)
{
// Do something with the row
}
Here are some basic examples to demonstrate how to use the DataRow
class.
// Adding a new row to the DataTable
DataTable dt = new DataTable();
dt.Columns.Add("ID", typeof(int));
dt.Columns.Add("Name", typeof(string));
DataRow dr = dt.NewRow();
dr["ID"] = 1;
dr["Name"] = "Bob";
dt.Rows.Add(dr);
// Accessing a specific cell and setting its value
DataRow row = dt.Rows[0];
row["Name"] = "Charlie";
See also: