基于隐藏列的DataGridView行格式 [英] DataGridView row formatting based on hidden column

查看:76
本文介绍了基于隐藏列的DataGridView行格式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有什么方法可以基于用户看不见的列中的某个值来设置行的样式?网格包含两行,如果要删除某些行,我希望将某些行显示为红色。我有一个隐藏的列,如果删除了列,则存储true,否则存储false。我已经尝试过 CellFormatting ,但是由于我的列不可见,因此 e.ColumnIndex 永远都没有正确的隐藏值柱。

Is there any way to set the style of a row based on some value from a column that is not visible to the user? The grid contains a couple of rows and I want some rows to be colored in red if they were deleted. I have a hidden column that stores true if the columns were deleted, false otherwise. I've tried CellFormatting but since my column is not visible, e.ColumnIndex never has the correct value for my hidden column.

任何帮助将不胜感激。

编辑:

下面是我要完成的任务的图像。您可以看到第二行具有红色文本,这是由于用户无法在数据网格中看到的列中的值所致。当用户也第一次(在加载时)看到表单时,该网格应该像这样着色。

Below is an image of what I am trying to accomplish. You can see that the second row has the text red which is due to the values in a column that the user cannot see in the datagrid. This grid should be colored like this when the user see the form for the first time too (on load).

推荐答案

代替 CellFormatting ,请尝试 CellValueChanged 用于未绑定的数据,或者 DataBindingComplete 用于绑定的数据集。例如,假设您正在使用以下 Button.Click 事件删除/取消删除行:

Instead of CellFormatting, try CellValueChanged for unbound data or DataBindingComplete for a bound data set. For example, let's say that you are "deleting/undeleting" a row using the following Button.Click event:

private void Button1_Click(object sender, EventArgs e)
{
    bool value = (bool)dataGridView1.CurrentRow.Cells["Deleted"].Value;
    dataGridView1.CurrentRow.Cells["Deleted"].Value = !value;

    // For bound data (like a DataTable) add the following line:
    // ((DataTable)dataGridView1.DataSource).AcceptChanges();
}

未绑定数据

以这种方式更改行已删除列的值将触发以下事件处理程序。因此,您可以根据列的值 True False

Changing the rows "deleted" column value in this way will trigger the following event handler. Therefore, you can color your row based on that column's value of True or False:

private void DataGridView1_CellValueChanged(object sender, DataGridViewCellEventArgs e)
{
    if (e.ColumnIndex == dataGridView1.Columns["Deleted"].Index)
    {
        dataGridView1.Rows[e.RowIndex].DefaultCellStyle.ForeColor = (bool)dataGridView1[e.ColumnIndex, e.RowIndex].Value ? Color.Red : Color.Black;
    }
}

绑定数据

对于绑定数据(例如来自 DataTable 的数据),处理 DataBindingComplete 事件就足够了。首次设置绑定时以及更改之后(例如, Button1.Click 事件的更改)时,都会触发此事件。在这里,您将循环浏览各行,并根据隐藏列的值设置所需的样式。 (请注意,对于带有 DataTable 源的网格,对 Button1_Click 事件处理程序的其他更改。这是必需的。立即更改样式-否则,除非您导航到另一行,否则更改不会发生。)

For bound data, such as from a DataTable, handling the DataBindingComplete event will be enough. This event will trigger when the binding is first set as well as after changes - such as the changes from the Button1.Click event. Here, you'll loop through the rows and set the desired style according to the hidden column's value. (Note the additional change to the Button1_Click event handler for a grid with a DataTable source. This is needed to give an immediate style change - otherwise it won't happen until you navigate to a different row.)

private void DataGridView1_DataBindingComplete(object sender, DataGridViewBindingCompleteEventArgs e)
{
    foreach (DataGridViewRow row in dataGridView1.Rows)
    {
        row.DefaultSCellStyle.ForeColor = (bool)row.Cells["Deleted"].Value ? Color.Red : Color.Black;
    }
}

这篇关于基于隐藏列的DataGridView行格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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