首页 > 解决方案 > vb.net动态设置gridview列的属性

问题描述

你好 StackOverFlow 家族

我的数据库中有一个表,其中包含已在 vb.net 应用程序表单上创建的所有网格视图的所有列,如下所示。

使用此表,我想控制我的应用程序中的每个 gridview,而不是手动在 vb.net 中添加列的属性,这意味着每次我想修改任何属性时,我都必须在应用程序级别进行并创建新的 exe 文件。

桌子

我的程序从上表(从数据库)中检索数据

我需要根据存储在此表中的数据动态修改列属性

提前致谢

标签: c#sql.netvb.net

解决方案


我可以给你一些例子。如果要更改整列的属性,可以这样做:

    DataGridView1.Columns(0).DefaultCellStyle.BackColor = Color.Green
    DataGridView1.Columns(0).DefaultCellStyle.ForeColor = Color.Yellow

如果要更改具有“TextToSearch”值的单元格的属性,可以这样做:

Dim x,y As Integer
For y = 0 to DataGridView1.Columns.Count - 1 
For x = 0 To DataGridView1.Rows.Count - 1
    If DataGridView1.Rows(y).Cells(x).Value.ToString = "TextToSearch" Then
        DataGridView1.Rows(y).Cells(x).Style.BackColor = Color.Red
        DataGridView1.Rows(y).Cells(x).Style.ForeColor = Color.White
    End If
Next
Next

推荐阅读