时间:2016-08-09 11:11:58

标签: asp.net vb.net datagridview

我看过了,从我所知道的情况来看,这是一个你可以吃蛋糕而且也吃它的情况,但我想我会问,如果有人知道的话我可以使用的解决方法。

我以编程方式构建DataGridView控件,基本上有两列,字段名称和值,源数据来自我们的CRM系统,它以复杂的方式存储,因此被认为是最佳格式检索。

我遇到的问题是,当它们从数据库中提取时,它不会呈现HTML标记,与电子邮件地址相同,我手动将它们转换为hrefs,因此只需在页面上单击即可打开一封新电子邮件。

使用下面的示例,存储在数据库中的是什么是http://地址,我已将其变为html标记,否则它只是将URL显示为纯文本。

result.value = "<a href= """ & result.value & ">" & result.value & "</a>"

Display of data - added href tags

从我读过的内容中,将htmlEncode添加到该值会起作用,但它没有。

result.value = HttpUtility.HtmlEncode(result.value)

Display of data - added htmlencode

有没有人对如何使此列显示字符串和超链接有任何想法?

我正在使用的完整方法是:

Private Sub getData()
    'DropDownList1.SelectedIndex = 0
    If DropDownList1.SelectedValue Is "" Then
        ' assume firstrun

    Else
        'assume not firstrun
        firstrun = False
        thisClientID = dbconn.getClientID(DropDownList1.SelectedValue)
        Dim results As List(Of DataDump) = dbconn.GetAllData(thisClientID)

        Label1.Text = "(CRM ID: " & thisClientID & ")"

        alldc = New DataColumn()
        'dc.DataType = System.Type.GetType("system.string")
        'alldc.ColumnName = "Tab"
        'alldc.ReadOnly = "True"
        'AllDataTable.Columns.Add(alldc)

        alldc = New DataColumn()
        ' dc.DataType = System.Type.GetType("system.string")
        alldc.ColumnName = "Field"
        alldc.ReadOnly = "True"


        AllDataTable.Columns.Add(alldc)



        alldc = New DataColumn()
        'alldc.DataType = System.Type.GetType("system.hyperlink")
        alldc.ColumnName = "Value"
        alldc.ReadOnly = "True"
        AllDataTable.Columns.Add(alldc)

        CompanyDetailsDataTable = AllDataTable.Clone
        ConnectionDetailsDataTable = AllDataTable.Clone
        EnvironmentalConfigurationDataTable = AllDataTable.Clone
        ModulesInstalledDataTable = AllDataTable.Clone
        OnlineConfigurationDataTable = AllDataTable.Clone
        PensionDetailsDataTable = AllDataTable.Clone
        PeopleDataTable = AllDataTable.Clone
        SupportDetailsDataTable = AllDataTable.Clone
        SystemConfigurationDataTable = AllDataTable.Clone
        UATSystemDataTable = AllDataTable.Clone

        ' Instantiate the DataSet variable.
        Dim AllDetailsDataSet As New DataSet()
        Dim CompanyDetailsDataset As New DataSet()
        Dim ConnectionDetailsDataset As New DataSet()
        Dim EnvironmentalConfigurationDataset As New DataSet()
        Dim ModulesInstalledDataset As New DataSet()
        Dim OnlineConfigurationDataset As New DataSet()
        Dim PensionDetailsDataset As New DataSet()
        Dim PeopleDataset As New DataSet()
        Dim SupportDetailsDataset As New DataSet()
        Dim SystemConfigurationDataset As New DataSet()
        Dim UATSystemDataset As New DataSet()

        ' Add the new DataTable to the DataSet.
        AllDetailsDataSet.Tables.Add(AllDataTable)
        CompanyDetailsDataset.Tables.Add(CompanyDetailsDataTable)
        ConnectionDetailsDataset.Tables.Add(ConnectionDetailsDataTable)
        EnvironmentalConfigurationDataset.Tables.Add(EnvironmentalConfigurationDataTable)
        ModulesInstalledDataset.Tables.Add(ModulesInstalledDataTable)
        OnlineConfigurationDataset.Tables.Add(OnlineConfigurationDataTable)
        PensionDetailsDataset.Tables.Add(PensionDetailsDataTable)
        PeopleDataset.Tables.Add(PeopleDataTable)
        SupportDetailsDataset.Tables.Add(SupportDetailsDataTable)
        SystemConfigurationDataset.Tables.Add(SystemConfigurationDataTable)
        UATSystemDataset.Tables.Add(UATSystemDataTable)

        For Each result As DataDump In results
            alldr = AllDataTable.NewRow()
            Select Case True
                Case result.value.StartsWith("http")
                    'Hyperlinks

                    result.value = "<a href= """ & result.value & ">" & result.value & "</a>"
                    'result.value = HttpUtility.HtmlEncode(result.value)
                ' ****** need to make hyperlinks work
                Case result.value.Contains("\")
                    'Connection Strings
                    Do While result.value.Contains("\\")
                        result.value = Replace(result.value, "\\", "\")
                    Loop
                Case result.value.Contains("@") And result.value.Contains(".")
                    'Email Addresses - set as hyper link
                    result.value = "<a href= ""mailto:" & result.value & ">" & result.value & "</a>"
                Case Else
            End Select

            'alldr("Tab") = result.tabname
            alldr("Field") = result.fieldname
            alldr("Value") = result.value
            Select Case result.tabname
                Case "Company Details"
                    CompanyDetailsDataTable.Rows.Add(result.fieldname, result.value)

                Case "Connection Details"
                    ConnectionDetailsDataTable.Rows.Add(result.fieldname, result.value)

                Case "Environmental Configuration"
                    EnvironmentalConfigurationDataTable.Rows.Add(result.fieldname, result.value)

                Case "Modules Installed (Live)"
                    ModulesInstalledDataTable.Rows.Add(result.fieldname, result.value)

                Case "Online Configuration"
                    OnlineConfigurationDataTable.Rows.Add(result.fieldname, result.value)

                Case "Pension Details"
                    PensionDetailsDataTable.Rows.Add(result.fieldname, result.value)

                Case "People"
                    PeopleDataTable.Rows.Add(result.fieldname, result.value)

                Case "Support Details"
                    SupportDetailsDataTable.Rows.Add(result.fieldname, result.value)

                Case "System Configuration"
                    SystemConfigurationDataTable.Rows.Add(result.fieldname, result.value)

                Case "UAT System"
                    UATSystemDataTable.Rows.Add(result.fieldname, result.value)

                Case Else

            End Select
            'AllDataTable.Rows.Add(result.tabname, result.fieldname, result.value)
        Next
        CompDetailsGV.DataSource = CompanyDetailsDataset
        ConnDetailsGV.DataSource = ConnectionDetailsDataset
        EnvironmentalDetailsGV.DataSource = EnvironmentalConfigurationDataset
        ModulesDetailsGV.DataSource = ModulesInstalledDataset
        OnlineDetailsGV.DataSource = OnlineConfigurationDataset
        PensionDetailsGV.DataSource = PensionDetailsDataset
        peopleDetailsGV.DataSource = PeopleDataset
        SupportDetailsGV.DataSource = SupportDetailsDataset
        SystemDetailsGV.DataSource = SystemConfigurationDataset
        UATDetailsGV.DataSource = UATSystemDataset


        CompDetailsGV.DataBind()
        ConnDetailsGV.DataBind()
        EnvironmentalDetailsGV.DataBind()
        ModulesDetailsGV.DataBind()
        OnlineDetailsGV.DataBind()
        PensionDetailsGV.DataBind()
        peopleDetailsGV.DataBind()
        SupportDetailsGV.DataBind()
        SystemDetailsGV.DataBind()
        UATDetailsGV.DataBind()
    End If
End Sub

1 个答案:

答案 0 :(得分:0)

您可以将列设置为DataGridViewLinkColumn,以便

alldc = New DataColumn()
必须将

替换为

alldc = New DataGridViewLinkColumn()

并将列设置为true

alldc.UseColumnTextForLinkValue = True

你有没试过这个?