質問

Greeting everyone, I have a question regarding drawing row numbers in ListView like I did in DataGridView. I'm using Visual Studio 2010.

In my DataGridView, I draw the row number using this code :

Private Sub SalesDGV_RowPostPaint(ByVal sender As Object, ByVal e As DataGridViewRowPostPaintEventArgs) Handles SalesDGV.RowPostPaint
    'draw the row number
    Using b As SolidBrush = New SolidBrush(SalesDGV.RowHeadersDefaultCellStyle.ForeColor)
        e.Graphics.DrawString(e.RowIndex + 1, _
                               SalesDGV.DefaultCellStyle.Font, _
                               b, _
                               e.RowBounds.Location.X + 20, _
                               e.RowBounds.Location.Y + 4)
    End Using
End Sub

And the result in my DataGridView: http://puu.sh/3h4CF.png

I want my ListView row numbers have the exact same style as my DataGridView, but it seem that ListView doesn't support such style in default and I need a custom control (?)

I know some people will suggest me to use back DataGridView instead, but the reason I use ListView instead is that because DataGridView will give me void area if I don't populate data, while ListView allow me to have GridLines without populating any data.

So I have two question here :

  • How do I draw row headers with numbers in ListView like I did in DataGridView?
  • Can I have gridlines display in DataGridView even through I don't populate any data? If possible, how can I achieve it?

Thanks!

役に立ちましたか?

解決

Can I have gridlines display in DataGridView even through I don't populate any data? If possible, how can I achieve it?

If you populate a datatable with spaces and use that as a datasource your gridview will have gridlines but appear to have no data.

Something like this:

    Dim dt As New DataTable
    'Column to hold row numbers
    dt.Columns.Add(" ")
    'Number of columns
    For I = 1 To 5
        dt.Columns.Add(I.ToString)
    Next
    'Number of rows
    For I = 1 To 10
        'Extra column holds the row number
        dt.Rows.Add({I.ToString," ", " ", " ", " ", " "})
    Next
    SalesDGV.DataSource = dt

how do I insert it so that the gridlines is still there and that the data is insert in row accordingly?

put the data into the datatable. Just check for null data and make it a space instead. If you're adding the data from a database, one way is to loop through the columns and rows of the datatable looking for empty data and changing it to a space. If the data is from a file you probably already have a loop that you can use to modify the data.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top