2012-12-10 46 views
5

我有一個DataGridView在一個WinForm,我添加到DataGrid有一個複選框列使用我在這裏看到一個代碼:的DataGridView用默認值的複選框選中

DataGridViewCheckBoxColumn column = new DataGridViewCheckBoxColumn(); 
    { 
     column.HeaderText = "Export"; 
     column.Name = "Export"; 
     column.AutoSizeMode = 
      DataGridViewAutoSizeColumnMode.DisplayedCells; 
     column.FlatStyle = FlatStyle.Standard; 
     column.CellTemplate = new DataGridViewCheckBoxCell(false); 
     column.CellTemplate.Style.BackColor = Color.White; 
    } 

    gStudyTable.Columns.Insert(0, column); 

這個工作,但我想要的複選框作爲默認鋸檢查我補充說:

foreach (DataGridViewRow row in gStudyTable.Rows) 
    {     
     row.Cells[0].Value = true; 
    } 

但複選框col仍未選中。我使用集合作爲數據源,並在添加數據源後更改列的值。

+0

您可以嘗試更改數據源的集合而不是單元格值本身。像'dataSourceCollection [0] =。出口等TRUE' ... –

+0

複選框是不是我的數據源 – meirrav

+0

可以,只要您的數據網格DataBindingComplete引發的事件複選框值設置爲true的一部分:HTTP://social.msdn。 microsoft.com/Forums/en/csharplanguage/thread/2c7fe077-095e-472f-9833-c0633eb7035f – Mate

回答

9

我覺得沒有設置在列聲明的檢查值的方式。您將有 到irerate行檢查它的數據源設置(例如在DataBindingComplete事件)後:

for (int i = 0; i < dataGridView1.Rows.Count -1; i++) 
{ 
dataGridView1.Rows[i].Cells[0].Value = true; 
} 

與您列名:

for (int i = 0; i < dataGridView1.Rows.Count -1; i++) 
{ 
    dataGridView1.Rows[i].Cells["Export"].Value = true; 
} 
+0

This works非常感謝 – meirrav

+0

歡迎您:) –

+1

它適用於除最後一行以外的所有行。請將for循環更改爲:for(int i = 0; i dsmyrnaios

3

嘗試做這樣的:

foreach (DataGridViewRow row in dgv.Rows)  
{ 
    row.Cells[CheckBoxColumn.Name].Value = true;  
} 
0

您可以使用datatable並在datagridview中創建列,然後添加行,將第一列值提供爲'T rue'或'False'。

嘗試這種

0

如果((布爾)this.dataGridView2.Rows [I] .Cells [0] == .FormattedValue真)

+1

您可能希望將這樣的簡短答案作爲將來的評論。 –

0

期間或之後負荷在網格中的值,檢查在網格中的值使用此代碼,並設置格列的屬性

foreach (DataGridViewRow row in dataGridView.Rows) 
{ 
    row.Cells[0].Value = 1; 
} 

enter image description here

-2

一個簡單的方法是使用屬性NewRowIndex,如下所示:

dvgInvoiceItems.Rows[dvgInvoiceItems.NewRowIndex-1].Cells[5].Value = true; 
dvgInvoiceItems.Rows[dvgInvoiceItems.NewRowIndex-1].Cells[6].Value = true;