none
想请教使用array的方法控制datagridview column 宽度。 RRS feed

  • 问题

  •  

    我篇写了有关代码控制datagridview column 宽度的代码。我想请教如何使用array的方法控制datagridview column 宽度。

    Dim PRODUCT_PICTURE As DataGridViewColumn = dgv1.Columns("PRODUCT_PICTURE")
            PRODUCT_PICTURE.Width = 120

            Dim PRODUCT_ID As DataGridViewColumn = dgv1.Columns("PRODUCT_ID")
            PRODUCT_ID.Width = 110

            Dim FA_NO As DataGridViewColumn = dgv1.Columns("FILE_NAME")
            FILE_NAME.Width = 110

            Dim CURRENT_YEAR As DataGridViewColumn = dgv1.Columns("CURRENT_YEAR")
            CURRENT_YEAR.Width = 100

            Dim PICTURE_LOCATION1 As DataGridViewColumn = dgv1.Columns("PICTURE_LOCATION1")
            PICTURE_LOCATION1.Width = 125

            Dim DESCRIPTION As DataGridViewColumn = dgv1.Columns("DESCRIPTION")
            DESCRIPTION.Width = 300

    2020年7月7日 7:34

答案

  • Hi christing,

    修改 DataGridView 的 AutoSizeColumnsMode 以后:

    DataGridView1.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.None

    尝试以下代码(用 Dictionary 存储数据):

        Dim dic As Dictionary(Of String, Integer) = New Dictionary(Of String, Integer)()
        dic.Add("PRODUCT_PICTURE", 120)
        dic.Add("PRODUCT_PICTURE1", 110)
        dic.Add("FILE_NAME", 110)
        '...
    
        For i As Integer = 0 To dataGridView1.Columns.Count - 1
            Dim s As Integer = dic.Where(Function(x) x.Key = dataGridView1.Columns(i).Name).[Select](Function(y) y.Value).FirstOrDefault()
            dataGridView1.Columns(i).Width = s
        Next

    希望可以解决问题。

    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年7月8日 2:33
    2020年7月7日 9:50
    版主