How to: Locate a Specific Row in a DataTable
来源:互联网 发布:浙江日报报业集团cms 编辑:程序博客网 时间:2024/06/04 18:49
Most applications that consume data need to access specific records that satisfy some kind of criteria. In order to find a particular row in a dataset, you can invoke the Find method of the DataRowCollection object. If the primary key exists, then a DataRow object is returned. If the primary key cannot be found, then a null value is returned.
Finding a Row with a Primary Key Value
To find a row in a typed dataset with a primary key value
Call the strongly typed FindBy method that uses the table's primary key to locate a row.
In the following example, the CustomerID column is the primary key of the Customers table, so the generated FindBy method is FindByCustomerID. The example shows how to assign a specific DataRow to a variable using the generated FindBy method.
Visual BasicCopy CodeDim customersRow As NorthwindDataSet.CustomersRowcustomersRow = NorthwindDataSet1.Customers.FindByCustomerID("ALFKI")
C#Copy CodeNorthwindDataSet.CustomersRow customersRow = northwindDataSet1.Customers.FindByCustomerID("ALFKI");
J#Copy CodeNorthwindDataSet.CustomersRow customersRow = northwindDataSet1.get_Customers().FindByCustomerID("ALFKI");
To find a row in an untyped dataset with a primary key value
Call the Find method of a DataRowCollection collection, passing the primary key as a parameter.
The following example shows how to declare a new row called foundRow and assign it the return value of the Find method. If the primary key is found, the contents of column index 1 are displayed in a message box.
Visual BasicCopy CodeDim s As String = "primaryKeyValue"Dim foundRow As DataRow = DataSet1.Tables("AnyTable").Rows.Find(s)If foundRow IsNot Nothing Then MsgBox(foundRow(1).ToString())Else MsgBox("A row with the primary key of " & s & " could not be found")End If
C#Copy Codestring s = "primaryKeyValue";DataRow foundRow = dataSet1.Tables["AnyTable"].Rows.Find(s);if (foundRow != null) { MessageBox.Show(foundRow[1].ToString());}else{ MessageBox.Show("A row with the primary key of " + s + " could not be found");}
J#Copy CodeString s = "primaryKeyValue";DataRow foundRow = dataSet1.get_Tables().get_Item("AnyTable").get_Rows().Find(s);if (foundRow != null){ MessageBox.Show(foundRow.get_Item(1).toString());}else{ MessageBox.Show("A row with he primary key of " + s + " could not be found.");}
Finding Rows by Column Values
To find rows based on the values in any column
Data tables are created with a Select method that returns an array of DataRows based on the expression passed to the Select method. For more information on creating valid expressions, see the "Expression Syntax" section of the page about the Expression property.
The following example shows how to use the Select method of the DataTable to locate specific rows.
Visual BasicCopy CodeDim foundRows() As Data.DataRowfoundRows = DataSet1.Tables("Customers").Select("CompanyName Like 'A%'")
C#Copy CodeDataRow[] foundRows;foundRows = dataSet1.Tables["Customers"].Select("CompanyName Like 'A%'");
J#Copy CodeDataRow[] foundRows;foundRows = dataSet1.get_Tables().get_Item("Customers").Select("CompanyName Like 'A%'");
- How to: Locate a Specific Row in a DataTable
- How to add a new "CustomAction" in ECB for a specific list
- How to list files in a dictionary modified after a specific date
- How to: Create a Custom Action Specific to a List
- Primefaces: How to set page on datatable after inserting a new row
- How to find number of a specific integer in an array of Fixnums ruby
- How to use 'cp' command to exclude a specific directory?
- nrf51822, How to use a vendor specific UUID?
- How to Capture the Row Selected on Search Page In a Variable?
- git tip: How to undo an uncommitted change/deletion for a specific file in your working directory
- How to roll back/cherry-pick a specific file to a node you want?
- Four in a Row
- trees in a row
- Four In A Row
- How to force NSLocalizedString to use a specific language iOS APP内国际化
- Cognos How to set the default email sender to a specific value
- How to Succeed in A Professional Career
- How To: Implement A Server Plug-in
- 通过按钮调用帮助(模拟窗体上的‘?’功能)
- vfp程序自毁代码
- 什么是RAW文件系统?
- [ZZ]Fedora - 启动过程
- 短信支付接口
- How to: Locate a Specific Row in a DataTable
- (转)Eclipse3.2+ Myeclipse5.5+Tomcat5.5的配置
- [ZZ]{nfs:server is not responding,still trying} 解决办法
- VoIP技术基础
- 在.net中利用webbrowser控件实现WinForm与WebForm交互
- Checkboxlist多选的用法
- mysql查询上周、月、季度、年和本周、月、季度、年
- WinCE USB驱动架构及术语明析
- gst看一下1