HOME > .NET Framework > Forum > checkbox to delete within gridview ทำการลบโดย เลือกจาก checkboxใน gridview แล้วกดปุ่ม delete แต่ทำยังงัย มันก้อไม่ยอมเข้าลูปเลยค่ะ
checkbox to delete within gridview ทำการลบโดย เลือกจาก checkboxใน gridview แล้วกดปุ่ม delete แต่ทำยังงัย มันก้อไม่ยอมเข้าลูปเลยค่ะ
-- code behind---
Protected Sub btnDelete_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnDelete.Click
For Each row As GridViewRow In gvMagnetWireList.Rows
Dim checkbox As CheckBox = CType(row.FindControl("chkMagnetRowID"), CheckBox)
If checkbox.Checked Then 'มันไม่เข้า loop นี้เลย
Dim MgWireID As Integer = Convert.ToInt32(gvMagnetWireList.DataKeys(row.RowIndex).Value)
Dim strconn As String
Response.Write(MgWireID)
strconn = WebConfigurationManager.ConnectionStrings("scConnectionString").ConnectionString
Dim Conn As New SqlConnection(strconn)
Conn.Open()
Dim da As New SqlDataAdapter
Dim ds As New DataSet
Dim sqlSelectCmd As New SqlCommand
sqlSelectCmd.CommandType = CommandType.StoredProcedure
sqlSelectCmd.CommandText = "dbo.sp_magnetDelete"
sqlSelectCmd.Connection = Conn
Dim paramUserid As New SqlParameter("@vc_UserID", System.Data.SqlDbType.NVarChar)
Dim paramMgWireID As New SqlParameter("@vc_mgnetwireinfo_id", System.Data.SqlDbType.Int)
paramUserid.Value = lblUserName.Text
paramMgWireID.Value = MgWireID
ลองทำตามแระค่ะพี่ มันerrorค่ะพี่วิน
Dim chkDel As CheckBox = CType(gvMagnetWireList.FindControl("chkMagnetRowID"), CheckBox)
Dim TextBox1 As TextBox = CType(gvMagnetWireList.FindControl("TextBox1"), TextBox)
If chkDel.Checked = True Then '- - - >error บรรทัดนี้น่ะพี่วิน
มันฟ้องว่า
System.NullReferenceException: Object reference not set to an instance of an object.
Line 325: ' Response.Write(i)
Line 326: ' If isChecked = True Then
Line 327: If chkDel.Checked = True Then
Date :
2010-06-04 13:28:35
By :
เนย
No. 3
Guest
debug ก้อไม่ได้ด้วย มันฟ้อง error
unable to connect to the asp.net development server
Imports System.Collections.Generic
Imports System.Linq
Imports System.Web
Imports System.Web.UI
Imports System.Web.UI.WebControls
Imports System.Data
Public Partial Class MyGridView
Inherits System.Web.UI.Page
Private dataSource As DataTable
Protected Sub Page_Init(sender As Object, e As EventArgs)
dataSource = New DataTable()
End Sub
Protected Sub Page_Load(sender As Object, e As EventArgs)
dataSource = If(ViewState("dataSource") IsNot Nothing, DirectCast(ViewState("dataSource"), DataTable), GetDataSource())
GridView1.DataSource = dataSource
Dim Index As New BoundField()
Index.HeaderText = "#"
Index.HeaderStyle.HorizontalAlign = HorizontalAlign.Center
Index.ItemStyle.HorizontalAlign = HorizontalAlign.Right
Index.DataField = "ID"
Dim MonthName As New BoundField()
MonthName.HeaderText = "MonthName"
MonthName.HeaderStyle.HorizontalAlign = HorizontalAlign.Center
MonthName.ItemStyle.HorizontalAlign = HorizontalAlign.Left
MonthName.DataField = "Month"
Dim DeleteButtom As New CommandField()
DeleteButtom.HeaderText = "Delete"
DeleteButtom.ButtonType = ButtonType.Image
DeleteButtom.ShowEditButton = False
DeleteButtom.ShowDeleteButton = True
DeleteButtom.DeleteImageUrl = "~/images/delete-16x16.png"
DeleteButtom.ItemStyle.HorizontalAlign = HorizontalAlign.Center
DeleteButtom.HeaderStyle.HorizontalAlign = HorizontalAlign.Center
GridView1.AutoGenerateColumns = False
GridView1.DataKeyNames = New String() {"ID"}
If Not IsPostBack Then
ViewState("dataSource") = dataSource
GridView1.Columns.Add(Index)
GridView1.Columns.Add(MonthName)
GridView1.Columns.Add(DeleteButtom)
GridView1.DataBind()
End If
End Sub
Protected Sub GridView1_RowDataBound(sender As Object, e As GridViewRowEventArgs) Handles GridView1_RowDataBound
If e.Row.RowType = DataControlRowType.DataRow Then
Dim DeleteButton As ImageButton = DirectCast(e.Row.Cells(2).Controls(0), ImageButton)
DeleteButton.OnClientClick = "javascript:return confirm('คุณต้องการลบข้อมูลนี้ใช่หรือไม่')"
End If
End Sub
Protected Sub GridView1_RowDeleting(sender As Object, e As GridViewDeleteEventArgs) Handles GridView1.RowDeleting
dataSource.Rows(e.RowIndex).Delete()
dataSource.AcceptChanges()
GridView1.DataSource = dataSource
GridView1.DataBind()
ViewState("dataSource") = dataSource
End Sub
Private Function GetDataSource() As DataTable
Dim Dt As New DataTable()
Dt.Columns.Add(New DataColumn("ID", GetType(Integer)))
Dt.Columns.Add(New DataColumn("Month", GetType(String)))
For i As Integer = 1 To 12
Dim Dr As DataRow = Dt.NewRow()
Dr("ID") = i.ToString()
Dr("Month") = DateTime.Parse(String.Format("1/{0}/2010", i.ToString())).ToString("MMMM")
Dt.Rows.Add(Dr)
Next
Return Dt
End Function
End Class