🐋作者简介:博主是一位.Net开发者,同时也是RPA和低代码平台的践行者。
🐬个人主页:会敲键盘的肘子
🐰系列专栏:.Net实用方法总结
🦀专栏简介:博主针对.Net开发和C站问答过程中遇到的问题进行总结,形成本专栏,希望可以帮助到您解决问题。
🐶座右铭:总有一天你所坚持的会反过来拥抱你。
🌈写在前面:
本文主要介绍System.Data命名空间的 DataRow 类,介绍其常用的方法和实践。
👉本文关键字:System.Data、DataRow类、DataTable类、方法实践、C#
文章目录
-
-
- 1️⃣ System.Data命名空间
- 2️⃣ DataRow类
-
- ♈ 定义
- ♊ 属性
- ♉ 常用方法
-
- AcceptChanges() 提交自上次调用 AcceptChanges() 以来对该行进行的所有更改
- BeginEdit() 对 DataRow 对象启动编辑操作
- EndEdit() 终止该行的编辑
- CancelEdit() 提交自上次调用 AcceptChanges() 以来对该行进行的所有更改
- Delete() 删除 DataRow
- IsNull(DataColumn) 获取一个值,该值指示指定的 DataColumn 是否包含 null 值
- IsNull(String) 获取一个值,该值指示指定的列是否包含 null 值
- IsNull(Int32) 获取一个值,该值指示位于指定索引处的列是否包含 null 值
- GetChildRows(DataRelation) 使用指定的 DataRelation 获取此 DataRow 的子行
- GetChildRows(String) 使用 DataRelation 的指定 RelationName 获取 DataRow 的子行
- GetParentRow(DataRelation) 使用指定的 DataRelation 获取此 DataRow 的父行
- GetParentRow(String) 使用 DataRelation 的指定 RelationName 获取 DataRow 的父行
- SetNull(DataColumn) 将指定 DataColumn 的值设置为 null 值
- ♌ 注解
- ♋ 更多方法
-
1️⃣ System.Data命名空间
提供对表示 ADO.NET 体系结构的类的访问权限。 通过 ADO.NET,可以生成可有效管理多个数据源的数据的组件。
2️⃣ DataRow类
♈ 定义
表示 DataTable 中的一行数据。
public class DataRow
示例
以下示例通过调用NewRow对象的方法DataTable创建新的DataRow方法。
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;
}
♊ 属性
Item[DataColumn] 获取或设置指定 DataColumn 中存储的数据。
public object this[System.Data.DataColumn column] { get; set; }
示例
以下示例演示如何使用 [Item] 属性获取和设置特定列索引的值。 第一个示例获取用户在控件中 DataGrid 单击的任何行中第一列的值。 第二个设置作为参数传递给方法的值。
Private Sub DataGrid1_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs)
Dim dataGridTable As DataTable = _
CType(DataGrid1.DataSource, DataTable)
' Set the current row using the RowNumber
' property of the CurrentCell.
Dim currentRow As DataRow = _
dataGridTable.Rows(DataGrid1.CurrentCell.RowNumber)
Dim column As DataColumn = dataGridTable.Columns(1)
' Get the value of the column 1 in the DataTable.
label1.Text = currentRow(column).ToString()
End Sub
Private Sub SetDataRowValue( _
ByVal grid As DataGrid, ByVal newVal As Object)
' Set the value of a column in the last row of a DataGrid.
Dim table As DataTable = CType(grid.DataSource, DataTable)
Dim row As DataRow = table.Rows(table.Rows.Count - 1)
Dim column As DataColumn = table.Columns("FirstName")
row(column)= newVal
End Sub
Item[Int32] 获取或设置由索引指定的列中存储的数据
public object this[int columnIndex] { get; set; }
示例
以下示例演示如何使用 [Item] 属性获取和设置特定列索引的值。 第一个示例获取用户在控件中 DataGrid 单击的任何行中第一列的值。
private void DataGrid1_Click(object sender,
System.EventArgs e)
{
// Get the DataTable the grid is bound to.
DataGrid thisGrid = (DataGrid) sender;
DataTable table = (DataTable) thisGrid.DataSource;
DataRow currentRow =
table.Rows[thisGrid.CurrentCell.RowNumber];
// Get the value of the column 1 in the DataTable.
Console.WriteLine(currentRow[1]);
// You can also use the name of the column:
// Console.WriteLine(currentRow["FirstName"])
}
private void SetDataRowValue(DataGrid grid, object newValue)
{
// Set the value of the last column in the last row of a DataGrid.
DataTable table;
table = (DataTable) grid.DataSource;
DataRow row;
// Get last row
row = (DataRow)table.Rows[table.Rows.Count-1];
// Set value of last column
row[table.Columns.Count-1] = newValue;
}
Item[String] 获取或设置由名称指定的列中存储的数据。
public object this[string columnName] { get; set; }
示例
以下示例演示如何使用 [Item] 属性获取和设置特定列索引的值。 第一个示例获取用户在控件中 DataGrid 单击的任何行中第一列的值。 第二个设置作为参数传递给方法的值。
private void DataGrid1_Click(
object sender, System.Eve