简体   繁体   English

在没有标题的datagridview中显示数据库

[英]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 . 我想将数据库表加载到datagridview,我可以编辑和更新列,我现在使用列表视图,但我想使用gartagridview,因为它更容易编辑其中的数据。

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. 如何在datagridview中执行相同操作而不显示数据库的标头并在datagridview属性中使用我的自定义标头。

if i use this code to show data in datagridview : 如果我使用此代码在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 在使用检索到的数据表设置DataGridView.DataSource属性后,您可以启动一个简单的循环,将列标题文本更改为您的首选项

.....
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. 您可以将datagridview的auto generate columns属性设置为false ,并使用每列的data属性name属性将其映射到数据源的列。

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; 这样你就可以从表中选择Column1Column5 ,但只有Column1Column3会出现在DGV上; also the column headers will be ColumnA , ColumnB and ColumnC . 列标题也是ColumnAColumnBColumnC

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; 您还可以在DataGridView上的设计时指定它,以保存一些代码并从非常开始的列中定义将出现以及如何; you'll only need to set AutoGenerateColumns = false before setting it's data source. 在设置数据源之前,您只需要设置AutoGenerateColumns = false

设计中的DataPropertyName

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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