Вопрос

I am stuck with the following problem and I wonder can anyone please help me find a solution. I am retrieving data from a sql server database and displaying the information in a gridview on my asp.net page. However one of the columns returns reams of text, that doesn't appear well on the asp.net page and I would like to trim text from this column only.

Initially the AutoGenerateColumn property of my gridview was set to True, but as I wanted to trim this text, I have looked at manually binding the datacolumns of the gridview, so changed this property to False. I am fairly new to vb.net, so I'm unsure where to go from here, using Template and ItemTemplates.

I have attached my code below:

Asp.Net code:

<asp:GridView ID="grdStoredProcs" CssClass="grdView" runat="server" ForeColor="#333333" GridLines="None" Height="191px" Width="324px" Font-Italic="False" Font-Names="Arial" AutoGenerateColumns="false" >
<Columns>
    <asp:TemplateField HeaderText="ID">
        <ItemTemplate>

        </ItemTemplate>     
    </asp:Templatefield>
</Columns>
<AlternatingRowStyle BackColor="#C6C2C4" BorderColor="Black" BorderStyle="Groove" />
<FooterStyle BackColor="#990000" Font-Bold="True" ForeColor="White" />
<HeaderStyle CssClass="gridViewHeader" BackColor="#990000" Font-Bold="True" ForeColor="White" Font-Size="Medium" />
<PagerStyle BackColor="#FFCC66" ForeColor="#333333" HorizontalAlign="Center" />
<RowStyle BackColor="#A6A1A3" ForeColor="White" /> 
<SelectedRowStyle BackColor="#FFCC66" Font-Bold="True" ForeColor="Navy" />
<SortedAscendingCellStyle BackColor="#FDF5AC" />
<SortedAscendingHeaderStyle BackColor="#4D0000" />
<SortedDescendingCellStyle BackColor="#FCF6C0" />
<SortedDescendingHeaderStyle BackColor="#820000" />
</asp:GridView>

Vb Code:

Private Sub ddlStoredProcedureList_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ddlStoredProcedureList.SelectedIndexChanged
    procLabel.Text = String.Empty
    procLabel.Visible = True
    Dim ds As DataSet
    Dim errors As String = String.Empty

    If Session("proctable") IsNot Nothing Then
        ds = Session("proctable")
    Else
        ds = GetProcsList()
    End If
    Dim resultTable As New DataTable
    If Not ds.Tables(0) Is Nothing Then
        resultTable = ds.Tables(0)
    End If
    For Each row As DataRow In resultTable.Rows
        If row.Item("proc").ToString = ddlStoredProcedureList.SelectedValue Then
            procLabel.Text = row.Item("parametername").ToString
            procLabel.Visible = True
            procTextbox.Visible = True
            ExecuteButton.Visible = True
        ElseIf ddlStoredProcedureList.SelectedValue = "" Then
            procLabel.Visible = False
            procTextbox.Visible = False
            ExecuteButton.Visible = False
            grdStoredProcs.Visible = False
            Exit For
        End If
    Next

End Sub

Protected Sub ExecuteButton_Click(sender As Object, e As EventArgs) Handles ExecuteButton.Click
    Dim ds As DataSet
    Dim colour As String = String.Empty
    Dim systemError As String = String.Empty
    'retrieve the session else retrieve from db
    If Not Session("proctable") Is Nothing Then
        ds = Session("proctable")
    Else
        ds = GetProcsList()
    End If
    Dim resultTable As New DataTable
    If Not ds.Tables(0) Is Nothing Then
        resultTable = ds.Tables(0)
    End If
    Dim name As String = String.Empty
    'now loop through table to get proc name where it equals what the user selected
    For Each row As DataRow In resultTable.Rows
        If row.Item("proc").ToString = ddlStoredProcedureList.SelectedValue Then
            name = row.Item("parametername").ToString
            Exit For
        End If
    Next

    If name.Length > 0 Then
        'got a match
        grdStoredProcs.DataSource = ExecuteProc(name)
        grdStoredProcs.DataBind()

    End If

End Sub

'check for errors in the log trace and highlight red
Protected Sub GridStoredProcs_RowDataBound(sender As Object, e As GridViewRowEventArgs) Handles grdStoredProcs.RowDataBound
    If e.Row.RowType = DataControlRowType.DataRow Then
        If e.Row.Cells(6).Text.Contains("Errors:") Then
            e.Row.BackColor = System.Drawing.Color.Red
        End If
    End If
Это было полезно?

Решение

Here is what I think you are looking for.

Protected Sub GridStoredProcs_RowDataBound(sender As Object, e As GridViewRowEventArgs) Handles grdStoredProcs.RowDataBound
    If e.Row.RowType = DataControlRowType.DataRow Then
        If e.Row.Cells(6).Text.Contains("Errors:") Then
            e.Row.BackColor = System.Drawing.Color.Red
        End If

        Dim x As Integer = WhatColumnDoYouWant
        Dim y As Integer = HowManyCharactersDoYouWant
        If e.Row.Cells(x).Text.Length > y Then
            e.Row.Cells(x).Text = e.Row.Cells(x).Text.SubString(0, y)
        End If
    End If
End Sub

Something else I think you should also do is create an enum for the columns in your grid, then instead of using numbers in code, you can use the enum. It makes more sense six months from now when you are trying to figure out what column 6 is.

Private Enum dgCols
    ID = 1,
    Name = 2,
    SomeDate = 3
End Enum

Then when you pull from the grid you can do this...

e.Row.Cells(dgCols.Name).Text

It is easier to read in the long run, but just a suggestion

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top