如何在 vb.net 中手动填充控件数据绑定 datagridview?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17171967/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to manually populate control data binding datagridview in vb.net?
提问by user2059064
I'm new to vb.net. I'm used to do things the old school way (VB6).
我是 vb.net 的新手。我习惯于以老派的方式(VB6)做事。
Is there a way to manually populate a datagridview? In the old way (VB6) I populate a listview by adding columns manually and loop to a recordset to add the rows.
有没有办法手动填充数据网格视图?在旧方式(VB6)中,我通过手动添加列并循环到记录集以添加行来填充列表视图。
For example what if I have this command (storedproc).
例如,如果我有这个命令(storedproc)怎么办。
"Select OrderID, ProductName, Qty, SellingPrice, CustomerName FROM tblOrders".
"Select OrderID, ProductName, Qty, SellingPrice, CustomerName FROM tblOrders".
What if I only need to show OrderID, ProductName and SellingPrice?
如果我只需要显示怎么办 OrderID, ProductName and SellingPrice?
Is there a way to manually add columns OrderID, ProductName, SellingPrice to the datagridview and loop to the result set to add the rows?
有没有办法手动将列 OrderID、ProductName、SellingPrice 添加到 datagridview 并循环到结果集以添加行?
Thanks in advance.
提前致谢。
回答by matzone
To add column to DataTable
将列添加到数据表
Dim MyTable as New DataTable
Dim dc As DataColumn
dc = New DataColumn
dc.DataType = System.Type.GetType("System.String")
dc.ColumnName = "OrderID"
MyTable.Columns.Add(dc)
dc = New DataColumn
dc.DataType = System.Type.GetType("System.String")
dc.ColumnName = "ProductName"
MyTable.Columns.Add(dc)
dc = New DataColumn
dc.DataType = System.Type.GetType("System.String")
dc.ColumnName = "SellingPrice"
MyTable.Columns.Add(dc)
So, you can set MyTable as Datasource
因此,您可以将 MyTable 设置为数据源
DataGridview1.DataSource = MyTable
To add rows .. http://msdn.microsoft.com/en-us/library/5ycd1034(v=vs.80).aspx
添加行.. http://msdn.microsoft.com/en-us/library/5ycd1034(v=vs.80).aspx
回答by justanotheruser
I have the strange solution. If your query will never changes (i.e first column is forever "OrderID", second is "ProductName" etc.) you could hide unwanted columns.
我有奇怪的解决方案。如果您的查询永远不会改变(即第一列永远是“OrderID”,第二列永远是“ProductName”等),您可以隐藏不需要的列。
DataGridView1.Columns(2).Visible = False 'hiding 3rd column
DataGridView1.Columns(4).Visible = False 'hiding 5th column
回答by user2059064
I got this code from a book. This code is close enough. At least I can configure the format and alignment of a Column.
我从一本书上得到了这个代码。这段代码足够接近了。至少我可以配置列的格式和对齐方式。
Imports System.Data
Imports System.Data.SqlClient
Imports System.Configuration
Public Class Form1
Dim objConnection As New SqlConnection(ConfigurationManager.ConnectionStrings("MyDatabase").ConnectionString)
Dim objDataAdapter As New SqlDataAdapter()
Dim objDataSet As New DataSet()
Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
objDataAdapter.SelectCommand = New SqlCommand()
objDataAdapter.SelectCommand.Connection = objConnection
objDataAdapter.SelectCommand.CommandText = "SELECT authors.au_lname, authors.au_fname, titles.title, titles.price FROM authors INNER JOIN titleauthor ON authors.au_id = titleauthor.au_id INNER JOIN titles ON titleauthor.title_id = titles.title_id ORDER BY authors.au_lname, authors.au_fname"
objDataAdapter.SelectCommand.CommandType = CommandType.Text
objConnection.Open()
objDataAdapter.Fill(objDataSet, "authors")
objConnection.Close()
grdAuthorsTitles.AutoGenerateColumns = True
grdAuthorsTitles.DataSource = objDataSet
grdAuthorsTitles.DataMember = "authors"
Dim objAlignRightCellStyle As New DataGridViewCellStyle
objAlignRightCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight
Dim objAlernatingCellStyle As New DataGridViewCellStyle()
objAlernatingCellStyle.BackColor = Color.WhiteSmoke
grdAuthorsTitles.AlternatingRowsDefaultCellStyle = objAlernatingCellStyle
Dim objCurrencyCellStyle As New DataGridViewCellStyle()
objCurrencyCellStyle.Format = "c"
objCurrencyCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight
grdAuthorsTitles.Columns(0).HeaderText = "Last Name"
grdAuthorsTitles.Columns(1).HeaderText = "First Name"
grdAuthorsTitles.Columns(2).HeaderText = "Book Title"
grdAuthorsTitles.Columns(2).Width = 225
grdAuthorsTitles.Columns("price").HeaderCell.Value = "Retail Price"
grdAuthorsTitles.Columns("price").HeaderCell.Style = objAlignRightCellStyle
grdAuthorsTitles.Columns("price").DefaultCellStyle = objCurrencyCellStyle
objDataAdapter = Nothing
objConnection = Nothing
objCurrencyCellStyle = Nothing
objAlignRightCellStyle = Nothing
objAlernatingCellStyle = Nothing
End Sub
End Class

