2011-04-04 65 views
0

我正在以編程方式創建一個gridview,我想用這個問題的解決方案:Programmatically access GridView columns and manipulate。換句話說,如果行中「已提交」列的值爲1,我想隱藏超鏈接字段,而是顯示一個模板字段。我列出僱傭應用程序,並讓用戶點擊「加載應用程序」,如果他們還沒有提交了一個應用程序,或者如果他們已經提交了應用程序,或者以純文本顯示「提交」我知道如何做到這一點,如果我在aspx文件中創建我的gridview,但我不知道如何做到這一點,當我在運行時創建我的gridview。這裏是我的代碼:以編程方式創建gridview時,如何評估列的值?

Public Sub getSavedApps(ByVal userID As Integer) 
     Dim ds As New DataSet 
     ds = clsData.sqlGetAllApps(userID) 
     If ds.Tables(0).Rows.Count > 0 Then 
      popAppSelect.Show() 

      'let user select which app they want to work on 
      Dim grdAppSelect As New GridView 

      'need to find out if this row has a value of 1 or 0 for submitted 
      Dim submitted As Boolean 

      'position column 
      Dim fieldPosition As New BoundField 
      fieldPosition.DataField = "positionDesired" 
      fieldPosition.HeaderText = "Position Desired" 
      If fieldPosition.DataField.ToString = "" Then 
       fieldPosition.DataFormatString = "<em>No position specified</em>" 
      End If 
      Dim colPosition As DataControlField = fieldPosition 

      'date column 
      Dim fieldDate As New BoundField 
      fieldDate.DataField = "dateStarted" 
      fieldDate.HeaderText = "Date Started" 
      Dim colDate As DataControlField = fieldDate 

      Dim strAppID(0) As String 
      strAppID(0) = "appID" 
      Dim colLoad As DataControlField 

      'submitted column 
      If submitted Then 
       Dim fieldLoad As New TemplateField 
       fieldLoad.ItemTemplate = New GridViewTemplate(DataControlRowType.DataRow, "submitted") 
       colLoad = fieldLoad 
      Else 
       Dim fieldLoad As New HyperLinkField 
       fieldLoad.Text = "<b>Load Application &raquo;</b>" 
       fieldLoad.DataTextFormatString = "{0}" 
       fieldLoad.DataNavigateUrlFields = strAppID 
       fieldLoad.DataNavigateUrlFormatString = "?load={0}" 
       colLoad = fieldLoad 
      End If 

      'add the columns to the gridview 
      With grdAppSelect 
       .ID = "grdAppSelect" 
       .CssClass = "grdAppSelect" 
       .CellPadding = 5 
       .BorderWidth = "0" 
       .HeaderStyle.HorizontalAlign = HorizontalAlign.Left 
       With .Columns 
        .Add(colPosition) 
        .Add(colDate) 
        .Add(colLoad) 
       End With 
       .AutoGenerateColumns = False 
       .DataSource = ds.Tables(0) 
       .DataBind() 
      End With 

      Dim lnkNew As New HyperLink 
      lnkNew.Text = "<b>Start New Application &raquo;</b>" 
      lnkNew.NavigateUrl = "?load=" 

      Dim strClear As New LiteralControl 
      strClear.Text = "<br class='clearer' />" 

      'add the controls to the panel 
      pnlAppSelect.Controls.Add(grdAppSelect) 
      pnlAppSelect.Controls.Add(strClear) 
      'pnlAppSelect.Controls.Add(lnkNew) 

     Else 
      'should be apps there but couldn't find them 
      lblGeneralError.Text = "Could not find any previously started applications." 
     End If 
    End Sub 

回答

0

我找到了解決方案。當我創建gridview時,我需要添加一個事件處理程序,以便我可以訪問RowDataBound事件並在那裏進行評估。下面是我使用的事件處理代碼:

AddHandler grdAppSelect.RowDataBound, AddressOf grdAppSelect_RowDataBound 

然後,我只是做的RowDataBound期間對我的評價,像這樣:

Sub grdAppSelect_RowDataBound(ByVal sender As Object, ByVal e As GridViewRowEventArgs) 

    If e.Row.RowType = DataControlRowType.DataRow Then 

     Dim load As Control = e.Row.Controls(2) 
     Dim submitted As Control = e.Row.Controls(3) 
     Dim submittedText As String = e.Row.Cells(3).Text 
     If submittedText = "True" Then 
      load.Visible = False 
     End If 
     submitted.Visible = False 

    End If 

End Sub 
相關問題