1

我是 vb.net 的新手。我习惯用老派的方式做事(VB6)。

有没有办法手动填充datagridview?在旧方式(VB6)中,我通过手动添加列来填充列表视图并循环到记录集以添加行。

例如,如果我有这个命令(storedproc)怎么办。

"Select OrderID, ProductName, Qty, SellingPrice, CustomerName FROM tblOrders".

如果我只需要显示怎么办OrderID, ProductName and SellingPrice?

有没有办法手动将列 OrderID、ProductName、SellingPrice 添加到 datagridview 并循环到结果集以添加行?

提前致谢。

4

3 回答 3

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

于 2013-06-18T14:59:24.870 回答
0

我有奇怪的解决方案。如果您的查询永远不会改变(即第一列永远是“OrderID”,第二列是“ProductName”等),您可以隐藏不需要的列。

DataGridView1.Columns(2).Visible = False       'hiding 3rd column
DataGridView1.Columns(4).Visible = False       'hiding 5th column
于 2013-06-18T16:21:36.280 回答
0

我从一本书中得到了这个代码。这段代码足够接近。至少我可以配置列的格式和对齐方式。

    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
于 2013-06-18T17:26:52.403 回答