哪位高手能不能把下边这段用vb写的代码改成c#,特别是函数头那段。多谢!! Private Sub DataGrid1_ItemDataBound(ByVal sender As Object, _ ByVal e As System.Web.UI.WebControls.DataGridItemEventArgs) _ Handles DataGrid1.ItemDataBound If e.Item.ItemType = ListItemType.EditItem Then Dim DRV As DataRowView = CType(e.Item.DataItem, DataRowView) Dim CurrentShip As String = DRV("ShipVia") Dim DDL As DropDownList = CType(e.Item.Cells(4).Controls(1), DropDownList) Dim SQL As String = "SELECT ShipperID, CompanyName FROM Shippers ORDER BY ShipperID" Dim DA As SqlDataAdapter = New SqlDataAdapter(SQL, ConnStr) Dim DS As New DataSet Dim item As ListItem DA.Fill(DS, "Shippers") DDL.DataSource = DS.Tables("Shippers").DefaultView DDL.DataTextField = "CompanyName" DDL.DataValueField = "ShipperID" DDL.DataBind() item = DDL.Items.FindByValue(CurrentShip) If Not item Is Nothing Then item.Selected = True End If End Sub
得到实例 DDL ,用DDL就可以操作了
DropDownList DDL= CType(e.Item.Cells(4).Controls(1), DropDownList);
语法错误
DropDownList DDL= CType(e.Item.Cells(4).Controls(1), DropDownList);
语法错误
DropDownList DDL= (DropDownList)(e.Item.Cells[4].Controls[1], DropDownList);
后,还是有错误:
“System.Web.UI.WebControls.DropDownList”表示“类”,此处应为“变量”
C#中的正确语法,就像孟子说的那样
Private Sub DataGrid1_ItemDataBound(ByVal sender As Object, _
ByVal e As System.Web.UI.WebControls.DataGridItemEventArgs) _
Handles DataGrid1.ItemDataBound
If e.Item.ItemType = ListItemType.EditItem Then
Dim DRV As DataRowView = CType(e.Item.DataItem, DataRowView)
Dim CurrentShip As String = DRV("ShipVia")
Dim DDL As DropDownList = CType(e.Item.Cells(4).Controls(1), DropDownList)
Dim SQL As String = "SELECT ShipperID, CompanyName FROM Shippers ORDER BY ShipperID"
Dim DA As SqlDataAdapter = New SqlDataAdapter(SQL, ConnStr)
Dim DS As New DataSet
Dim item As ListItem
DA.Fill(DS, "Shippers")
DDL.DataSource = DS.Tables("Shippers").DefaultView
DDL.DataTextField = "CompanyName"
DDL.DataValueField = "ShipperID"
DDL.DataBind()
item = DDL.Items.FindByValue(CurrentShip)
If Not item Is Nothing Then item.Selected = True
End If
End Sub