我是vb.net的新手。我习惯用旧学校的方式做事(VB6)。
有没有办法手动填充数据网格视图?在旧的方式(VB6)中,我通过手动添加列来填充列表视图并循环到记录集以添加行。
例如,如果我有这个命令(storedproc)。
"Select OrderID, ProductName, Qty, SellingPrice, CustomerName FROM tblOrders".
如果我只需要显示OrderID, ProductName and SellingPrice?
有没有办法手动将OrderID,ProductName,SellingPrice列添加到datagridview并循环到结果集以添加行?
提前致谢。
答案 0 :(得分:2)
将列添加到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)
因此,您可以将MyTable设置为Datasource
DataGridview1.DataSource = MyTable
添加行.. http://msdn.microsoft.com/en-us/library/5ycd1034(v=vs.80).aspx
答案 1 :(得分:0)
我有一个奇怪的解决方案。如果您的查询永远不会更改(即第一列永远是“OrderID”,第二列是“ProductName”等),您可以隐藏不需要的列。
DataGridView1.Columns(2).Visible = False 'hiding 3rd column
DataGridView1.Columns(4).Visible = False 'hiding 5th column
答案 2 :(得分:0)
我从一本书中得到了这个代码。这段代码足够接近。至少我可以配置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