Dim row As GridViewRow = GridView1.SelectedRow
だが、TemplateFieldとしてイメージボタン等を配置した場合、このボタンをクリックしてもSelectedRowは取得できない。
その場合どうするか。MicrosoftのヘルプにはRowCommandイベントを使って取得する例が載っている。(下リンク)
http://msdn2.microsoft.com/ja-jp/library/system.web.ui.webcontrols.gridview.rowcommand(VS.80).aspx
が、注意すべきはこのイベントだけの手続きでは取得できないことだ。
その下に記載されているRowCreatedイベントとセットになっていることを当初全く考えもしなかったため、いくらやっても取得できず、最後には「ヘルプが間違っている」とまで思っていたのだ(^^;
説明の仕方にも問題があるとは言えないだろうか?
Microsoftのヘルプのサンプルコード(補足)
Sub CustomersGridView_RowCreated(ByVal sender As Object, _
ByVal e As GridViewRowEventArgs)
If e.Row.RowType = DataControlRowType.DataRow Then
Dim addButton As LinkButton = CType(e.Row.Cells(0).Controls(0), LinkButton)
'ここで行番号をセットしておく
addButton.CommandArgument = e.Row.RowIndex.ToString()
End If
End Sub
Sub CustomersGridView_RowCommand(ByVal sender As Object, _
ByVal e As GridViewCommandEventArgs)
If e.CommandName = "Add" Then
'セットしておいた行番号を取得
Dim index As Integer = Convert.ToInt32(e.CommandArgument)
Dim row As GridViewRow = CustomersGridView.Rows(index)
Dim item As New ListItem()
item.Text = Server.HtmlDecode(row.Cells(2).Text)
If Not CustomersListBox.Items.Contains(item) Then
CustomersListBox.Items.Add(item)
End If
End If
End Sub
私も「ヘルプが間違っている」と思って悩んでいたところでした。
本当に感謝