【问题标题】:Binding to DataGridView - Is there a way to "bind" the background color of a cell?绑定到 DataGridView - 有没有办法“绑定”单元格的背景颜色?
【发布时间】:2010-09-25 05:36:16
【问题描述】:

我将 List 绑定到 DataGridView。 SomeObject 类的一个属性是状态码(例如 Red、Yellow、Green)。我可以轻松地将状态“绑定”到单元格的背景颜色吗?也绑定到工具提示怎么样?

【问题讨论】:

  • 我真的希望你能得到一个答案,这是可能的,我一直无法弄清楚,我通过改变每个单元格样式来设置数据绑定后的背景颜色。不幸的是,这真的很慢:( datagridview1.Rows[1].Cells[2].Style.BackColor = Color.Red

标签: .net winforms data-binding datagridview


【解决方案1】:

您可以为 DataGridView 的 CellFormatting 事件编写一个处理程序来自定义背景颜色。这是一个工作示例(您需要将 DataGridView 拖到默认窗体上,然后双击 CellFormatting 事件以创建处理程序):

using System.Drawing;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        private BindingSource _source = new BindingSource();

        public Form1()
        {
            InitializeComponent();

            _source.Add(new MyData(Status.Amber, "Item A"));
            _source.Add(new MyData(Status.Red, "Item B"));
            _source.Add(new MyData(Status.Green, "Item C"));
            _source.Add(new MyData(Status.Green, "Item D"));

            dataGridView1.DataSource = _source;
            dataGridView1.Columns[0].Visible = false;
        }

        private void dataGridView1_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
        {
            if (e.ColumnIndex == 1)
            {
                DataGridView dgv = sender as DataGridView;
                MyData data = dgv.Rows[e.RowIndex].DataBoundItem as MyData;

                switch (data.Status)
                {
                    case Status.Green:
                        e.CellStyle.BackColor = Color.Green;
                        break;
                    case Status.Amber:
                        e.CellStyle.BackColor = Color.Orange;
                        break;
                    case Status.Red:
                        e.CellStyle.BackColor = Color.Red;
                        break;
                }
            }
        }
    }

    public class MyData
    {
        public Status Status { get; set; }
        public string Text { get; set; }

        public MyData(Status status, string text)
        {
            Status = status;
            Text = text;
        }
    }

    public enum Status
    {
        Green,
        Amber,
        Red
    }
}

为了简单起见,这里的对象只有一个状态和文本。我为这些对象的示例集创建了一个 BindingSource,然后将其用作 DataGridView 的数据源。默认情况下,网格会在您绑定时自动生成列,因此无需手动执行此操作。我还隐藏了绑定到状态值的第一列,因为我们要为文本单元格着色。

为了实际进行绘画,我们响应 CellFormatting 事件。我们通过转换 sender 来获得对 DataGridView 的引用,然后使用 DataGridViewCellFormattingEventArgs 对象的 RowIndex 属性来获取数据项本身(每个 Row 都有一个 DataBoundItem 属性,可以方便地为我们提供这个)。由于 DataBoundItem 是一个对象类型,我们需要将它转换为我们的特定类型,然后我们才能真正获得 Status 属性本身......唷!

我没有任何工具提示编程经验,但我认为您应该响应 MouseHover 事件,然后努力发现从哪一行开始。

我希望这会有所帮助。

【讨论】:

  • +1,很好的答案。我发现最后一行是空行,所以必须检查 MyData 是否为空。除此之外,太棒了!
  • 这很好用,唯一的问题是是否有办法一次完成这一行而不是一个单元格。只是想知道当您有 1000 多行、10 多列时是否会影响性能。
  • @Sint - 格式仅适用于屏幕项目,因此不应该存在性能问题。如果要将样式应用于整行,可以向 RowPostPaint 事件添加处理程序:msdn.microsoft.com/en-us/library/…
【解决方案2】:

开箱即用,任何 DataGridViewColumn 只能绑定到 DataSource 中对象的一个​​属性,属性名称由每个 DataGridViewColumn 的 DataPropertyName 给出(您将拥有特定的列类型,例如:DataGridViewTextBoxColumn、. ..)。

您可以使用 DataGridView.CellFormatting 事件来根据数据绑定项更改单元格的样式。在这个事件的 DataGridViewCellFormattingEventArgs 中获取行索引,从那里可以获取当前对象(行的源)。从那里,您可以使用对象的任何属性来影响您的单元格。

一个好的起点(类似的想法):here

第二个想法是开发您自己的 DataGridViewColumn 类型并为您需要绑定的其他内容添加属性。例如,与它具有内置 DataPropertyName 的方式相同,您可以添加自己的:BackgroundColorPropertyName。可以在here 找到构建自定义 DataGridViewColumns 的起点。

【讨论】:

    猜你喜欢
    • 2014-10-08
    • 1970-01-01
    • 2013-04-12
    • 2014-02-27
    • 1970-01-01
    • 1970-01-01
    • 2016-05-27
    • 2020-05-15
    • 1970-01-01
    相关资源
    最近更新 更多