1
votes

i want to load database table to datagridview to i can edit and update the column , I'm now using list view but i want to use gartagridview because its more ease to edit the data in it .

I'm using this code to show data in database in list view :

Public Sub showlistview()
    Try
        Dim dt As New DataTable
        Dim ds As New DataSet
        ds.Tables.Add(dt)
        Dim da As New OleDbDataAdapter("select * from pay_pretalk where cust_id=" & pretalk_pay.TextBox4.Text & " order by sdate DESC ", con)
        da.Fill(dt)
        Dim myrow As DataRow
        For Each myrow In dt.Rows
            ListView1.Items.Add(myrow.Item(0))
            ListView1.Items(ListView1.Items.Count - 1).SubItems.Add(myrow.Item(2))
            ListView1.Items(ListView1.Items.Count - 1).SubItems.Add(myrow.Item(3))
            ListView1.Items(ListView1.Items.Count - 1).SubItems.Add(myrow.Item(4))
            ListView1.Items(ListView1.Items.Count - 1).SubItems.Add(myrow.Item(5))
            ListView1.Items(ListView1.Items.Count - 1).SubItems.Add(myrow.Item(6))
            ListView1.Items(ListView1.Items.Count - 1).SubItems.Add(myrow.Item(7))
        Next
    Catch ex As Exception
        MsgBox(ex.Message)
    End Try
End Sub

how can i do the same in datagridview without showing the header of the database and use my custom header in the datagridview properties.

if i use this code to show data in datagridview :

        Dim dt As New DataTable
        Dim ds As New DataSet
        ds.Tables.Add(dt)
        Dim da As New OleDbDataAdapter("select * from pay_pretalk where cust_id=" & pretalk_pay.TextBox4.Text & " order by sdate DESC ", con)
        da.Fill(dt)
        DataGridView1.DataSource = dt

this show all table with the header !, but i have already add a custom header , so this miss up everything :(

2
Do you mean without showing the column names in the header of each column of a datagridview? - Steve
yes @Steve i want to add just the records from database , i want the header column in deferent names . - Rabeea Qabaha

2 Answers

2
votes

After setting the DataGridView.DataSource property with the datatable retrieved you could start a simple loop that changes the columns header text to your preferences

.....
dt.Columns.RemoveAt(1)
dataGridView1.DataSource = dt

for each cl As DataGridViewColumn in dataGridView1.Columns
    Select Case cl.HeaderText
        Case "FirstColumnName": 
           cl.HeaderText="MyColumn1Name" 
        Case "SecondColumnName": 
           cl.HeaderText="MyColumn2Name"
        case "ThirdColumnName": 
            cl.HeaderText="MyColumn3Name"

        ... and so on for the other columns ....
    End Select
Next
1
votes

You can set to false the auto generate columns property of the datagridview, and use the data property name property of each column to map it to a column of the data source.

Sample Code:

DataGridView1.AutoGenerateColumns = False
Dim index As Integer
index = DataGridView1.Columns.Add("ColumnA", "ColumnA")
DataGridView1.Columns(index).DataPropertyName = "Column1"
index = DataGridView1.Columns.Add("ColumnB", "ColumnB")
DataGridView1.Columns(index).DataPropertyName = "Column2"
index = DataGridView1.Columns.Add("ColumnC", "ColumnC")
DataGridView1.Columns(index).DataPropertyName = "Column3"

Dim dt As New DataTable
Dim ds As New DataSet
ds.Tables.Add(dt)
Dim da As New OleDbDataAdapter("SELECT Column1, Column2, Column3, Column4, Column5 FROM Table1 WHERE Condition = true", con)
da.Fill(dt)
DataGridView1.DataSource = dt

This way you're selecting Column1 to Column5 from the table but only Column1 to Column3 will appear on the DGV; also the column headers will be ColumnA, ColumnB and ColumnC.

You can also specify this in design-time on the DataGridView, to save some code and to define from the very beggining wich columns will appear and how; you'll only need to set AutoGenerateColumns = false before setting it's data source.

DataPropertyName in design