2012-03-30 50 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。謝謝你們倆!