[英]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.