我有一个带有列的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获取其值。我有一个SP,如果传递相关行的id,则以表格形式返回这些。
如何从gridview传递到下拉列表并正确加载?
由于
答案 0 :(得分:0)
所以,如果我读得正确,你已经将Dropval的数值读入了DropDownItems数组,但是你看不到如何获取该数组并从中填充DropDownList,对吗?
好吧,DropDownList可以使用数组作为其“DataSource
,因此我们可以设置属性并调用DataBind
方法,我们就完成了:
....
e.Row.Cells(5).Controls.Item(3).Visible = True
ndropdown.DataSource = DropDownItems
ndropdown.DataBind()
BTW,您可以使用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())