【问题标题】:format cell of datagridview to show substring of column text in vb.net格式化 datagridview 的单元格以在 vb.net 中显示列文本的子字符串
【发布时间】:2009-08-25 17:43:00
【问题描述】:

我的数据库中有一个列项目代码,我已绑定到数据网格视图。项目代码采用这种格式“A-B-C”,我只希望显示代码的“B”部分,我已将此列绑定到 gridview,现在希望让它显示子字符串。我尝试了 defaultcellstyle.format 但不知道如何为其获取子字符串。

【问题讨论】:

    标签: .net vb.net winforms datagridview cell-formatting


    【解决方案1】:

    是否可以向绑定对象添加新属性,例如 ItemCodePart,它返回项目代码的中间部分,然后将此属性绑定到列而不是项目代码?那将是最简单的方法。

    另一种选择是处理 DataGridView 的 CellFormatting 事件并将 e.Value 设置为您要显示的项目代码部分:

    Private Sub myDataGridView_CellFormatting(ByVal sender As Object, ByVal e As DataGridViewCellFormattingEventArgs) Handles myDataGridView.CellFormatting
    
    If e.ColumnIndex = MyItemPartColumn.Index Then
        Dim currentValue As String = CStr(myDataGridView.Item(e.ColumnIndex, e.RowIndex).Value)
        Dim parts As String() = currentValue.Split(New Char() {"-"c})
        e.Value = parts(1)
    End If
    
    End Sub
    

    【讨论】:

    • 对象是一个数据表,你能告诉我如何给它添加属性吗?
    • DataColumn 是列的类,您可以通过 datatable.columns.add 方法添加。然后在您的行上,您将照常访问新列并将其值设置为您的项目的格式化版本。
    • 使用数据表可能第二个选项更简单;-)
    【解决方案2】:

    RowDataBound 事件 - 您可以编辑该字段的文本。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-03-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多