0

我有一个带有列的 GRIDVIEW,其中一个包含一个 CSV,其中包含必须出现在每一行上的下拉列表的可能值。

   Private Sub GridViewParameters_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles GridViewParameters.RowDataBound

        If e.Row.RowType = DataControlRowType.DataRow Then
            'Get value of third column. Index is zero based, to 
            'get text of third column we use Cells[2].Text
            Dim CellValue As Integer = Convert.ToSingle(e.Row.Cells(0).Text)
            Dim ntextbox As New TextBox
            Dim ncheckbox As New CheckBox
            Dim ndropdown As New DropDownList
            Dim CellType As String = Convert.ToString(e.Row.Cells(3).Text)
            'MsgBox(CellType)
            Dim DropDownItems(0) As String
            Dim cnt As Integer
            Dim marker As Integer
            Dim arraydim As Integer = 0
            'MsgBox(Convert.ToString(e.Row.Cells(2).Text))
            If Trim(CellType) = "select" Then
                'e.Row.Cells(2).Controls.Remove(ntextbox)
                'e.Row.Cells(2).Controls.Add(ndropdown)
                If Len(Trim(Convert.ToString(e.Row.Cells(4).Text))) > 0 Then
                    For cnt = 1 To Len(Trim(Convert.ToString(e.Row.Cells(4).Text)))
                        If Mid(Trim(Convert.ToString(e.Row.Cells(4).Text)), cnt, 1) = "," Then
                            'e.Row.Cells(2).Controls.
                            'MsgBox("lll")
                        End If
                    Next
                    arraydim = arraydim + 1
                    ReDim Preserve DropDownItems(arraydim)
                    DropDownItems(arraydim) = Mid(Trim(Convert.ToString(e.Row.Cells(4).Text)), marker + 1, cnt - marker - 1)
                End If
                e.Row.Cells(5).Controls.Item(5).Visible = False
                e.Row.Cells(5).Controls.Item(1).Visible = False
                e.Row.Cells(5).Controls.Item(2).Visible = False
                e.Row.Cells(5).Controls.Item(3).Visible = True 'select drop down
                'e.Row.Cells(5).Controls.Item(3).Controls.
            End If
            If Trim(CellType) = "textbox" Then
                'e.Row.Cells(2).Controls.Add(ntextbox)
                'e.Row.Cells(2).Text = Convert.ToString(e.Row.Cells(3).Text)
                e.Row.Cells(5).Controls.Item(5).Visible = True
                e.Row.Cells(5).Controls.Item(1).Visible = False
                e.Row.Cells(5).Controls.Item(2).Visible = False ' textbox
                e.Row.Cells(5).Controls.Item(3).Visible = False
                'e.Row.DataItem(
            End If
            If Trim(CellType) = "boolean" Then
                'e.Row.Cells(2).Controls.Add(ndropdown)
                'e.Row.Cells(2).Text = Convert.ToString(e.Row.Cells(3).Text)
                e.Row.Cells(5).Controls.Item(5).Visible = False
                e.Row.Cells(5).Controls.Item(1).Visible = True 'checkbox
                e.Row.Cells(5).Controls.Item(2).Visible = False
                e.Row.Cells(5).Controls.Item(3).Visible = False
            End If
            If Trim(CellType) = "hidden" Then
                e.Row.Visible = False
            End If
            'e.Row.Cells(2).Controls.Remove(ntextbox)

            '' If value is greater of 10, change format
            If CellValue > 0 Then
                ' Use this syntax to change format of complete row
                e.Row.BackColor = System.Drawing.Color.LightGreen
                e.Row.Cells(0).ForeColor = System.Drawing.Color.LightGreen
                e.Row.Cells(3).ForeColor = System.Drawing.Color.LightGreen
                'e.Row.Cells(5).ForeColor = System.Drawing.Color.LightGreen
            Else
                e.Row.BackColor = System.Drawing.Color.LightPink
                e.Row.Cells(0).ForeColor = System.Drawing.Color.LightPink
                e.Row.Cells(3).ForeColor = System.Drawing.Color.LightPink
                'e.Row.Cells(5).ForeColor = System.Drawing.Color.LightPink

                ' Use this syntax to change format of single cell
                'e.Row.Cells(2).BackColor = System.Drawing.Color.Red
            End If
        End If
    End Sub

根据控件的类型是下拉列表、文本框还是复选框(在表格中限定),它会隐藏或显示相关的控件类型。如果它是下拉列表,则需要从相应列中的 CSV 获取其值。如果传递了相关行的 id,我有一个 SP 以表格形式返回这些。

我如何从 gridview 中获取它以传递给下拉列表并正确加载它?

谢谢

4

1 回答 1

0

因此,如果我没看错,您已经将 CSV 值读入 DropDownItems 数组,但是您看不到如何获取该数组并从中填充 DropDownList,对吗?

好吧,一个 DropDownList 可以使用一个数组作为它的' DataSource,所以我们可以设置属性并调用DataBind方法,我们就完成了:

....
e.Row.Cells(5).Controls.Item(3).Visible = True

ndropdown.DataSource = DropDownItems
ndropdown.DataBind()

顺便说一句,您可以使用 String.Split 方法为您完成工作,而不是自己解析 CSV 值以去除逗号:

'We can replace all this
For cnt = 1 To Len(Trim(Convert.ToString(e.Row.Cells(4).Text)))
    If Mid(Trim(Convert.ToString(e.Row.Cells(4).Text)), cnt, 1) = "," Then
    End If
Next
arraydim = arraydim + 1
ReDim Preserve DropDownItems(arraydim)
DropDownItems(arraydim) = Mid(Trim(Convert.ToString(e.Row.Cells(4).Text)), marker + 1, cnt - marker - 1)

'with
DropDownItems = e.Row.Cells(4).Text.Split(",".ToCharArray())
于 2012-05-03T13:39:09.410 回答