C# DataRow

http://www.cnblogs.com/fengkuangshubiaodian/archive/2012/08/01/2609911.html

DataRow 模拟的是数据库中的一行。使用 HasVersion 和 IsNull 属性确定特定行值的状态。

1. 添加行

  创建新的 DataRow,要使用 DataTable 对象的 NewRow 方法。然后,使用 Add 方法将新的 DataRow 添加到 DataRowCollection 中。最后,调用 DataTable 对象的 AcceptChanges 方法以确认是否已添加。具体描述参考我另一篇文章 C# DataTable。

private void CreateNewDataRow()
{
    // Use the MakeTable function below to create a new table.
    DataTable table;
    table = MakeNamesTable();

    // Once a table has been created, use the
    // NewRow to create a DataRow.
    DataRow row;
    row = table.NewRow();

    // Then add the new row to the collection.
    row["fName"] = "John";
    row["lName"] = "Smith";
    table.Rows.Add(row);

    foreach(DataColumn column in table.Columns)
        Console.WriteLine(column.ColumnName);
    dataGrid1.DataSource=table;
}

private DataTable MakeNamesTable()
{
    // Create a new DataTable titled ‘Names.‘
    DataTable namesTable = new DataTable("Names"); 

    // Add three column objects to the table.
    DataColumn idColumn = new  DataColumn();
    idColumn.DataType = System.Type.GetType("System.Int32");
    idColumn.ColumnName = "id";
    idColumn.AutoIncrement = true;
    namesTable.Columns.Add(idColumn);

    DataColumn fNameColumn = new DataColumn();
    fNameColumn.DataType = System.Type.GetType("System.String");
    fNameColumn.ColumnName = "Fname";
    fNameColumn.DefaultValue = "Fname";
    namesTable.Columns.Add(fNameColumn);

    DataColumn lNameColumn = new DataColumn();
    lNameColumn.DataType = System.Type.GetType("System.String");
    lNameColumn.ColumnName = "LName";
    namesTable.Columns.Add(lNameColumn);

    // Create an array for DataColumn objects.
    DataColumn [] keys = new DataColumn [1];
    keys[0] = idColumn;
    namesTable.PrimaryKey = keys;

    // Return the new DataTable.
    return namesTable;
}

///////////////////////////////////////////////////////
DataRow workRow;
for (int i = 0; i <= 9; i++)
{
  workRow = workTable.NewRow();
  workRow[0] = i;
  workRow[1] = "CustName" + i.ToString();
  workTable.Rows.Add(workRow);
}

2. 删除行

  您可通过调用 DataRowCollection 的 Remove 方法或调用 DataRow 对象的 Delete 方法,从 DataRowCollection 中删除 DataRow。Remove 方法将行从集合中移除。与此相反,Delete 标记要移除的 DataRow。在调用AcceptChanges 方法时发生实际移除。通过调用 Delete,您可在实际删除行之前以编程方式检查哪些行被标记为移除。具体描述参考我另外一篇文章 C# DataTable。

private void DemonstrateAcceptChanges()
{
    //Run a function to create a DataTable with one column.
    DataTable table = MakeTable();
    DataRow row;

    // Create a new DataRow.
    row = table.NewRow();
    // Detached row.
    Console.WriteLine("New Row " + row.RowState);

    table.Rows.Add(row);
    // New row.
    Console.WriteLine("AddRow " + row.RowState);

    table.AcceptChanges();
    // Unchanged row.
    Console.WriteLine("AcceptChanges " + row.RowState);

    row["FirstName"] = "Scott";
    // Modified row.
    Console.WriteLine("Modified " + row.RowState);

    row.Delete();
    // Deleted row.
    Console.WriteLine("Deleted " + row.RowState);
}

private DataTable MakeTable()
{
    // Make a simple table with one column.
    DataTable table = new DataTable("table");
    DataColumn fnameColumn = new DataColumn(
        "FirstName", Type.GetType("System.String"));
    table.Columns.Add(fnameColumn);
    return table;
}
 

时间: 2024-10-05 04:42:15

C# DataRow的相关文章

DataRow[]转换DataTable

public DataTable ToDataTable(DataRow[] rows)    {        if (rows == null || rows.Length == 0) return null;        DataTable tmp = rows[0].Table.Clone();  // 复制DataRow的表结构        foreach (DataRow row in rows)            tmp.Rows.Add(row);  // 将DataRo

DataRow数组转换DataTable

public DataTable ToDataTable(DataRow[] rows) { if (rows == null || rows.Length == 0) return null; DataTable tmp = rows[0].Table.Clone(); // 复制DataRow的表结构 foreach (DataRow row in rows) tmp.Rows.Add(row.ItemArray); // 将DataRow添加到DataTable中 return tmp;

DataTable查询出DataRow数据

代码如下: DataTable dt = new DataTable(); DataColumn dc1 = new DataColumn("Id", typeof(string)); DataColumn dc2 = new DataColumn("Name", typeof(string)); dt.Columns.Add(dc1); dt.Columns.Add(dc2); DataRow dr = dt.NewRow(); dr["Id"

DataSet、DataTable、DataRow、DataColumn区别及使用实例

DataSet 表示数据在内存中的缓存. 属性 Tables  获取包含在 DataSet 中的表的集合. ds.Tables["sjxx"] DataTable 表示内存中数据的一个表. 公共属性 Columns 获取属于该表的列的集合. DataSet 获取此表所属的 DataSet. DefaultView 获取可能包括筛选视图或游标位置的表的自定义视图. PrimaryKey 获取或设置充当数据表主键的列的数组. Rows 获取属于该表的行的集合. TableName 获取或设

DataRow专为实体

把包含 对象 数据的DataRow转换成 实体对象(泛型反射方式) public static T Row2Model<T>(DataRow dr) where T : new() { T model = new T(); foreach (PropertyInfo p in (typeof(T)).GetProperties()) { if (dr[p.Name] is System.DBNull) { break; } p.SetValue(model, dr[p.Name], null)

用反射,将DataRow行转为Object对象

/// <summary> /// 反射辅助类 /// </summary> public class ReflectionHelper { /// <summary> /// 获取类型 /// </summary> /// <param name="typeAndAssName"></param> /// <returns></returns> public static Type Get

(转)DataRow的各种状态和DataView的两种过滤属性

DataRow的各种状态 http://www.cnblogs.com/zxjyuan/archive/2008/08/20/1271987.html 一个DataRow对象刚被创建之后(DataTable.NewRow())其状态是Detached,是孤立的一个存在. 所以建立了DataRow之后在DataRow中的单元填充了数据后还要通过DataTable.Rows.Add(DataRow)方法将此DataRow添加到DataTable,DataRow添加到DataTable后, 这个Dat

DataSet - DataTable - DataRow 读取 xml 文件 + 搜索

DataSet ds = XmlHelper.GetDataSetByXml(AppDomain.CurrentDomain.BaseDirectory + "/Config/ConfigName.xml");//取得xml文件 DataTable dt = ds.Tables["table_Name"];//取得xml文件的节点内容 DataRow[] dr = dr = ds.Tables["table_Name"].Select("

自动从DataRow里取数据给属性赋值

public abstract class ModelsBase     { //自动从DataRow里取数据给属性赋值 public void RightDataFrom(DataRow row) { Type type = this.GetType(); //遍历每一个属性 foreach (PropertyInfo prop in type.GetProperties()) { //判断是否可写入 if (prop.CanWrite == false) continue; //判断Data

How to use dt.Rows.Cast&lt;System.Data.DataRow&gt;().Take(n)

参考文章:http://stackoverflow.com/questions/2787458/how-to-select-top-n-rows-from-a-datatable-dataview-in-asp-net DataTable dtPage = dt.Rows.Cast<System.Data.DataRow>().Skip(pageSize * (page - 1)).Take(pageSize).CopyToDataTable();