ทำการเขียนโค้ดเพิ่มข้อมูลคะ แต่พอแก้ไขแล้วข้อมูลไม่ยอม data grid view ไม่ยอม Refresh
ต้องทำการปิดและเปิดใหม่ทุกครั้งเลยคะ พอจะมีวิธีไหนบ้างคะที่ให้ Refresh Auto เลยขณะที่เพิ่มข้อมูลเสร็จ
ขอบคุณคะ Code (VB.NET)
Private Sub btnAdd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAdd.Click
If (txtSupID.Text = "" Or txtSupName.Text = "" Or txtAddress.Text = "" Or txtTel.Text = "") Then
MessageBox.Show("Please enter all fields.", "Create Supplier Master List", MessageBoxButtons.OK, MessageBoxIcon.Error)
Else
strsql = "INSERT INTO Suppiler(Sup_id ,Sup_name ,Address ,Tel)"
strsql &= "VALUES('" & txtSupID.Text & "' ,'" & txtSupName.Text & "','" & txtAddress.Text & "' ,'" & txtTel.Text & "')"
With cmd
.Connection = con
.CommandText = strsql
.CommandType = CommandType.Text
.ExecuteNonQuery()
MessageBox.Show("Create Supplier Master List Complete.", "Create Supplier Master List", MessageBoxButtons.OKCancel, MessageBoxIcon.Information)
End With
txtSupID.Clear()
txtSupName.Clear()
txtAddress.Clear()
txtTel.Clear()
End If
End Sub
Tag : ASP, Ms SQL Server 2008, VB.NET, VS 2010 (.NET 4.x)
With cmd
.Connection = con
.CommandText = strsql
.CommandType = CommandType.Text
.ExecuteNonQuery()
MessageBox.Show("Create Supplier Master List Complete.", "Create Supplier Master List", MessageBoxButtons.OKCancel, MessageBoxIcon.Information)
'Call sub display
me.Display()
End With
ทำการ Select data ขึ้นมาใหม่ค่ะ (เผื่อช่วยได้นะค่ะ )
Code (VB.NET)
With cmd
.Connection = con
.CommandText = strsql
.CommandType = CommandType.Text
.ExecuteNonQuery()
MessageBox.Show("Create Supplier Master List Complete.", "Create Supplier Master List", MessageBoxButtons.OKCancel, MessageBoxIcon.Information)
End With
txtSupID.Clear()
txtSupName.Clear()
txtAddress.Clear()
txtTel.Clear()
'เรียก Refresh grid มาใช้
shwgdet()
Private Sub shwgdet()
DataGridView1.Refresh()
con = New OleDbConnection(strcon)
If con.State = ConnectionState.Closed Then con.Open()
dt = New DataTable
da = New OleDbDataAdapter("select bar,ldd,ltm,note from brrecret where left(bar,17) like '" & Lotdet & "' and brrecret.note = '" & rea & "' and (load IS NULL or load = '' ) and (box IS NULL or box = '' ) order by ldt desc ", con)
da.Fill(dt)
If dt.Rows.Count = 0 Then
Me.DataGridView1.Columns.Clear()
Else
'-----Create Gride-----
Dim tbl As DataTable
Dim r As DataRow
tbl = New DataTable
tbl.Columns.Add(New DataColumn("BAR", GetType(System.String)))
tbl.Columns.Add(New DataColumn("DATE", GetType(System.String)))
tbl.Columns.Add(New DataColumn("TIME", GetType(System.String)))
tbl.Columns.Add(New DataColumn("Reason", GetType(System.String)))
DataGridView1.DataSource = tbl
'-----------------------------------------
For i = 0 To dt.Rows.Count - 1
With DataGridView1
r = tbl.NewRow
r("BAR") = dt.Rows(i).Item("bar")
r("DATE") = Convert.ToDateTime(dt.Rows(i).Item("ldd")).ToString("dd-MM-yy")
r("TIME") = Convert.ToDateTime(dt.Rows(i).Item("ltm")).ToString("HH:mm:ss")
r("Reason") = dt.Rows(i).Item("note")
tbl.Rows.Add(r)
tbl.NewRow()
End With
DataGridView1.DataSource = tbl
Next
DataGridView1.Columns(0).Width = 220
DataGridView1.Columns(1).Width = 150
DataGridView1.Columns(2).Width = 150
DataGridView1.Columns(3).Width = 500
DataGridView1.ColumnHeadersDefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter
DataGridView1.RowsDefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleLeft
DataGridView1.DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleLeft
con.Close()
End If
end sub
น่าจะสร้าง Sub Load... ข้อมูลที่จะมาแสดงใน DataGridView นะครับ เมื่อกดบันทึก แล้วก็เรียก Sub Load...
Ex:
Code (VB.NET)
Sub LoadType()
OpenDatabase()
Dim CmdLoad As New SqlCommand("SELECT * FROM SAStype ORDER BY SAStypeCode", Connt)
If CmdLoad.Connection.State = ConnectionState.Open Then CmdLoad.Connection.Close()
CmdLoad.Connection.Open()
Dim DsLoad As New DataSet
Dim DaLoad As New SqlDataAdapter(CmdLoad)
DaLoad.Fill(DsLoad)
DGV.DataSource = DsLoad.Tables(0)
DsLoad = Nothing
DaLoad = Nothing
CmdLoad.Connection.Close()
CmdLoad = Nothing
End Sub
เรียกใช้ Code (VB.NET)
Private Sub btnAdd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAdd.Click
If (txtSupID.Text = "" Or txtSupName.Text = "" Or txtAddress.Text = "" Or txtTel.Text = "") Then
MessageBox.Show("Please enter all fields.", "Create Supplier Master List", MessageBoxButtons.OK, MessageBoxIcon.Error)
Else
strsql = "INSERT INTO Suppiler(Sup_id ,Sup_name ,Address ,Tel)"
strsql &= "VALUES('" & txtSupID.Text & "' ,'" & txtSupName.Text & "','" & txtAddress.Text & "' ,'" & txtTel.Text & "')"
With cmd
.Connection = con
.CommandText = strsql
.CommandType = CommandType.Text
.ExecuteNonQuery()
MessageBox.Show("Create Supplier Master List Complete.", "Create Supplier Master List", MessageBoxButtons.OKCancel, MessageBoxIcon.Information)
End With
txtSupID.Clear()
txtSupName.Clear()
txtAddress.Clear()
txtTel.Clear()
LoadType()
End If
End Sub