天天看點

HOW TO:在您通過使用 Visual C# .NET 排序、插入或删除 DataGrid 行後檢索目前選中的 DataGrid 行 (From MSDN)

本任務的内容

  • 摘要
    • 用于在您排序、插入或删除行後檢索 DataGrid 的目前行的代碼示例
    • 确認它可以使用
  • 參考

概要

本文分步介紹在您排序、插入或删除 DataGrid 行後如何檢索目前選中的 DataGrid 行。當您排序、插入或删除 DataGrid 行後,貨币管理器仍以實際格式保留資料。修改了 DataGrid 的預設視圖,而沒有修改貨币管理器。 DataGrid 的預設視圖合并了行的任何排序、插入或删除。您可以使用預設視圖來檢索目前選中的 DataGrid 行。

傳回頁首

用于在您排序、插入或删除行後 檢索 DataGrid 的目前行的代碼示例

若要在您排序、插入或删除行後檢索 DataGrid 的目前行,請按下列步驟操作:

  1. 在 Microsoft Visual Studio .NET 中,使用 Microsoft Visual Basic .NET 或 Microsoft Visual C# .NET 建立新的 Windows 應用程式項目。

    預設情況下,将建立“Form1”。

  2. 在工具欄的“資料”頁籤上,輕按兩下“SqlDataAdapter”,然後單擊“下一步”。
  3. 單擊“建立連接配接”。鍵入您的 Microsoft SQL Server 的使用者名和密碼以連接配接到 SQL Server。
  4. 從“選擇伺服器上的資料庫”清單中,單擊選中“Northwind”,然後單擊“确定”。
  5. 在建立此連接配接後,單擊“下一步”,然後再單擊“下一步”。
  6. 在“生成 SQL 語句”頁上,鍵入 SQL 語句 Select * from Customers,然後單擊“完成”。
  7. 右鍵單擊“SqlDataAdapter1”,然後單擊“生成資料集”。單擊“确定”。
  8. 從工具箱中,将 DataGrid 控件拖到“Form1”上。
  9. 從工具箱向“Form1”添加兩個“按鈕”控件和兩個“文本框”控件。
  10. 在解決方案資料總管中,右鍵單擊“Dataset1”,然後單擊“檢視代碼”。
  11. 添加下面的代碼以便将“DataGrid”與“DataSet”綁定到一起:
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
          Button1.Text = "Get Current Row"
          Button2.Text = "Sort on City"
    
          ' Fill the DataSet with the Data from the database
          Me.SqlDataAdapter1.Fill(Me.DataSet11)
          ' Bind the DataGrid with the DataSource
          DataGrid1.DataSource = DataSet11.Tables(0).DefaultView
       End Sub      
  12. 将下面的代碼添加到“Button1_click”事件處理程式以檢索目前行資料:
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
          ' Get the Currency Manager by using the BindingContext of the DataGrid
          Dim cm As CurrencyManager = CType(Me.BindingContext(DataGrid1.DataSource, DataGrid1.DataMember), CurrencyManager)
    
          ' Retrieve the default DataView of the DataGrid
          Dim dv As DataView = CType(cm.List, DataView)
    
          ' Use Currency Manager and DataView to retrieve the Current Row
          Dim dr As DataRow
          dr = dv.Item(cm.Position).Row
    
          ' Display the Current Row Data
          TextBox1.Text = dr(0).ToString
          TextBox2.Text = dr(1).ToString
       End Sub      
  13. 将下面的代碼添加到“Button2_click”事件處理程式以便在“DataGrid”中排序資料:
    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
          ' Sort the Data
          DataSet11.Tables(0).DefaultView.Sort = "City"
          ' Refresh the Grid to display the Sorted data
          DataGrid1.Refresh()
       End Sub      
  14. 在“生成”菜單上,單擊“生成解決方案”。

傳回頁首

确認它可以使用

要驗證它是否正常工作,請按下面的步驟操作:

  1. 在“調試”菜單上,單擊“啟動”以運作該應用程式。
  2. 在“DataGrid”中單擊選中一行。
  3. 單擊“擷取目前行”。

    目前行資料顯示在“文本框”中。

  4. 單擊“按城市排序”。

    “DataGrid”中的行将根據城市來排序。

  5. 再次單擊“擷取目前行”。

    目前行資料顯示在“文本框”中。

傳回頁首

參考

有關其他資訊,請單擊下面的文章編号,以檢視 Microsoft 知識庫中相應的文章:

308070 HOW TO:Implement a Searchable DataGrid by Using ADO.NET and Windows Forms

313154 HOW TO:Create a Summary Row for a DataGrid in ASP.NET by Using Visual Basic .NET

傳回頁首

這篇文章中的資訊适用于:

  • Microsoft Visual Basic .NET (2003)
  • Microsoft Visual Basic .NET (2002)
最近更新: 2003-12-23 (2.0)
關鍵字: kbWindowsForms kbSystemData kbSqlClient kbDataBinding kbDatabase kbDataAdapter kbHOWTOmaster KB817247 kbAudDeveloper

繼續閱讀