Vb.net DataGrid中列的HeaderText与设计名称
我有以下代码可以正常工作,但它获取我的列的设计名称(DataGridViewTextBoxColumn11),而不是标题文本。如何更改代码以获取列的标题文本而不是设计名称Vb.net DataGrid中列的HeaderText与设计名称,vb.net,.net-4.0,Vb.net,.net 4.0,我有以下代码可以正常工作,但它获取我的列的设计名称(DataGridViewTextBoxColumn11),而不是标题文本。如何更改代码以获取列的标题文本而不是设计名称 For Each row As DataGridViewRow In grdTransaction.Rows If grdTransaction.Item("DataGridViewTextBoxColumn11", row.Index).Value IsNot Nothing Then
For Each row As DataGridViewRow In grdTransaction.Rows
If grdTransaction.Item("DataGridViewTextBoxColumn11", row.Index).Value IsNot Nothing Then
If grdTransaction("DataGridViewTextBoxColumn11", row.Index).Value.Equals(DBNull.Value) Then
row.DefaultCellStyle.BackColor = Color.Red
End If
End If
Next
我对这类事情的约定是将该项命名为以dgc(DataGridColumn)为前缀的数据库列名。因此,标题为“客户名称”,列名称为“dgcCustomerName”。如果表单或用户控件上有多个datagrid,并且(例如)两个网格中的列中都有CustomerId,则将它们命名为相同会产生冲突。dgcCustomerId是容器命名空间中的一个对象,允许您从中获取属性设置,而无需首先指定网格成员资格。在这种情况下,在第二个网格中,您可能必须调用列“dgcInvoiceCustomerId”或“dgcLineItemCustomerId”。创建一个函数,从提供的列标题文本从DGV返回列索引:
Private Function getColID(ByVal colHeaderText As String) As Integer
Dim result As Integer = -1
For Each col As DataGridViewColumn In grdTransaction.Columns
If col.HeaderText = colHeaderText Then
result = col.Index
Exit For
End If
Next
return result
End Function
然后在代码中:
For Each row As DataGridViewRow In grdTransaction.Rows
If grdTransaction.Item(getColID("header text"), row.Index).Value IsNot Nothing Then
If grdTransaction(getColID("header text"), row.Index).Value.Equals(DBNull.Value) Then
row.DefaultCellStyle.BackColor = Color.Red
End If
End If
Next
或者这一点变化:
For Each row As DataGridViewRow In grdTransaction.Rows
If row.cells(getColID("header text")).Value IsNot Nothing Then
If row.cells(getColID("header text")).Value.Equals(DBNull.Value) Then
row.DefaultCellStyle.BackColor = Color.Red
End If
End If
Next
我认为这很简单
For Each row As DataGridViewRow In grdTransaction.Rows
If Not IsDBNull(row.cells("column_name").Value) Then
row.DefaultCellStyle.BackColor = Color.Red
End If
Next
不,我得到了相同的错误,没有找到该列。我需要把这个:DataGridViewTextBoxColumn11放在你的代码段上。我想要的是能够输入在运行时加载表单时显示的列标题文本。Thanks@nectarines .. 不要将DataGridViewTextBoxColumn11替换为列名称。。将其替换为数据库中的字段名,因为如果您没有更改它,则通常是标题文本。..@nectarines。。你弄明白了吗?