简体   繁体   English

如何使用访问数据库更改 datagridview 中的列名

[英]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.或者我如何只从我的 datagridview 填充行,因为我想在我的 datagridview 上分配一个列标题名称,而不是用我的所有数据填充数据库。 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.您已将 DGV 设置为自动生成列,因此它会生成具有与列同名的 HeaderText 的列。 Just change the column HeaderText after the columns have been bound (binding happens when you set the DataSource property):只需在绑定列后更改列 HeaderText(绑定发生在设置 DataSource 属性时):

For example you can look the DGV column up by the datatable column it is bound to:例如,您可以通过它绑定到的数据表列向上查找 DGV 列:

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.绑定列没有名称,只有索引 position,因此如果您想按名称查找它们,则必须查看列集合以搜索例如列的 DataPropertyName 为“ip”的位置,这就是上面的 LINQ做。 It's a bit awkward as a syntax because a DataGridViewColumnCollection doesn't do LINQ without being cast.作为一种语法,它有点笨拙,因为 DataGridViewColumnCollection 不执行转换就不会执行 LINQ。 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:如果您知道“ip”列总是第一个,您可以更简单地:

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

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

相关问题 DataGridView中不需要的名称列 - 如何摆脱它? - Unwanted name column in DataGridView - How can I get rid of it? 如何使用vb.net将数据从数据库绑定到特定的DatagridView列名 - How to bind data from database to a specific DatagridView Column Name using vb.net 如何将标题居中在DataGridView的列中? - How can I center the heading in a column on a DataGridView? 想要删除带有Access数据库列的DataGridView列 - Want to delete DataGridView column with Access database column 如何使 datagridview 实时连接到本地存储在 VB.NET 中的访问数据库? - How can I make a datagridview live connected to an access database stored locally in VB.NET? 如何将带有访问数据库的 Datagridview 导出到 CSV 文件? - How can export the Datagridview with access database to CSV file? 如何使用带有数据库值的计算在DataGridView中添加列? - How can you add a column in DataGridView using calculations with database values? 如何使用Npgsql从数据库中获取正确的列名? - How can get correct Column Name from Database with Npgsql? 如何将基于用户名的MS Access数据库中的表插入DataGridView? - How to insert a table from an MS Access database based on the username into a DataGridView? 如何从 DataGridView 编辑行? MS Access 数据库 - How to edit rows from DataGridView? MS Access Database
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM