C#DatagridView-禁用自动转换布尔值到复选框 [英] C# DatagridView - Disable autoconversion bool values to checkbox

查看:58
本文介绍了C#DatagridView-禁用自动转换布尔值到复选框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有一些布尔列的DataTable,当我用作Datagridview数据源时,Datagridview将这些布尔单元格显示为复选框.有可能避免吗?我需要字符串单元格

I have a DataTable with some boolean columns and when i use as Datagridview datasource, the Datagridview display those boolean cells as checkbox. Is it possible avoid it? I need string cells

谢谢如果更容易,我可以编辑数据表

Thanks i can edit the datatable if it's easier

推荐答案

我看到了两种解决方法:

I see two ways for it:

  1. DataGridView 中设置属性 AutoGenerateColumns = false ,然后在设计器中或通过代码创建列.
    对于 bool 字段,请使用 DataGridViewTextBoxColumn 类型的列.
    DataGridViewTextBoxColumn 将自动以 True False
  2. bool 转换为文本
  1. In DataGridView set property AutoGenerateColumns = false, then create columns in the designer or by code.
    For bool field use column of DataGridViewTextBoxColumn type.
    DataGridViewTextBoxColumn will convert bool to the text automatically as True or False

预定义的列可能是一个很好的解决方案,因为您可以控制向用户显示数据的方式,并且值保持原始类型.

Predefined columns can be good solution, because you can control how data are displayed to the user, and values remain in original types.

  1. 通过更改sql查询或操作您的 DataTable
  2. ,将值转换为数据源中的字符串
  1. Convert value to the string in the datasource by changing sql query or manipulating your DataTable

DataTable newDataTable = yourDataTable.Clone();
newDataTable.Columns["BoolTypeColumnName"].DataType = typeof(bool);
foreach (DataRow row in yourDataTable.Rows) 
{
    newDataTable.ImportRow(row);
}
this.YourDataGridView.DataSource = newDataTable;

这篇关于C#DatagridView-禁用自动转换布尔值到复选框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆