我有一個DataTable綁定到DataGridView。我在DGV中啓用了FullRowSelect。有沒有辦法將選定的行作爲DataRow獲得,以便我可以對所選行的值進行強類型訪問?如何獲取DataGridView中選定的DataRow?
回答
我不知道如何做到這一點瓦特/ OA BindingSource的,這裏是如何與一個做到這一點:
var drv = bindingSoure1.Current as DataRowView;
if (drv != null)
var row = drv.Row as MyRowType;
您應該能夠直接投你選擇一行到強類型一行被綁定到DataGridView。
DataRowView currentDataRowView = (DataRowView)dgv1.CurrentRow.DataBoundItem
DataRow row = currentDataRowView.Row
有可能通過獲取以下屬性:
this.dataGridView.SelectedRows
得到一個類型的集合:DataGridViewSelectedRowCollection。它包含以下類型的項目:DataGridViewRow。
那麼可以得到bounditem與那些自己鍵入以下方式:
DataGridViewSelectedRowCollection list = this.dataGridViewInventoryRecords.SelectedRows;
MyType selectedItem = (MyType)list[0].DataBoundItem; //[0] ---> first item
你試試這個。
DataRow row = gridView1.GetDataRow(gridView1.FocusedRowHandle);
if (row != null)
{
XtraMessageBox.Show(row["ID"].ToString());
}
else
return;
如果您已綁定您的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通過的BindingSource綁定到數據表中,執行如下:
創建一個新項目。
插入一個名爲ds1的DataSet和一個名爲dt99的DataTable,其中列名爲DataColumn1和DataColumn2,都是字符串類型。
一個DataGridView添加到主窗體,並將其綁定到dt99
從而使dt99BindingSource連接的DataGridView和數據表
添加和事件處理程序的選擇更改DataGridView的,然後將下面的代碼段
私人無效dataGridView1_SelectionChanged(對象發件人,EventArgs的){
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);
}
- 1. 如何從DataGridView的行中獲取DataRow
- 2. 如何從datagridview的選定行中獲取id
- 3. 如何從不同頁面獲取DataGridView中的選定行
- 4. 如何從datagridview中的選定行獲取ID?
- 5. 如何將DataGridView中的選定項目作爲DataTable獲取?
- 6. 獲取選定的dataGridView列數據?
- 7. DataGridView獲取當前選定的對象
- 8. 從DataGridView獲取選定的行
- 9. 如何獲取datagridview組合框的選定項的值
- 10. 如何獲取選定單元格的值DataGridView C#?
- 11. 如何從DataRow獲取原始DataColumn值?
- 12. 將DataGridView中的DataRow投射到自定義對象?
- 13. 獲取對象與DataGridView中的選定行關聯時出錯
- 14. 獲取與DataGridView中選定行關聯的對象
- 15. C#Datagridview:獲取組合框中的選定項目
- 16. 從DataRow獲取DefaultView DataRowView
- 17. 從ASP.NET GridView獲取DataRow
- 18. 如何獲取數據表中DataRow的列值
- 19. 如何獲取表中的選定行?
- 20. 如何在DataGridView中獲取CurrentCell位置
- 21. 獲取所選列數出來的DataGridView
- 22. 如何取消選中DataGridView中的複選框
- 23. 如何正確獲取datagridview的
- 24. 如何獲取DataGridView的DateTimePicker checkstate值?
- 25. datagridview中選定的索引
- 26. 如何在選擇一個DataGridView獲取特定的細胞改變
- 27. 獲取選定的Datagridview(行)到數據庫
- 28. Datagridview獲取特定列的總和
- 29. 從datagridview的選擇中獲取數據表中相應的行
- 30. 如何獲取DropDownListFor的選定項目?
嗯......對於我來說,選中的行的「DataBoundItem」爲空。 – 2016-06-16 12:21:27