vb.net 在数据绑定 DataGridView 中显示复选框

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14328359/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-17 11:53:31  来源:igfitidea点击:

Displaying a checkbox in a databound DataGridView

vb.netdatagridview

提问by Wine Too

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 0is displayed when in database is FALSE.

但是使用此代码,在我应该显示复选框的列中,0当数据库中的字符串值为FALSE.

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.
That way I can get checkboxes, but in the new column.

我在绑定之前和之后尝试使用 .Columns.Add... 但没有想要的结果。
这样我就可以得到checkboxes,但是在新列中。

回答by Jeremy Thompson

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. That's the magic behind the mapping.

当您向 DataGridView 添加列时,在该对话框中设置 DataPropertyName 以匹配 DataTable (myTable) 列名称。这就是映射背后的魔力。

enter image description here

在此处输入图片说明

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;

enter image description here

在此处输入图片说明

回答by anpadia

I had the same problem. I had a DataSet that it was filling with this SQL:

我有同样的问题。我有一个数据集,它用这个 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”列。

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"

回答by spajce

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 DataGridViewbecause 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;