2017-08-07 33 views
-1

我有一個datagridview。用戶在datagridview中輸入數據。現在我想從datagridview獲取數據到數據表。如何從C#中的datagridview中獲取數據.net

+0

請告訴我們你的嘗試。 – Ben

+0

BindingSource bs = new BindingSource(); bs.DataSource = dataGridView2.DataSource; DataTable dat =(DataTable)(bs.DataSource); –

+0

但dataGridView2.DataSource爲空 –

回答

0
for (int rows = 0; rows < dataGrid.Rows.Count; rows++) 
{ 
    for (int col= 0; col < dataGrid.Rows[rows].Cells.Count; col++) 
    { 
     string value = dataGrid.Rows[rows].Cells[col].Value.ToString(); 
     //fill your model and prepare object to save in database 
    } 
} 

您必須從模型對象中的網格中獲取數據,然後必須使用該數據發送保存呼叫。 希望這是你想要的

+0

感謝您的回答。但是有沒有什麼辦法可以從datagridview中獲取並像上面那樣保存數據表 –

相關問題