简体   繁体   English

在数据绑定DataGridView中显示一个复选框

[英]Displaying a checkbox in a databound DataGridView

I am unable to correctly populate a DataGridView checkbox column from a boolean column from a database. 我无法从数据库的布尔列正确填充DataGridView复选框列。

First form_load code: 第一个form_load代码:

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

formatGrid()

In GetData I fill myTable with data: 在GetData中,我用myTable填充数据:

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

And finally I format grid the before showing. 最后我在显示前格式化网格。

I format it manually because loading is much faster than with automatic formatting. 我手动格式化,因为加载比自动格式化要快得多。

    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

But with this code, in my column that should show checkboxes the string value 0 is displayed when in database is FALSE . 但是使用此代码,在我的列中应显示复选框,当数据库为FALSE时,将显示字符串值0

How in this situation can I get checkboxes in the middle column instead of text? 在这种情况下,如何在中间列而不是文本中获取复选框?

I try with .Columns.Add... before and after binding but with no wanted results. 我尝试使用.Columns.Add ...绑定之前和之后但没有想要的结果。
That way I can get checkboxes , but in the new column. 这样我就可以获得checkboxes ,但是在新列中。

In design-time add the columns to the DataGridView and set the middle column as a CheckBoxColumn. 在设计时将列添加到DataGridView并将中间列设置为CheckBoxColumn。

Then set: 然后设置:

With DataGridView1
   .AutoGenerateColumns = False

Edit: I see the problem now. 编辑:我现在看到了问题。 You need to set the DataPropertyName to be the same as the column. 您需要将DataPropertyName设置为与列相同。

When you add columns to the DataGridView, in that dialog set the DataPropertyName to match the DataTable (myTable) column Names. 向DataGridView添加列时,在该对话框中设置DataPropertyName以匹配DataTable(myTable)列名称。 That's the magic behind the mapping. 这是映射背后的魔力。

在此输入图像描述

Here is the code: 这是代码:

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;

在此输入图像描述

I had the same problem. 我有同样的问题。 I had a DataSet that it was filling with this SQL: 我有一个DataSet,它正在填充这个SQL:

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

Assignment 分配

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

I wanted that the column "Baja" it was displaying as a "Checkbox". 我想要将“Baja”列显示为“Checkbox”。

I could do it with: 我可以这样做:

AutoGenerateColumns = False

But an easier way, changing the SQL sentence: 但更简单的方法是,更改SQL语句:

"SELECT nombre, CAST(CASE WHEN fecha IS NULL THEN 0 ELSE 1 END AS BIT) AS baja"
Dim cell As DataGridViewCell = New DataGridViewCheckBoxCell()

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

EDIT: 编辑:

This a suggestion, don't try to add columns at design-time in your DataGridView because you query itself it will generate a DataGridViewCheckBoxCell 这个建议,不要试图在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;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM