简体   繁体   中英

How can I change the column name from datagridview with access database

我想改变这个

or how can I populate the rows only from my datagridview since I want to assign a columnheader name on my datagridview instead of filling the database with all my data. I want to adjust the column height/width and such of each column. Thank you

I know why you put an image, but you should perhaps try to avoid it for future questions; people tend to prefer seeing code as text they can copypaste

You have your DGV set to autogenerate columns so it makes columns with a HeaderText that is the same name as the column. Just change the column HeaderText after the columns have been bound (binding happens when you set the DataSource property):

For example you can look the DGV column up by the datatable column it is bound to:

yourDataGridView.Columns.Cast(Of DataGridViewColumn)().First(Function(c) c.DataPropertyName = "ip").HeaderText = "IP"

Bound columns don't have a name, only an index position, so if you want to find them by name you'll have to look through the columncollection searching for eg where the column's DataPropertyName is "ip", which is what the LINQ above does. It's a bit awkward as a syntax because a DataGridViewColumnCollection doesn't do LINQ without being cast. It might be simpler to put all your mappings into a dictionary and use a loop:

Dim d = New Dictionary(Of String, String) From
  {{"ip", "IP"}, {"aws", "AmazonWebServices"}, ... }
For Each dgvc as DataGridViewColumn in d
  If d.ContainsKey dgvc.DataPropertyName Then d.HeaderText = d(dgvc.DataPropertyName)
Next dgvc

If you know "ip" column will always be first you can more simply:

yourDataGridView.Columns(0).HeaderText = "IP"

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