none
想请教如何确保datagridview每insert的data都是一size的宽和高 RRS feed

  • 问题

  • 我正在篇写有关datagridview 长跟宽的program以下是我的代码。我想请教如何确保datagridview每insert的data都是一的宽和高

       Dim column As DataGridViewColumn = dgv1.Columns(0)
            column.Width = 50
            Dim row As DataGridViewRow = dgv1.Rows(0)
            row.Height = 50

    2020年6月3日 5:58

答案

  • Hi christing,

    为了正确设置 datagridview 的宽和高,你可以使用 DataGridView.RowTemplate 属性和 DataGridView.RowsAdded 事件来完成。

    我做了一个测试,你可以参考以下测试内容:

        Private dt As DataTable = New DataTable()
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            dt.Columns.Add("id")
            dt.Columns.Add("name")
            dt.Columns.Add("age")
            dt.Rows.Add(1, "A", 20)
            dt.Rows.Add(2, "B", 21)
            dt.Rows.Add(3, "C", 21)
        End Sub
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            DataGridView1.RowTemplate.Height = 50
            DataGridView1.DataSource = dt
        End Sub
    
        Private Sub DataGridView1_ColumnAdded(sender As Object, e As DataGridViewColumnEventArgs) Handles DataGridView1.ColumnAdded
            e.Column.Width = 50
        End Sub

    测试结果:

    Best Regards,

    Xingyu Zhao


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • 已标记为答案 christing 2020年6月4日 3:29
    2020年6月4日 3:16
    版主