 |
|
Dataset สามารถเคลียร์ค่าได้ไหมครับ มีวิธีการอย่างไรบ้างครับ |
|
 |
|
|
 |
 |
|
Code (C#)
sub SetGrid()
Datagridview.Datasource =null
ds = BindDS()
With DataGridView1
.DataSource = ds.Tables("TableName")
.AutoGenerateColumns = False
End With
end sub
|
 |
 |
 |
 |
Date :
2014-11-20 11:57:58 |
By :
lamaka.tor |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
close() มันก็เคลียร์ แล้วไม่ใช่เหรอครับ
|
 |
 |
 |
 |
Date :
2014-11-20 12:03:17 |
By :
Chaidhanan |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ขอโทษครับ
ผมหา Datagridview.Datasource.Clear() ไม่เจอ   
มันต้องเพิ่มตัวไหนเข้ามาป่าวครับ
ผมเจอแต่แบบนี้อ่า
Company_PositionDataGridView.DataBindings.Clear()
|
 |
 |
 |
 |
Date :
2014-11-20 12:27:34 |
By :
lamaka.tor |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ลองเพิ่ม
dt = new datatable
ก่อน
da.Fill(dt)
ดูครับ เผื่อได้
|
 |
 |
 |
 |
Date :
2014-11-20 13:17:43 |
By :
fonfire |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
งั้นเพิ่ม
messagebox.show(dt.rows.count)
เข้าไปแทนได้ไหมครับ
จะได้รู้ว่าข้อมูลมันได้มาเพิ่มจริง ๆ
-------------------
ds = BindDS()
With DataGridView1
.DataSource = ds.Tables("TableName")
.AutoGenerateColumns = False
End With
ขอดูโค้ด BindDS เต็ม ๆ ได้ไหมครับ
|
 |
 |
 |
 |
Date :
2014-11-20 13:28:09 |
By :
fonfire |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ขั้นตอนการทำงานนะครับ
ผมคลิกที่ปุ่ม

จะเห็นว่ามีข้อมูลเก่าครับ

นี่คือโค้ดที่เรียกมาแสดงเมื่อเปิดฟอร์ม
Code (VB.NET)
Private Sub showDatagridview1()
conn.Open()
cm = conn.CreateCommand()
With cm
.CommandText = "SELECT * FROM GoodsExport ORDER BY rec_no"
.CommandType = CommandType.Text
End With
da = New SqlDataAdapter(cm)
da.Fill(dt)
conn.Close()
DataGridView1.DataSource = dt
DataGridView1.Columns.Clear()
Dim col As DataGridViewTextBoxColumn
col = New DataGridViewTextBoxColumn()
With col
.DataPropertyName = "rec_no"
.HeaderText = " ลำดับ"
.Width = "100"
End With
DataGridView1.Columns.Add(col)
col = New DataGridViewTextBoxColumn()
With col
.DataPropertyName = "dates"
.HeaderText = " วันที่จำหน่าย"
.Width = "150"
End With
DataGridView1.Columns.Add(col)
col = New DataGridViewTextBoxColumn()
With col
.DataPropertyName = "goodsID"
.HeaderText = " รหัสครุภัณฑ์"
.Width = "150"
End With
DataGridView1.Columns.Add(col)
col = New DataGridViewTextBoxColumn()
With col
.DataPropertyName = "goodsName"
.HeaderText = " ชื่อ"
.Width = "150"
End With
DataGridView1.Columns.Add(col)
col = New DataGridViewTextBoxColumn()
With col
.DataPropertyName = "barcode"
.HeaderText = " บาร์โค้ด"
.Width = "150"
End With
DataGridView1.Columns.Add(col)
col = New DataGridViewTextBoxColumn()
With col
.DataPropertyName = "goodsModel"
.HeaderText = " ยี่ห้อ/รุ่น"
.Width = "150"
End With
DataGridView1.Columns.Add(col)
col = New DataGridViewTextBoxColumn()
With col
.DataPropertyName = "goodsDetail"
.HeaderText = " รายละเอียด"
.Width = "150"
End With
DataGridView1.Columns.Add(col)
col = New DataGridViewTextBoxColumn()
With col
.DataPropertyName = "room"
.HeaderText = " ห้อง"
.Width = "150"
End With
DataGridView1.Columns.Add(col)
col = New DataGridViewTextBoxColumn()
With col
.DataPropertyName = "amountExport"
.HeaderText = " จำนวนจำหน่าย"
.Width = "150"
End With
DataGridView1.Columns.Add(col)
End Sub 'โชว์ข้อมูลใน Datagridview1
ผมกดปิดฟอร์มครับ แล้วเปิดฟอร์มมาใหม่ ข้อมูลยังเป็นปกติครับ
Code (VB.NET)
If MessageBox.Show("กลับหน้าหลัก ใช่หรือไม่ ?", "Message", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button1) = Windows.Forms.DialogResult.Yes Then
DataGridView1.DataSource.Clear()
Me.Close()
End If

คราวนี้ผมลองเพิ่มครับ เพิ่มได้ครับ การเรียกข้อมูลมาแสดงคือผ่าน Dataset
Code (VB.NET)
ds = BindDataExport()
With DataGridView1
.DataSource = ds.Tables("GoodsExport")
.AutoGenerateColumns = False
End With
ผมกดปิดฟอร์มครับ

^
^
นี่คือรูปตอนที่เปิดฟอร์มมาใหม่อีกทีนะครับ
จะเห็นว่ามีข้อมูลออกมา 2 ชุด โดยชุดที่ขีดขั้นไว้ข้างบน จะเห็นจำนวนเร็คคอร์ดที่ 141
ส่วนขีดล่างจะเห็นที่เร็คคอร์ด 142 ครับ (142 คือเร็คคอร์ดใหม่ที่เพิ่งเพิ่มเข้าไป)
พอผมกดปิดฟอร์มอีกที่ แล้วเปิดขึ้นมาใหม่ ก็จะขึ้นเหมือนภาพที่ 2 ครับ
ฟอร์มหลักผมคือ frmHome ส่วนฟอร์มนี้คือ frmExport ครับ
frmExport ถูกเปิดโดยใช้คำสั่ง Showdialog() ครับ
|
 |
 |
 |
 |
Date :
2014-11-20 13:38:13 |
By :
nameball |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
โทษทีผมเพิ่งกลับมาไปหัด VB ยุปกติใช้ C#
Code (C#)
Private Sub Form2_Load(sender As Object, e As EventArgs) Handles MyBase.Load
DataGridView1.DataSource = Getdata("SELECT * FROM dbo.Company_Position", "Data Source=192.168.6.204,1433;Initial Catalog=BASE;User ID=LAMAKA.TOR;Password=*******").DefaultView()
End Sub
Function Getdata(sql As String, connstring As String) As DataTable
Dim ds As System.Data.DataSet = New DataSet()
Dim adapter As SqlClient.SqlDataAdapter = New SqlClient.SqlDataAdapter(sql, connstring)
adapter.Fill(ds)
Return ds.Tables(0)
End Function
แบบนี้พอได้ป่ะครับผมไม่ค่อยเหมือนใครชอบ return function เอา
ประเด็นคือแค่อยาก รีเฟชดาต้าเฉยๆรึอย่างอื่นด้วยครับ
|
 |
 |
 |
 |
Date :
2014-11-20 13:39:47 |
By :
lamaka.tor |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ตอนเปิด form
ใช้ .show หรือ .showdialog ครับ
|
 |
 |
 |
 |
Date :
2014-11-20 13:46:50 |
By :
fonfire |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
อ้อ
ถ้าใช้ showdialog
ตอนปิด form
ลองเพิ่ม .dispose เข้าไปด้วยครับ
|
 |
 |
 |
 |
Date :
2014-11-20 13:48:48 |
By :
fonfire |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ขอบคุณครับ ได้แล้วครับ
ทั้งสองวิธีเลยครับ สุดยอดครับ  

|
 |
 |
 |
 |
Date :
2014-11-20 13:55:45 |
By :
nameball |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ขอบคุณ คุณ lamaka.tor
ขอบคุณ คุณ Chaidhanan
ขอบคุณ คุณ fonfire
มากๆครับ ที่เข้ามาให้ความรู้ครับ
|
 |
 |
 |
 |
Date :
2014-11-20 13:58:00 |
By :
nameball |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
  
เรายื่นมือออกไปถึงจะมีคนยื่นมือมาจับกับเรา
คิดดูถ้าเรามีมือครบ 2 มือละ
   
|
 |
 |
 |
 |
Date :
2014-11-20 13:59:40 |
By :
lamaka.tor |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
|
|