这段代码正在使用C#代码,但我无法在VB.net上运行。 [英] This code is working on C# code but I can't run on VB.net.
本文介绍了这段代码正在使用C#代码,但我无法在VB.net上运行。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
这是错误:
无法将'System.Web.UI.WebControls.TemplateField'类型的对象强制转换为输入'System.Web.UI.WebControls.BoundField'.System Error。
受保护的 Sub GriView1_RowCreated( ByVal sender As 对象, ByVal e 作为 GridViewRowEventArgs)句柄 GridView1.RowCreated
对于 每个单元格作为 TableCell 在 e.Row.Cells
如果 不 字符串 .IsNullOrEmpty( cell.Text) AndAlso (cell.Text<> )然后
Dim 字段 As BoundField = CType (< span class =code-keyword> CType (cell,DataControlFieldCell).ContainingField,BoundField)
If (field.DataField = 名称)然后
字段。 ReadOnly = True
结束 如果
结束 如果
下一步
结束 Sub < /跨度>
<二v class =h2_lin>解决方案
请参阅我对该问题的评论。如果正常工作,它就不可能是这个代码。您始终可以在C#和VB.NET之间自动转换。请参阅我以前的回答:代码解释,C#到VB .NET [ ^ ]。
-SA
错误信息非常清楚 - 你的网格中有一个< asp:TemplateField>
,它不是< asp:BoundField>
,所以你试图这样做会失败。
等效代码也在C#中失败。
要使它在C#中运行,你可以使用为
运算符 [ ^ ]并测试null
。在VB.NET中,TryCast
运算符 [ ^ ]执行类似的功能:
受保护的 Sub GriView1_RowCreated( ByVal 发件人作为 对象, ByVal e As GridViewRowEventArgs)句柄 GridView1.RowCreated
对于 每个单元格作为 TableCell < span class =code-keyword>在 e.Row.Cells
' 不需要比较空字符串;
' 这已经由IsNullOrEmpty调用处理。
如果 不 字符串 .IsNullOrEmpty(cell.Text)然后
Dim fieldCell As DataControlFieldCell = TryCast (cell,DataControlFieldCell)
如果 fieldCell < span class =code-keyword> IsNot Nothing 然后
Dim 字段 As BoundField = TryCast (fieldCell.ContainingField ,BoundField)
如果字段 IsNot Nothing AndAlso field.DataField = 名称 然后
字段。< span class =code-keyword> ReadOnly = True
结束 < span class =code-keyword>如果
结束 如果
结束 如果
下一步
结束 Sub
This is the error:
Unable to cast object of type 'System.Web.UI.WebControls.TemplateField' to type 'System.Web.UI.WebControls.BoundField'.System Error.
Protected Sub GriView1_RowCreated(ByVal sender As Object, ByVal e As GridViewRowEventArgs) Handles GridView1.RowCreated
For Each cell As TableCell In e.Row.Cells
If Not String.IsNullOrEmpty(cell.Text) AndAlso (cell.Text <> "") Then
Dim field As BoundField = CType(CType(cell, DataControlFieldCell).ContainingField, BoundField)
If (field.DataField = "Name") Then
field.ReadOnly = True
End If
End If
Next
End Sub
解决方案
Please see my comment to the question. It could not be "this code" it if was "working". You can always automatically translate between C# and VB.NET. Please see my past answer: Code Interpretation, C# to VB.NET[^].
—SA
The error message is quite clear - you have a<asp:TemplateField>
in your grid, which is not a<asp:BoundField>
, so your attempt to cast it as such will fail.
The equivalent code would also fail in C#.
To make it work in C#, you would use theas
operator[^] and test fornull
. In VB.NET, theTryCast
operator[^] performs a similar function:
Protected Sub GriView1_RowCreated(ByVal sender As Object, ByVal e As GridViewRowEventArgs) Handles GridView1.RowCreated For Each cell As TableCell In e.Row.Cells ' No need to compare to an empty string as well; ' this has already been taken care of by the "IsNullOrEmpty" call. If Not String.IsNullOrEmpty(cell.Text) Then Dim fieldCell As DataControlFieldCell = TryCast(cell, DataControlFieldCell) If fieldCell IsNot Nothing Then Dim field As BoundField = TryCast(fieldCell.ContainingField, BoundField) If field IsNot Nothing AndAlso field.DataField = "Name" Then field.ReadOnly = True End If End If End If Next End Sub
这篇关于这段代码正在使用C#代码,但我无法在VB.net上运行。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文