iT邦幫忙

0

(已解決)C# Datagrid疑問

Ray 2020-10-06 15:00:031187 瀏覽

上網爬文找取得Datagrid欄位值的方法,執行後會報錯,煩請協助

程式碼

string s_1 = (dg_8.Columns[0].GetCellContent(dg_8.Items[1]) as TextBlock).Text

error
https://ithelp.ithome.com.tw/upload/images/20201006/20129652D1zzpB1KLn.png


後來爬文找到解決方法-MVVM
連結

另外無意間發現在程式碼前加上Messagebox.show()方法就能取得欄位值,待高手解惑!

圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

2 個回答

0
allenlwh
iT邦高手 1 級 ‧ 2020-10-06 15:15:54
private void PrintCellValues(DataGrid myGrid){
    int iRow;
    int iCol;
    DataTable myTable;
    // Assumes the DataGrid is bound to a DataTable.
    myTable = (DataTable) dataGrid1.DataSource;
    for(iRow = 0;iRow < myTable.Rows.Count ;iRow++) {
       for(iCol = 0;iCol < myTable.Columns.Count ;iCol++) {
          Console.WriteLine(myGrid[iRow, iCol]);
       }
    }
 }

參考來源

Ray iT邦新手 5 級 ‧ 2020-10-06 15:53:57 檢舉

感謝回答,剛試了一下,WPF的Datagrid控件似乎是隸屬於Controls

請問要如何轉成Forms.Datagrid?

0
sonia
iT邦新手 2 級 ‧ 2020-10-07 09:23:39

刪除

Ray iT邦新手 5 級 ‧ 2020-10-07 15:31:27 檢舉

@@

我要發表回答

立即登入回答