2014-03-31 98 views
0

我遇到以下問題,我想知道任何人都可以幫我找到解決方案。 我正在從sql server數據庫中檢索數據並在asp.net頁面的gridview中顯示信息。然而,其中一列會返回大量文本,這在asp.net頁面上顯得不太好,我想只修剪來自該列的文本。如何在GridDiew列中修剪數據,從數據庫中檢索

最初GridView的AutoGenerateColumn屬性被設置爲True,但是因爲我想修剪這個文本,所以我查看了手動綁定gridview的datacolumns,所以將此屬性更改爲False。我對vb.net相當陌生,所以我不確定從這裏走到哪裏,使用Template和ItemTemplates。

我重視我下面的代碼:

Asp.Net代碼:

<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代碼:

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 

回答

0

以下是我認爲你正在尋找。

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 

別的東西,我想你也應該做的是在網格創建列枚舉,然後,而不是在代碼中使用的數字,你可以使用枚舉。從現在起六個月,當你試圖找出第6列是什麼時候,這會更有意義。

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

然後當你可以做到這一點的電網拉...

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

更容易從長遠來看看書,但只是一個建議

+0

由於這是一個偉大的幫幫我。我只是在鑄造時遇到了一些問題,但希望我能夠解決這個問題。感謝幫助:) – user3481605