Private Sub show_grid()
With DataGridView1
.ColumnHeadersDefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter
.Columns.Add("no", "ลำดับ")
.Columns.Add("name", "ชื่อ - นามสกุล")
' .Columns.Add("edit", "แก้ไข")
' .Columns.Add("delete", "ลบ")
.Columns("no").DataPropertyName = "no"
.Columns("name").DataPropertyName = "name"
.AutoGenerateColumns = False
dt= New DataTable ' อย่าลืมประกาศตัวแปร public dt as datatable ข้างบน
dt.Columns.Add("no", GetType(System.String))
dt.Columns.Add("name", GetType(System.String))
End With
End sub
code ของปุ่ม save
Code (VB.NET)
Private Sub btn_save_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_save.Click
Dim rw As DataRow = dt.NewRow()
Dim row_no as integer = DataGridView1.RowCount + 1
rw("no") = row_no
rw("name") = textbox1.text
dt.Rows.Add(rw)
DataGridView1.DataSource = dt
End Sub