PDA

View Full Version : Cần giúp về thêm dòng trong DataGridView



huydungktv
01-12-2010, 22:35
Em có đoạn code thế này

sql = "SELECT masp , tensp FROM sanpham ORDER BY tensp"
Dim adapter As New OleDbDataAdapter(sql, conn)
Dim table As New DataTable
adapter.Fill(table)
DataGridView1.DataSource = table

Như vậy DataGridView1 đã được đổ dữ liệu vào và bây giờ muốn thêm vài dòng khác nữa vào DataGridView1, em có sử dụng DataGridView1.rows.add nhưng không được

zxcongducxz
21-12-2010, 11:01
Giải thuật 1: thêm dòng trong datatable

Cách 1
Dim sql$ = "SELECT masp , tensp FROM sanpham ORDER BY tensp" _
, table As New DataTable() _
, CaiGiDay = New OleDbDataAdapter(sql, "<chuoiketnoi>").Fill(table) _
, strThemMoi As String() = New String() {"a", "b"}
table.Rows.Add(strThemMoi)
DataGridView1.DataSource = table


Cách 2
Dim sql$ = "SELECT masp , tensp FROM sanpham ORDER BY tensp" _
, table As New DataTable() _
, CaiGiDay = New OleDbDataAdapter(sql, "<chuoiketnoi>").Fill(table) _
, rowMoi As DataRow = table.NewRow()
rowMoi.Item(0) = "a"
rowMoi.Item(1) = "b"
table.Rows.Add(rowMoi)
DataGridView1.DataSource = table

Giải thuật 2: Thêm vào DataGridView

Cách 1:
Dim sql$ = "SELECT masp , tensp FROM sanpham ORDER BY tensp" _
, table As New DataTable() _
, CaiGiDay = New OleDbDataAdapter(sql, "<chuoiketnoi>").Fill(table)
DataGridView1.DataSource = table
DataGridView1.Rows.Add(New String() {"a", "b"})

Cách 2:
Dim sql$ = "SELECT masp , tensp FROM sanpham ORDER BY tensp" _
, table As New DataTable() _
, CaiGiDay = New OleDbDataAdapter(sql, "<chuoiketnoi>").Fill(table)
DataGridView1.DataSource = table
Dim rowMoi As New DataGridViewRow()
rowMoi.Cells(0).Value = "a"
rowMoi.Cells(1).Value = "b"
DataGridView1.Rows.Add(rowMoi)

thanhdanhsoft
21-12-2010, 13:16
Giải thuật 1: thêm dòng trong datatable

Cách 1
Dim sql$ = "SELECT masp , tensp FROM sanpham ORDER BY tensp" _
, table As New DataTable() _
, CaiGiDay = New OleDbDataAdapter(sql, "<chuoiketnoi>").Fill(table) _
, strThemMoi As String() = New String() {"a", "b"}
table.Rows.Add(strThemMoi)
DataGridView1.DataSource = table


Cách 2
Dim sql$ = "SELECT masp , tensp FROM sanpham ORDER BY tensp" _
, table As New DataTable() _
, CaiGiDay = New OleDbDataAdapter(sql, "<chuoiketnoi>").Fill(table) _
, rowMoi As DataRow = table.NewRow()
rowMoi.Item(0) = "a"
rowMoi.Item(1) = "b"
table.Rows.Add(rowMoi)
DataGridView1.DataSource = table

Giải thuật 2: Thêm vào DataGridView

Cách 1:
Dim sql$ = "SELECT masp , tensp FROM sanpham ORDER BY tensp" _
, table As New DataTable() _
, CaiGiDay = New OleDbDataAdapter(sql, "<chuoiketnoi>").Fill(table)
DataGridView1.DataSource = table
DataGridView1.Rows.Add(New String() {"a", "b"})

Cách 2:
Dim sql$ = "SELECT masp , tensp FROM sanpham ORDER BY tensp" _
, table As New DataTable() _
, CaiGiDay = New OleDbDataAdapter(sql, "<chuoiketnoi>").Fill(table)
DataGridView1.DataSource = table
Dim rowMoi As New DataGridViewRow()
rowMoi.Cells(0).Value = "a"
rowMoi.Cells(1).Value = "b"
DataGridView1.Rows.Add(rowMoi)

Cái này cơ bản mà phải ko bác zxcongducxz.
Hit hit