簡體   English   中英

如何在DataGridView中獲取選中的DataRow?

[英]How Do I Get the Selected DataRow in a DataGridView?

我有一個綁定到 DataGridView 的 DataTable。 我在 DGV 中啟用了 FullRowSelect。 有沒有辦法將所選行作為 DataRow 獲取,以便我可以對所選行的值進行強類型訪問?

DataRowView currentDataRowView = (DataRowView)dgv1.CurrentRow.DataBoundItem
DataRow row = currentDataRowView.Row

我不知道如何在沒有 BindingSource 的情況下做到這一點,以下是如何做到這一點:

var drv = bindingSoure1.Current as DataRowView;
if (drv != null)
  var row = drv.Row as MyRowType;

可以通過獲得以下屬性:

this.dataGridView.SelectedRows

一個獲取類型的集合: DataGridViewSelectedRowCollection 它包含以下類型的項目: DataGridViewRow

然后可以通過以下方式獲得具有自己類型的 bounditem :

DataGridViewSelectedRowCollection list = this.dataGridViewInventoryRecords.SelectedRows;
MyType selectedItem = (MyType)list[0].DataBoundItem; //[0] ---> first item

您應該能夠直接將您選擇的行轉換為綁定到 DataGridView 的強類型行。

試試這個:

DataRow row = gridView1.GetDataRow(gridView1.FocusedRowHandle); `        
if (row != null)
{
     XtraMessageBox.Show(row["ID"].ToString());
}

如果您已將 datagridview 綁定到數據庫中的表或視圖,則可以將數據作為強類型對象取出。

此答案適用於在設計時使用 DataSet 連接到數據庫的 Windows 窗體。 示例名稱為 DataSet1,示例表名稱為 Customer_Info。

// cast the data bound item to DataRowView so you have
// access to "Row", which
// has the actual data for the row in typed fields. 
DataRowView drv = dgv.SelectedRows(0).DataBoundItem as DataRowView;
// run the code and look at the debugger value of drv.Row -- 
// the type will be shown
// which is the type created by the data binding, representing 
// your table or view
//{YourDataSetName.YourTableOrViewType} tmpTableData = drv.Row as {YourDataSetName.YourTableOrViewType};
DataSet1.Customer_InfoRow tmpTableData = drv.Row as DataSet1.Customer_InfoRow;

這個鏈接就是答案。 我希望我在上面增加了清晰度和示例。 https://social.msdn.microsoft.com/Forums/windows/en-US/f252e395-58e6-4703-ba7b-0740efcbecf3/can-i-convert-the-selected-row-in-a-bound-datagridview- to-a-typed-datarow?forum=winformsdatacontrols

此鏈接顯示以編程方式添加到數據源的數據,而不是從現有數據庫中提取該數據。 這讓我找到了答案: https : //msdn.microsoft.com/en-us/library/4wszzzc7(v=vs.110).aspx

如果數據集內未使用 tableadapter,則不可能對 datagridview 進行強類型訪問。

要在 datagridview 通過綁定源綁定到數據表時訪問強類型變量,請執行以下操作:

創建一個新項目。

插入名為 ds1 的數據集和名為 dt99 的數據表,其中列名為 DataColumn1 和 DataColumn2,均為字符串類型。

在此處輸入圖片說明

在主窗體中添加一個datagridView並綁定到dt99

在此處輸入圖片說明

使dt99BindingSource連接datagridview和datatable

在此處輸入圖片說明

為datagridview的Selection Change添加事件處理程序,插入如下代碼

private void dataGridView1_SelectionChanged(Object sender, EventArgs e) {

  ds1.dt99Row d= ((ds1.dt99Row)((DataRowView)dt99BindingSource.Current).Row);

  Debug.WriteLine(d.DataColumn1 + " " + d.DataColumn2);

}

現在您有強類型變量(d.DataColumn1 和 d.DataColumn2)來訪問在 datagridview 上選擇的單元格

另一個有趣的特性是插入到數據集中的數據表提供了一組公共類,在處理數據表時有很大幫助,例如

        private void Form1_Load(Object sender, EventArgs e)
    {
        ds1.dt99.Adddt99Row("K", "B");
        ds1.dt99.Adddt99Row("L", "D");
        ds1.dt99.Adddt99Row("M", "F");
        ds1.dt99.Adddt99Row("N", "H");

        ds1.dt99Row dr = ds1.dt99.Newdt99Row();
        dr.DataColumn1 = "X";
        dr.DataColumn2 = "Y";
        ds1.dt99.Adddt99Row(dr);

    }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM