繁体   English   中英

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

[英]Displaying a checkbox in a databound 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

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

在这种情况下,如何在中间列而不是文本中获取复选框?

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

在设计时将列添加到DataGridView并将中间列设置为CheckBoxColumn。

然后设置:

With DataGridView1
   .AutoGenerateColumns = False

编辑:我现在看到了问题。 您需要将DataPropertyName设置为与列相同。

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

在此输入图像描述

这是代码:

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;

在此输入图像描述

我有同样的问题。 我有一个DataSet,它正在填充这个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

我想要将“Baja”列显示为“Checkbox”。

我可以这样做:

AutoGenerateColumns = False

但更简单的方法是,更改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

编辑:

这个建议,不要试图在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