Datagridview - 复选框显示为是或否 [英] Datagridview - checkbox show as yes or no
问题描述
我有一个datagridview,其中有一个复选框列,但选中后会显示以下代码
Hi,
I have a datagridview that has a column for a checkbox, but when selected its showing the following code
System.Windows.Forms.CheckBox, CheckState: 1
而不仅仅是检查,如果Check = Yes,并且Non-Check = No.
如何更改它以显示是和否,而不是检查状态:1
代码:
Rather than just the check, and if Check = Yes, and Non-Check = No.
How can i change it to show yes and no, rather than checkstate:1
Code:
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
table.Rows.Add(TextBox1.Text, TextBox2.Text, TextBox3.Text, TextBox4.Text, ComboBox1.SelectedItem.ToString(), DateTimePicker1.Text, Label8.Text, PictureBox1.Image, CheckBox1.ToString)
DataGridView1.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill
DataGridView1.RowTemplate.Height = 100
DataGridView1.AllowUserToAddRows = False
DataGridView1.DataSource = table
我的尝试:
搜索论坛,但与我将列声明为外部数据库的方式不匹配。
What I have tried:
Searched forums, but doesn't match to the way i'm declaring columns as its outside database.
推荐答案
我认为最简单的方法是将源更改为输出是/否而不是true / false。但是,如果那不可行,你也可以编写代码来操作DataTable(我假设table = DataTable)。这是一个例子,不是类型安全的,但你可以增强它。下面的函数会将列布尔类型更改为字符串,将其值更改为是/否。
I think the easiest way is to change the source to output Yes/No instead of true/false. But, if that not doable, you also can write code to manipulate the DataTable(I'm assuming table = DataTable). Here is an example, is not type safe, but you can enhance it. The below function will change the column boolean type to string and its value to Yes/No.
Public Shared Function ChangeColumnDataType(ByRef table As DataTable, columnname As String, newtype As Type) As Boolean
If table.Columns.Contains(columnname) = False Then
Return False
End If
Dim column As DataColumn = table.Columns(columnname)
If column.DataType = newtype Then
Return True
End If
Try
Dim newcolumn As New DataColumn("temporary", newtype)
table.Columns.Add(newcolumn)
For Each row As DataRow In table.Rows
Try
row("temporary") = Convert.ChangeType(row(columnname), newtype)
row("temporary") = If(CBool(row(columnname)), "Yes", "No")
Catch
End Try
Next
table.Columns.Remove(columnname)
newcolumn.ColumnName = columnname
Catch generatedExceptionName As Exception
Return False
End Try
Return True
End Function
然后调用ChangeColumnDataType函数在DataSource绑定之前。我假设列名称为活动
Then call the ChangeColumnDataType function before the DataSource binding. I'm assuming the column name is "Active"
ChangeColumnDataType(table, "Active", GetType(String))
DataGridView1.DataSource = table
资料来源:
c# - 更改填充的DataTable列数据类型 - 堆栈溢出 [ ^ ]
部分问题是您将对象CheckBox1转换为字符串而不是比它的价值。
有几种方法可以做到这一点:
1.CheckBox1.CheckState.ToString()
会给你Checked/Unchecked
2.复选框1.Checked.ToString()
会给你真/假
3.IIf( CheckBox1.Checked,是,否)
会给你是/否
Part of your problem is that you are converting the object CheckBox1 to a string rather than it's value.
There are several ways to do this properly:
1.CheckBox1.CheckState.ToString()
will give you "Checked" / "Unchecked"
2.CheckBox1.Checked.ToString()
will give you "True" / "False"
3.IIf(CheckBox1.Checked, "Yes", "No")
will give you "Yes" / "No"
这篇关于Datagridview - 复选框显示为是或否的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!