摘要:[ASP.NET] 使用GridView_RowCommand事件時,取得所點該列為哪一列
問題 : 在GridView中放入一個TemplateField,裡面擺了一個Button,想要在點選Button得知它是哪一列
進而可以取得其他相關資訊,如一個欄的文字;或是改變該列之顏色

參考:http://aspdotnet-sequel.blogspot.tw/2009/11/aspnet-gridview-rowcommand-get-row.html
.aspx
<%@ Page Language="VB" EnableViewState ="false" AutoEventWireup="false" CodeFile="Test0702.aspx.vb" Inherits="Test0702" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:GridView ID="GridView1" runat="server">
<Columns>
<asp:TemplateField HeaderText="TEST">
<ItemTemplate>
<asp:Button ID="Button1" CommandName ="test" runat="server" Text="Button" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
</div>
</form>
</body>
</html>
.vb
Protected Sub GridView1_RowCommand(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewCommandEventArgs) Handles GridView1.RowCommand
If e.CommandName = "test" Then
Dim selectedRow As GridViewRow = DirectCast(DirectCast(e.CommandSource, Button).NamingContainer, GridViewRow)
Dim intRowIndex As Integer = Convert.ToInt32(selectedRow.RowIndex)
GridView1.Rows(intRowIndex).BackColor = System.Drawing.Color.Blue
Dim test As String = GridView1.Rows(intRowIndex).Cells(0).Text
End If
End Sub
