【问题标题】:Row color based on changing ID from MySQL database基于从 MySQL 数据库更改 ID 的行颜色
【发布时间】:2021-04-16 21:26:37
【问题描述】:

我正在尝试根据 ID 更改更改我的 datagridrows 的颜色。 我已经将数据绑定到datagridview了。

我认为最简单的方法是检查第 1 列中的 ID 是奇数还是偶数:

甚至行背景 = 白色 奇数行背景 = 棕色

这个想法是为了获得更好的概览,并根据背景颜色收集一些物品。

我对 WPF 很陌生: 目前我只包含如下数据网格:

<DataGrid x:Name="cusDetailGrid" Grid.Column="1" Grid.Row="2" ItemsSource="{Binding}"/>

这将由 MySqlDB 的选择字符串填充。

任何想法如何使它工作? 提前谢谢!

【问题讨论】:

  • 我已经尝试过了,但这不是我想要的,或者我不知道如何使用它。假设我的表如下所示:1 XY(白色)1 XY(白色)1 XY(白色)2 ZB(棕色)2 ZB(棕色)3 Bla(白色)3 Bla(白色)3 Bla(白色

标签: c# wpf datagrid datagridviewcolumn


【解决方案1】:

Here 看起来是个不错的答案。

您可能能够实现此目的的另一种方法是创建 LoadingRow 事件;您可以尝试这样的操作,然后调整到 ID 列所在的位置:

private void Dg_LoadingRow(object sender, DataGridRowEventArgs e)
        {
            try
            {
                if (Convert.ToInt32(((System.Data.DataRowView)(e.Row.DataContext)).Row.ItemArray[0]) % 2 != 0)
                {
                    e.Row.Background = new SolidColorBrush(Colors.Brown);
                }
                else
                {
                    e.Row.Background = new SolidColorBrush(Colors.White);
                }
            }
            catch
            {

            }
        }

【讨论】:

    猜你喜欢
    • 2020-11-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-03-27
    • 2020-04-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多