2013-01-14 38 views
0

我無法從數據庫的布爾列正確填充DataGridView複選框列。顯示數據綁定中的複選框DataGridView

第一的Form_Load代碼:

Me.DataGridView1.DataSource = Me.bindingSource1 
GetData("SELECT myInt, myBool, myString " & _ 
     "FROM " & myFavTable & " " & _ 
     "WHERE (myInt > 100) ORDER BY myString") 

formatGrid() 

在我的GetData填寫mytable的數據:

Me.dataAdapter.Fill(myTable) 
Me.bindingSource1.DataSource = myTable 

,並顯示之前最後我格式電網。

我手動格式化它,因爲加載比自動格式化要快得多。

With DataGridView1 
     .AllowUserToAddRows = False 
     .AllowDrop = False 
     .AllowUserToOrderColumns = False 
     .AllowUserToResizeRows = False 
     .SelectionMode = DataGridViewSelectionMode.FullRowSelect 
     .MultiSelect = False 
     .Dock = DockStyle.Fill 
     .EditMode = DataGridViewEditMode.EditProgrammatically 

     With .Columns(0) 
      .Name = "postN" 
      .HeaderText = "Postal" 
      .Width = 55 
     End With 

     With .Columns(1) 'here should be a checkbox 
      .Width = 20 
     End With 

     With .Columns(2) 
      .Name = "colCity" 
      .HeaderText = "City" 
      .Width = 180 
     End With 
    End With 

但有了這個代碼,在我的專欄中要顯示覆選框字符串值0當數據庫FALSE顯示。

在這種情況下,我可以如何獲得中間列中的複選框而不是文本?

我嘗試使用.Columns.Add ...綁定前後,但沒有想要的結果。
這樣我可以得到checkboxes,但在新的專欄。

+0

在綁定呢? –

+0

製作標題http://meta.stackexchange.com/questions/10647/how-do-i-write-a-good-title – spajce

+0

我授權@ spajce編輯您的標題。請參閱[「應該在其標題中包含」標籤「的問題嗎?」](http://meta.stackexchange.com/questions/19190/),其中的共識是「否,他們不應該」 –

回答

2

在設計階段將列添加到DataGridView並將中間列設置爲CheckBoxColumn。

然後設置:

With DataGridView1 
    .AutoGenerateColumns = False 

編輯: 我現在看到的問題。您需要將DataPropertyName設置爲與該列相同。

將列添加到DataGridView時,在該對話框中將DataPropertyName設置爲與DataTable(myTable)列名稱匹配。這是映射背後的魔力。

enter image description here

下面是代碼:

DataTable dt = new DataTable(); 
dt.Columns.Add("TextBoxCol"); 
dt.Columns.Add("CheckBoxCol"); 
DataRow dr = dt.NewRow(); 
dr[0] = "Hello"; 
dr[1] = false; 
dt.Rows.Add(dr); 
dr = dt.NewRow(); 
dr[0] = "World"; 
dr[1] = true; 
dt.Rows.Add(dr); 
dataGridView1.DataSource = dt; 

enter image description here

+0

這樣我得到新的列? Designtime + Runtime組合在一起。 –

+0

那好吧,如果你看到兩倍的列數,就不要在RunTime中添加它們(因爲你已經在設計時指定了它們)。否則,請查看Form1.Designer.vb代碼以查看設計時間列的創建方式,並在RunTime中重新使用該代碼並創建列。 –

+0

我沒有做運行列,有一些東西。綁定源碼還是什麼?我不能刪除,如果我想保持綁定。 –

0
Dim cell As DataGridViewCell = New DataGridViewCheckBoxCell() 

With DataGridView1 
    With .Columns(1) 
     .CellTemplate = cell 
    End With 
End With 

編輯:

這一個建議,不要試圖在設計中添加列時間您DataGridView因爲您查詢本身就會產生一個DataGridViewCheckBoxCell

GetData("SELECT myInt AS Id, myBool AS Bool, myString AS String " & _ 
     "FROM " & myFavTable & " " & _ 
     "WHERE (myInt > 100) ORDER BY myString") 


Me.dataAdapter.Fill(myTable) 
Me.bindingSource1.DataSource = myTable 

DataGridView1.DataSource = bindingSource1; 
+0

爲CellTemplate提供的值必須是System.Windows.Forms.DataGridViewTextBoxCell類型或從中派生。 –

+0

那是錯誤信息? – spajce

+0

好吧,我注意到我會盡力再次檢查:) – spajce

1

我有同樣的問題。 我有一個數據集,它正在與此SQL填充:

"SELECT nombre, CASE WHEN fecha IS NULL THEN 0 ELSE 1 END AS baja" 

分配

dtgEmpleado.DataSource = ds.Tables(0) 

With dtgEmpleado 
    .Columns(0).HeaderText = "Nombre" 
    .Columns(0).DataPropertyName = "nombre" 
    .Columns(0).Name = "nombre" 
    .Columns(0).Width = 100 
    .Columns(1).HeaderText = "Baja" 
    .Columns(1).DataPropertyName = "baja" 
    .Columns(1).Name = "baja" 
    .Columns(1).Width = 70 
End With 

我想,列「巴哈」它被顯示爲「複選框」。

我可以做到這一點:

AutoGenerateColumns = False 

但是,一個更簡單的方法,改變SQL語句:

"SELECT nombre, CAST(CASE WHEN fecha IS NULL THEN 0 ELSE 1 END AS BIT) AS baja" 
相關問題