如何使用其自身的值更改列表视图子项的背景色

发布于 2024-09-16 01:08:34 字数 915 浏览 4 评论 0原文

如何使用其自身的值以编程方式更改列表视图中单个单元格的背景颜色?

alt text

ColorFlag 列中的值来自数据库。

这是我的代码:

foreach(DataRow dr in _dataTbl.Rows) 
        {
            _markOW = dr["Mark"].ToString();
            _stock = dr["Stock"].ToString();
            _SteelSectio = dr["SteelSection"].ToString();
            _colo = (Int32)dr["Color"];


            ListViewItem _lvi = new ListViewItem(_markOW);
            _lvi.SubItems.AddRange(new string[]{_SteelSectio, _stock,     _colo.ToString()});

            _myListView.Items.Add(_lvi);   }

这是我尝试更改单元格背景色的代码:

for (int _i = 0; _i < _owLV.Items.Count; _i++)
            {
                _myListView.Items[_i].UseItemStyleForSubItems = false;
                _myListView.Items[_i].SubItems[3].BackColor = Color.FromArgb(_colo);
            }

提前致谢

How can I programmatically change the back color of a single cell in a listview using its own value?

alt text

The values in the ColorFlag Column Came from the database.

Here is my code:

foreach(DataRow dr in _dataTbl.Rows) 
        {
            _markOW = dr["Mark"].ToString();
            _stock = dr["Stock"].ToString();
            _SteelSectio = dr["SteelSection"].ToString();
            _colo = (Int32)dr["Color"];


            ListViewItem _lvi = new ListViewItem(_markOW);
            _lvi.SubItems.AddRange(new string[]{_SteelSectio, _stock,     _colo.ToString()});

            _myListView.Items.Add(_lvi);   }

Here is the code that I have tried to change the backcolor of the cells:

for (int _i = 0; _i < _owLV.Items.Count; _i++)
            {
                _myListView.Items[_i].UseItemStyleForSubItems = false;
                _myListView.Items[_i].SubItems[3].BackColor = Color.FromArgb(_colo);
            }

Thanks in advance

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

或十年 2024-09-23 01:08:34

据我所知,您的代码看起来不错。我只是组合了一个快速的 Windows 窗体应用程序,并在窗体上添加了一个 ListView,并在详细视图中包含两列。下面的代码工作正常。

var item1 = new ListViewItem( "Item 1");
item1.SubItems.Add( "Color" );
item1.SubItems[1].BackColor = Color.FromArgb( -16711936 );
item1.UseItemStyleForSubItems = false;

listView1.Items.Add( item1 );

我会在添加项目之前尝试设置背景颜色。看起来您还将所有项目设置为相同的颜色,这可能不是您想要的。

As far as I can tell, the code you have looks fine. I just threw together a quick Windows Forms application and tossed a ListView on the form with two columns in detail view. The following code works fine.

var item1 = new ListViewItem( "Item 1");
item1.SubItems.Add( "Color" );
item1.SubItems[1].BackColor = Color.FromArgb( -16711936 );
item1.UseItemStyleForSubItems = false;

listView1.Items.Add( item1 );

I would try setting the BackColor before you add the item. It also looks like you're setting all the items to the same color which is probably not what you want.

偏爱自由 2024-09-23 01:08:34

看一下这些链接:

C# ListView 详细信息,突出显示单个cell

使用 C# 更改列表视图单元格的颜色 (有一个可行的解决方案)

关键点是设置这个:

listView1.Items[0].UseItemStyleForSubItems = false;

这样做:

foreach (DataRow dr in _dataTbl.Rows)
{
    _markOW = dr["Mark"].ToString();
    _stock = dr["Stock"].ToString();
    _SteelSectio = dr["SteelSection"].ToString();
    _color = (Int32)dr["Color"];

    ListViewItem _lvi = new ListViewItem(_markOW);

    _lvi.SubItems.AddRange(new string[] {_SteelSectio, _stock, _color.ToString() });    
    _lvi.UseItemStyleForSubItems = false;
    _lvi.SubItems[2].BackColor = Color.FromArgb(_color);

    _myListView.Items.Add(_lvi);
}

Take a look at these links:

C# ListView Detail, Highlight a single cell

Changing color of list view cell using C# (has a working solution)

The key point is to set this:

listView1.Items[0].UseItemStyleForSubItems = false;

Do this:

foreach (DataRow dr in _dataTbl.Rows)
{
    _markOW = dr["Mark"].ToString();
    _stock = dr["Stock"].ToString();
    _SteelSectio = dr["SteelSection"].ToString();
    _color = (Int32)dr["Color"];

    ListViewItem _lvi = new ListViewItem(_markOW);

    _lvi.SubItems.AddRange(new string[] {_SteelSectio, _stock, _color.ToString() });    
    _lvi.UseItemStyleForSubItems = false;
    _lvi.SubItems[2].BackColor = Color.FromArgb(_color);

    _myListView.Items.Add(_lvi);
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文