如何使用数据库值动态设置表格单元格的背景颜色?

发布于 2024-09-11 15:15:50 字数 889 浏览 10 评论 0原文

作为我目前正在进行的项目的一个奖励,如果我可以根据各个表格单元格的值来更改其背景颜色,人们会喜欢它。因此,在我的 RadGrid_ItemDataBound 事件处理程序中,如果单元格的文本等于数据库中数据集中的某个值,我将尝试设置单元格的 BackColor。我当前的代码是这样的:

            For i As Integer = 0 To ds2.Tables(0).Rows.Count - 1 Step 1
                If tc.Text = ds2.Tables(0).Rows(i)("LookupValue") Then
                    tc.BackColor = ds2.Tables(0).Rows(i)("Ref1")
                    Exit For
                End If
            Next

问题是在代码隐藏中设置颜色时,我显然必须将其设置为 System.Drawing.Color 的对象 - 我不能像我一样只给它提供一个字符串值在CSS中。因此,在我上面发布的代码片段中,我的代码在 tc.BackColor = ds2.Tables(0).Rows(i)("Ref1") 上引发运行时异常。我还发现无法使用 CType 将字符串值更改为 System.Drawing.Color 的等效对象。

我想到了一个我可以使用的解决方案。我可以创建一个具有 Name 属性和 System.Drawing.Color 属性的自定义对象。我可以根据数据库中可用的所有颜色值实例化多个对象,然后将单元格文本与对象的名称属性进行比较,但是我担心这样做会相当耗费资源(并且该应用程序的性能已经受到影响,因为它必须在 IE7 下运行)。

那么有人知道我可以用一种相对简单、非资源密集型的方式实现我需要的东西吗?

As a bonus to the project I'm currently working on, the people would like it if I could change the background color of individual table cells depending on what their value is. So in the RadGrid_ItemDataBound event handler I have, I'm trying to set the BackColor of the cell if the cell's text equals a certain value from a dataset in my database. My current code is like so:

            For i As Integer = 0 To ds2.Tables(0).Rows.Count - 1 Step 1
                If tc.Text = ds2.Tables(0).Rows(i)("LookupValue") Then
                    tc.BackColor = ds2.Tables(0).Rows(i)("Ref1")
                    Exit For
                End If
            Next

The problem is when setting a color in the code-behind, I apparently have to set it to an object of System.Drawing.Color -- I can't just feed it a string value like I can in CSS. So in the code snippet I've posted above, my code throws a runtime exception on tc.BackColor = ds2.Tables(0).Rows(i)("Ref1"). I've also discovered that I can't use CType to change a string value into an equivalent object of System.Drawing.Color .

I've thought of a solution that I COULD use. I could create a custom object that has a Name property and a System.Drawing.Color property. I could instantiate several objects according to all color values available in the database and then compare the cell text against the objects' Name properties, however I fear doing this will be rather resource-intensive (and this application's performance is already taking a hit because it has to run under IE7).

So does anybody know of a way I could pull off what I need to here in a relatively simple, non-resource-intensive fashion?

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

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

发布评论

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

评论(1

走走停停 2024-09-18 15:15:50

Color.FromName() 将数据库中的名称传递给它。还有其他方法,例如 FromRGB,但 FromName 听起来像您想要的。

http://msdn.microsoft.com/en-我们/library/system.drawing.color.fromname.aspx

Color.FromName() handing it the name in your database. There are also other methods like FromRGB, but FromName sounds like what you want.

http://msdn.microsoft.com/en-us/library/system.drawing.color.fromname.aspx

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