替换 datagridview 列中的 true/false [英] replace true/false in datagridview columns

查看:24
本文介绍了替换 datagridview 列中的 true/false的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 datagridview,我按如下方式填充它:

var q= repository.GetStudents();//dataGridView1.DataSource = null;dataGridView1.Columns.Clear();dataGridView1.DataSource = q;dataGridView1.Columns.RemoveAt(1);//删除IsActive//因为我想有自己的实现dataGridView1.Columns[0].DataPropertyName = "StudentID";dataGridView1.Columns[0].HeaderText = "Studunet ID";dataGridView1.Columns[1].DataPropertyName = "IsActive";dataGridView1.Columns[1].HeaderText = "状态";

IsActive"属性是布尔类型.当显示IsActive"单元格时,它显示真/假.我想用我自己的自定义值替换它.

我阅读了这个和<一个 href="https://stackoverflow.com/questions/9532525/datagridviewbuttoncolumn-enabling-buttons-binding-boolean-field">this 帖子,但我无法解决我的问题.

解决方案

您可以使用 CellFormatting 事件,例如:

void dataGridView_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e){var grid = (DataGridView)sender;if (grid.Columns[e.ColumnIndex].Name == "IsActive"){e.Value = (bool)e.Value ?"MY_TEXT_FOR_TRUE" : "MY_TEXT_FOR_FALSE";e.FormattingApplied = true;}}

<小时>

编辑(根据评论):

这与您现在所做的非常相似,只需删除绑定列并添加所需类型的新列并正确设置 DataPropertyName 例如:

this.dataGridView1.Columns.Remove("COL_TO_CUSTOMIZE");var btnCol = new DataGridViewDisableButtonColumn();btnCol.Name = "COL_TO_CUSTOMIZE";btnCol.DataPropertyName = "COL_TO_CUSTOMIZE";var col = this.dataGridView1.Columns.Add(btnCol);

请注意,这会将列附加到末尾,但您可以使用 dataGridView.Columns.Insert 方法而不是 Add 来决定列的位置.

I have a datagridview which I fill it as below :

var q= repository.GetStudents();//

dataGridView1.DataSource = null;
dataGridView1.Columns.Clear();

dataGridView1.DataSource = q;

dataGridView1.Columns.RemoveAt(1);
//Remove IsActive 
//Cause I want to have my own implementation 

dataGridView1.Columns[0].DataPropertyName = "StudentID";
dataGridView1.Columns[0].HeaderText = "Studunet ID";

dataGridView1.Columns[1].DataPropertyName = "IsActive";
dataGridView1.Columns[1].HeaderText = "Status";

The "IsActive" property is of boolean Type. When the "IsActive" cell is being displayed, it show true/false. I want to replace it with my own custom value.

I read this and this posts but I could not resolve my problem.

解决方案

You can use the CellFormatting event of the DataGridView, e.g.:

void dataGridView_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
{
    var grid = (DataGridView)sender;
    if (grid.Columns[e.ColumnIndex].Name == "IsActive")
    {
        e.Value = (bool)e.Value ? "MY_TEXT_FOR_TRUE" : "MY_TEXT_FOR_FALSE";
        e.FormattingApplied = true;
    }
}


EDIT (as per comment):

It's very similar to what you're doing now, just remove the bound column and add a new column of the desired type and set the DataPropertyName properly e.g. :

this.dataGridView1.Columns.Remove("COL_TO_CUSTOMIZE");
var btnCol = new DataGridViewDisableButtonColumn();
btnCol.Name = "COL_TO_CUSTOMIZE";
btnCol.DataPropertyName = "COL_TO_CUSTOMIZE";
var col = this.dataGridView1.Columns.Add(btnCol);

Note that this append the column at the end, but you can decide the position of the column by using dataGridView.Columns.Insert method instead of Add.

这篇关于替换 datagridview 列中的 true/false的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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