แต่ไหนที่นี้คือ มันเอาค่า id ลำดับสุดท้าย แสดง ไม่ใช่ตัวที่เราคลิกเลือก จะมีวิธีแก้ไขยังไงค่ะ
มีโค้ดตัวอย่าง
Protected Sub GVshowName_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles GVshowName.RowDataBound
Dim CurrentRowView As System.Data.DataRowView = e.Row.DataItem
If e.Row.RowType = DataControlRowType.DataRow Then
Dim ibnAttachFile As ImageButton = CType(e.Row.FindControl("ibnAttachFileEmployee"), ImageButton)
Dim connString As String = System.Configuration.ConfigurationManager.ConnectionStrings("warehouseConnectionString").ConnectionString.ToString
Dim connectionString As String = connString
Dim dbConnection As System.Data.IDbConnection = New System.Data.SqlClient.SqlConnection(connectionString)
Dim dbCommand As System.Data.IDbCommand = New System.Data.SqlClient.SqlCommand
dbCommand.CommandType = System.Data.CommandType.StoredProcedure
dbCommand.CommandText = "procSRule_selectbystatusid" 'ชื่อ Store Procedureการแสดงข้อมูล
dbCommand.Connection = dbConnection
Dim dbParam_activity_project_id As System.Data.IDataParameter = New System.Data.SqlClient.SqlParameter
dbParam_activity_project_id.ParameterName = "@id"
dbParam_activity_project_id.Value = GVshowName.SelectedIndex
dbParam_activity_project_id.DbType = System.Data.DbType.StringFixedLength
dbCommand.Parameters.Add(dbParam_activity_project_id)
Dim dataAdapter As System.Data.IDbDataAdapter = New System.Data.SqlClient.SqlDataAdapter
dataAdapter.SelectCommand = dbCommand
Dim dataSet As System.Data.DataSet = New System.Data.DataSet
dataAdapter.Fill(dataSet)
Dim myurl As String = "displaypdf.aspx"
If dataSet.Tables(0).Rows.Count <> 0 Then
If Len(dataSet.Tables(0).Rows(0).Item("rule_url").ToString) > 3 Then
Session("pdfpath") = dataSet.Tables(0).Rows(0).Item("rule_url").ToString
End If
End If
If Session("usertype") = "hi_user" Or Session("usertype") = "admin" Then
myurl = "displaypdf.aspx"
ElseIf Session("usertype") = "mid_user" Then
If Session("rule_level").ToString = "1" Then
Response.Redirect("displaypdf_admin.aspx")
Else
myurl = "displaypdf.aspx"
End If
Else
myurl = "contactinfo.aspx"
End If
ibnAttachFile.PostBackUrl = String.Format(myurl)
End If
If e.Row.RowType = DataControlRowType.DataRow Then
Dim ibnAttachFile As ImageButton = CType(e.Row.FindControl("ibnAttachFileEmployee"), ImageButton)
ibnAttachFile.CommandArgument = GridView1.DataKeys(e.Rows.RowIndex).Value
แล้วเวลาคลิกก็ใช้ event OnCommand และ CommandEventHandler และ CommandEventArgs e
เวลาอ้างอิง id ก็ใช้ e.CommandArgument
Code (VB.NET)
Private Sub ibnAttachFile_Command(ByVal sender As Object, ByVal e As CommandEventArgs) Handles ibnAttachFile.Command
Dim ID As String
ID = e.CommandArgument
End Sub
Imports System.Data
Partial Class DeleteGridView
Inherits System.Web.UI.Page
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Dim CustomerData As SqlDatabaseManager = New SqlDatabaseManager()
Dim CustomerDataTable As DataTable = New DataTable()
CustomerData.CommandString = "Select * From [Customer] Where [TypeID]=1"
CustomerDataTable = CustomerData.ExecuteQuery()
Dim ID As BoundField = New BoundField()
Dim CustomerName As HyperLinkField = New HyperLinkField()
Dim CustomerPhone As BoundField = New BoundField()
Dim DeleteButton As CommandField = New CommandField()
ID.HeaderText = "#"
CustomerName.HeaderText = "ชื่อลูกค้า"
CustomerName.DataTextField = "CustomerThaiName"
CustomerPhone.HeaderText = "โทรศัพท์"
CustomerPhone.DataField = "CustomerPhone"
DeleteButton.HeaderText = "ลบ"
DeleteButton.ButtonType = ButtonType.Image
DeleteButton.ShowEditButton = False
DeleteButton.ShowDeleteButton = True
DeleteButton.DeleteImageUrl = "~/images/delete-16x16.png"
Dim KeyNames() As String = New String() {"CustomerCode"}
GridView1.AutoGenerateColumns = False
GridView1.DataKeyNames = KeyNames
If Not IsPostBack Then
GridView1.Columns.Add(ID)
GridView1.Columns.Add(CustomerName)
GridView1.Columns.Add(CustomerPhone)
GridView1.Columns.Add(DeleteButton)
GridView1.DataSource = CustomerDataTable
GridView1.DataBind()
End If
End Sub
Protected Sub GridView1_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles GridView1.RowDataBound
If e.Row.RowType = DataControlRowType.DataRow Then
Dim CustomerLink As HyperLink = CType(e.Row.Cells(1).Controls(0), HyperLink)
Dim CustomerDeleteButton As ImageButton = CType(e.Row.Cells(3).Controls(0), ImageButton)
e.Row.Cells(0).Text = e.Row.RowIndex + 1
CustomerLink.NavigateUrl = "~/customer.aspx?id=" & GridView1.DataKeys(e.Row.RowIndex).Value
CustomerDeleteButton.OnClientClick = "return confirm('คุณต้องการลบข้อมูลนี้ใช่หรือไม่?')"
CustomerDeleteButton.ToolTip = "ลบ"
End If
End Sub
Protected Sub GridView1_RowDeleting(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewDeleteEventArgs) Handles GridView1.RowDeleting
Dim CustomerData As SqlDatabaseManager = New SqlDatabaseManager()
CustomerData.CommandString = "Delete From [Customer] Where [CusotmerCode]=@CustomerCOde"
CustomerData.AddParameter("@CustomerCode", SqlDbType.NVarChar, GridView1.DataKeys(e.RowIndex).Value)
CustomerData.ExecuteNonQuery()
CustomerData.CommandString = "Select * From [Customer] Where [TypeID]=1"
GridView1.DataSource = CustomerData.ExecuteQuery()
GridView1.DataBind()
End Sub
End Class