【问题标题】:I'm looking for a way to make my first column in my datagridview a header我正在寻找一种方法让我的 datagridview 中的第一列成为标题
【发布时间】:2013-06-13 15:41:19
【问题描述】:

好的,所以我有一个数据集,它从 excel 文件中提取特定单元格并填充 datagridview 列。但是,我要拉的单元格确实需要是标题,而不是 datagridview 中的普通列。那么有没有一种简单的方法可以将这列数据转换为标题文本?为了帮助说明,我在下面提供了一些代码,包括 cmets。

' The following lines specify the exact cells I with to pull from the excel file and populates the first column of the datagridview

MyCommand1 = New OleDbDataAdapter("Select * from [myWorksheet$A15:B21]", MyConnection)


'Here is my dataset'
    ds1 = New System.Data.DataSet()
DataGridView1.DataSource = ds1.Tables(0).DefaultView
'So at this point I have a datagridview with a column of data from the exact cells 
' from the excel file that I want


'This last part is code I found on MSDN which will hide the column headers and will turn the first column into headertext. Essentially it is adding an additional column to the left and turning that into headertext. 

Private Sub DataGridView8_CellPainting(sender As Object, e As DataGridViewCellPaintingEventArgs) Handles DataGridView8.CellPainting
    Dim rowNumber As Integer = 1

    For Each row As DataGridViewRow In DataGridView8.Rows
        If row.IsNewRow Then Continue For
        row.HeaderCell.Value = "Row " & rowNumber
        rowNumber = rowNumber + 1
    Next
    DataGridView8.AutoResizeRowHeadersWidth( _
        DataGridViewRowHeadersWidthSizeMode.AutoSizeToAllHeaders)
End Sub

'If anyone can find out a way for me to make the first column headertext it would make my day. 

【问题讨论】:

  • 喜欢 switch col1 作为标题, col2 作为第一行?
  • 是的,这正是我想要做的。
  • 我不太确定,但我认为它需要两个步骤..将列转移到表格中的行..然后显示它..
  • 知道怎么做吗?
  • 你试过我的答案了吗?

标签: vb.net datagridview headertext


【解决方案1】:

只是一个想法..

Dim tblXls as DataTable = ds1.Tables(0) '---> this is your table
Dim tblNew as New Datatable
Dim dc as DataColumn

For x as Integer = 0 to tblXls.Rows.Count -1
    dc = New DataColumn
    dc.DataType = System.Type.GetType("System.DateTime")
    dc.ColumnName = tblXls.Rows(x).Item(0)
    tblNew.Columns.Add(dc)
Next

然后将 tblNew 设置为您的 DGV 数据源 ..

DataGridView.Datasource = tblNew

这只是准备标题..你知道其余的..

【讨论】:

    【解决方案2】:

    不太确定,但更改您的连接字符串可能会为您完成这项工作。

    connString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Test.xlsx;Extended Properties=" + Convert.ToChar(34).ToString() + "Excel 12.0;HDR=Yes;IMEX=2" + Convert.ToChar(34).ToString() + ""
    

    在上面的代码中HDR 表示标题。设置HDR = Yes 将第一行显示为标题,设置HDR = No 将第一行显示为普通行。

    您可以获取更多信息Get Data from MS Excel in asp.net

    【讨论】:

      【解决方案3】:

      您可以在 CellFormatting 事件中填充行标题:

      Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
          Dim SQL As String = "SELECT [ZIP], [Place] FROM [City_2]"
          dgv1.DataSource = Gen.GetDataView(SQL) ' load the datasource here
          dgv1.AutoResizeColumns(DataGridViewAutoSizeColumnsMode.AllCells)
          dgv1.Columns(0).Visible = False ' contains same as row header
          dgv1.RowHeadersWidthSizeMode = DataGridViewRowHeadersWidthSizeMode.AutoSizeToAllHeaders
      End Sub
      
      Private Sub dgv1_CellFormatting(sender As Object, e As System.Windows.Forms.DataGridViewCellFormattingEventArgs) Handles dgv1.CellFormatting
          dgv1.Rows(e.RowIndex).HeaderCell.Value = dgv1.Rows(e.RowIndex).Cells(0).Value
      End Sub
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-11-08
        • 1970-01-01
        • 2020-03-27
        • 2015-07-24
        • 2023-03-28
        相关资源
        最近更新 更多