2012-11-23 39 views
0

我想用包含使用asp.net的dropdownlist的列填充gridview。對於grid.rows中的每個gridrow作爲gridviewrow不處理最後一行

到目前爲止,我能夠使用SQL表中的所有選項填充下拉列表,並且我還能夠保存和讀取SQL表中選定的值,但最後一行似乎忽略它。

繼承人我的代碼:

Protected Sub gridGenerators_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles gridGenerators.RowDataBound 

    With e.Row 
     If e.Row.RowType = DataControlRowType.DataRow Then 

      'Populate all dropdown lists with options from SQL' 
      Dim ddl = TryCast(.Cells(0).FindControl("ddlOnCallGroup"), DropDownList) 

      ddl.DataSource = db.getSMSgroups() 
      ddl.DataTextField = "SMS_Group" 
      ddl.DataValueField = "id" 
      ddl.DataBind() 

      'And add a default when value is NULL' 
      Dim l1 As New ListItem 

      l1.Text = "-select-" 
      l1.Value = "" 

      ddl.Items.Add(l1) 

      'Get the current setting from SQL (that user has permissions for)' 
      Dim generators As String = Membership.GetUser().Comment 
      Dim genDT As DataTable = db.locations(generators.Split(",".ToCharArray)) 

      'Iterate through settings and select value for each dropdownlist' 
      For Each grdrow As GridViewRow In gridGenerators.Rows 

       Dim drdList = TryCast(gridGenerators.Rows(grdrow.RowIndex).Cells(0).FindControl("ddlOnCallGroup"), DropDownList) 

       drdList.SelectedValue = genDT.Rows(grdrow.RowIndex).Item("ID_SMS_Group").ToString 

      Next 

     End If 
    End With 

End Sub 

在此先感謝!

+2

爲什麼你循環RowDataBound中的所有GridViewRows?無論如何每個** GridViewRow被調用? –

+0

因爲...... Im a蠢貨!問題解決了,謝謝! – neilrudds

回答

4

爲什麼你循環所有GridViewRowsRowDataBound這是每GridViewRow要麼?

所以不是這個循環:

For Each grdrow As GridViewRow In gridGenerators.Rows 

    Dim drdList = TryCast(gridGenerators.Rows(grdrow.RowIndex).Cells(0).FindControl("ddlOnCallGroup"), DropDownList) 
    drdList.SelectedValue = genDT.Rows(grdrow.RowIndex).Item("ID_SMS_Group").ToString 

Next 

只是這樣的:

Dim drdList = DirectCast(e.Row.FindControl("ddlOnCallGroup"), DropDownList) 
drdList.SelectedValue = DirectCast(e.Row.DataItem, DataRowView).Item("ID_SMS_Group").ToString 

請注意,我也改變了一些其他的事情(如訪問DataItem或使用FindControle.Row)

+0

傳奇!!感謝你... – neilrudds

相關問題