简体   繁体   中英

show database in datagridview without header

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 :(

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM