2014-01-14 60 views
0

我正在開發一個WinForm應用程序。在我的應用程序中,有一個DataGridView控件綁定到一個DataTable對象。現在我想在其中的一個特定列中顯示用戶ProgressBar,但我想僅使用DataTable對象來執行此操作。我試過這個,但似乎沒有任何工作將控件添加到綁定到DataTable的DataGridView中

  dataGridView1.DataSource = dataTable; 

      DataColumn dc = new DataColumn("string", typeof(string)); 
      DataColumn dc2 = new DataColumn("Progress", typeof(ProgressBar)); 
      dataTable.Columns.Add(dc); 
      dataTable.Columns.Add(dc2); 

      DataRow dr = dataTable.NewRow(); 
      dr[0] = 1; 
      ProgressBar p = new ProgressBar(); 
      p.Value = 35; 
      dr[1] = p; 
      dataTable.Rows.Add(dr); 

也許我所做的是錯誤的。有沒有其他的方式來實現這一目標?

回答

0

嘗試下面的代碼:

DataTable _table = (dataGridView1.DataSource as DataTable); 

DataColumn dc = new DataColumn("string", typeof(string)); 
DataColumn dc2 = new DataColumn("Progress", typeof(ProgressBar)); 
_table.Columns.Add(dc); 
_table.Columns.Add(dc2); 

DataRow dr = _table.NewRow(); 
dr["string"] = 1; 
ProgressBar p = new ProgressBar(); 
p.Value = 35; 
dr["Progress"] = p; 

_table.Rows.Add(dr); 

使用ColumnName instate使用ColumnIndex在DataRow中插入值。

請檢查this link在DGV單元中創建ProgressBar

+0

Nada。沒有幫助:(。 –

+0

@ GaneshS爲我工作,你是否得到任何錯誤? –

+0

沒有任何錯誤,只是空網格 有一件事要提,而不是 'DataTable _table =(DataGridView1.DataSource as DataTable);' 我已經使用 'dataGridView1.DataSource = dataTable;' 因爲它返回錯誤null對象。 –

相關問題