可以绑定DataGridView中使用文本单元的布尔值

可以绑定DataGridView中使用文本单元的布尔值

本文介绍了可以绑定DataGridView中使用文本单元的布尔值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时删除!!

予有绑定到对象的列表一个DGV。这工作得很好,只是其中一个对象的属性是布尔值,因此为复选框显示,但我会preFER简单的是/否文本字段代替。我认为增加一个额外的列,并与基于布尔值相应的字符串填充但这似乎有点洁癖。有没有更简单的方法?

I have a DGV bound to a list of objects. This works fine except that one of the object properties is boolean and therefore displays as a checkbox but I would prefer a simple yes/no text field instead. I've considered adding an additional column and populating with the appropriate string based on the boolean value but this seems a little over the top. Is there an easier way?

的DGV是只读

推荐答案

正如上面似乎是不可能改变的布尔值的一个数据绑定场景的视觉外观。即使的 DataGridViewCellStyle.FormatProvider 的不与类型,如System.Int32的,System.Int64,System.Decima等正常工作

As mentioned above it seems to be impossible to change the visual appearance of boolean values in a data bound scenario.Even DataGridViewCellStyle.FormatProvider does not work correctly with types like System.Int32, System.Int64, System.Decima, etc.

  • social.msdn.microsoft.com/forums/en-US/winformsdatacontrols/thread/894580c5-ef14-4123-9d8e-1fd6efc3f582/
  • http://connect.microsoft.com/VisualStudio/feedback/ViewFeedback.aspx?FeedbackID=350751
  • social.msdn.microsoft.com/forums/en-US/winformsdatacontrols/thread/894580c5-ef14-4123-9d8e-1fd6efc3f582/

因此​​,我找到了一个解决方法这对我的作品。也许这是不是最好的解决办法,但目前它符合我的需要。我处理的 DataGridView.ColumnAdded 的事件和替换的 DataGridViewCheckBoxColumn 的用的 DataGridViewTextBoxColumn 的。后来我用的 CellFormating 的事件(Microsoft推荐,见上面的链接)格式化源数据。

Therefore I found a workaround which works for me. Probably it is not the best solution but currently it fits my needs.I handle the DataGridView.ColumnAdded event and replace DataGridViewCheckBoxColumn with DataGridViewTextBoxColumn. Afterwards I use CellFormating event (recommended by Microsoft, see links above) to format source data.

private DataGridViewTextBoxColumn textBoxColumn = null;
void _dataGrid_ColumnAdded(object sender, DataGridViewColumnEventArgs e)
{
    // Avoid recursion
    if (e.Column == textBoxColumn) return;

    DataGridView gridView = sender as DataGridView;
    if (gridView == null) return;

    if( e.Column is DataGridViewCheckBoxColumn)
    {
        textBoxColumn = new DataGridViewTextBoxColumn();
        textBoxColumn.Name = e.Column.Name;
        textBoxColumn.HeaderText = e.Column.HeaderText;
        textBoxColumn.DataPropertyName = e.Column.DataPropertyName;

        gridView.Columns.Insert(e.Column.Index, textBoxColumn);
        gridView.Columns.Remove(e.Column);
    }
}

void _dataGrid_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
{
    DataGridViewColumn col = _dataGrid.Columns[e.ColumnIndex];

    try
    {
        if ( col.Name == "IsMale")
        {
            bool isMale = Convert.ToBoolean(e.Value);
            e.Value = isMale ? "male" : "female";
        }
    }
    catch (Exception ex)
    {
        e.Value = "Unknown";
    }
}

这篇关于可以绑定DataGridView中使用文本单元的布尔值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

1403页,肝出来的..

09-06 21:33