2012-03-30 49 views
1

以下是应用程序:VB/ASP.NET,访问GridView的单元格值为变量

有一些行的网格视图。当用户单击Web窗体上的编辑时,它会打开一些用于编辑的字段。我想要做的是当RowUpdating触发时,检查网格视图中某个字段的值,并在特定范围内验证其值,然后取消该范围之外的值。

我实际上已经完成了几乎所有的框架,除非我无法从GridView中的字段获取值。

我已经搜索了这个网站与其他几个人,并列出了一些解决方案,但都没有工作。我得到不能转换为字符串/整数,加上一些其他人。

有什么建议吗?这里是我的最新:

Private Sub gvCourses_RowUpdating(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewUpdateEventArgs) Handles gvCourses.RowUpdating 
    Dim x As String 
    x = gvCourses.SelectedRow.Cells[3].Text 
    MsgBox(x) ' Just a check to see if I get the variable to display the correct value, not part of the final app 

我也尝试过上述的不同变化,没有任何工作。上面我得到的System.Web.UI.WebControls.TableCellCollection不能转换为String。

我见过几个例子,但都没有工作。任何人有任何建议?

谢谢。

回答

2

尝试将该行更改为。

x = TryCast(gvCourses.SelectedRow.Cells(3).Controls(1), TextBox).Text 

当gridview的是在更新模式下它呈现的文本框(如果它被绑定到的文本数据)和细胞将其添加到其的ControlCollection。

+0

如果我这样做,我会得到:1.“文本框”是一种类型,不能用作表达式。 2.')'预计。 – kaylendarr 2012-03-30 23:08:21

+0

@kaylendarr修改...请再试一次。 – scartag 2012-03-30 23:10:12

+0

.TableCellCollection无法转换为'String'和'identifier expected',其中[3]是。\ – kaylendarr 2012-03-30 23:13:19

1

你有没有试过以下?:

Dim x As String 
x = gvCourses.Rows[e.RowIndex].Cells[3].Text; 
+0

是的。我明白了。GridViewRowCollection不能转换为'String',而包围标识符缺少关闭']'(不是)。 – kaylendarr 2012-03-30 23:11:22

0

所以,我终于想通了如何完成我需要的东西。对于其他人来说,这可能对你有所帮助。

原始问题: - “我正在尝试做的是当RowUpdating触发时,检查网格视图中某个域的值并验证它在一定范围内,然后取消它的外部那范围“

解决方案:

Private Sub gvCourses_RowUpdating(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewUpdateEventArgs) Handles gvCourses.RowUpdating 
    Dim x As Integer = e.NewValues.Item("Column_Name") 
     ' The above grabs the value from the text box of the specified column and assigns it to x. I can then use x to do the validation I noted above. 

看来我是有一个逻辑问题,并试图用SelectedRows等,当我是什么,我试图做完全脱落基地。

特别感谢scartag为您的多次尝试提供帮助,并感谢Dennis。谢谢你们俩!