Datagridview图像列在更新

时间:2015-12-14 20:52:03

标签: vb.net datagridview

在我的表单上,我得到了datagridview,它由一些数据填充。此表单上还有一个组合框,当选择更改发生时,则根据所选值 - datagridview数据源设置为空,然后再次填充。一切都运行良好,直到我决定添加额外的datagridview图像列,我放置在网格的末尾,并显示基于一些以前的列文本数据的文本的图像。不幸的是,我有显示图像本身的问题(红色标记),最大的问题是当我更改组合框选择时,我的附加数据网格图像列每次组合更改时向左移动1个位置。我花了一整天时间寻找问题但却无法找到答案。我想也许是因为网格数据源没有被清理(没有),但确实如此。你能帮我解决一下这里可能出现的问题吗?下面找到我的代码和问题的屏幕截图。希望你能帮助我解决这个问题,因为我真的是在堆栈流过。

我的表单加载事件 - 我加载组合框的地方:

 Private Sub FrmTransportView_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        RemoveHandler CboProjects.SelectedIndexChanged, AddressOf CboProjects_SelectedIndexChanged
        Try
            mydb.OpenConn()
            If mydb.conn.State = ConnectionState.Open Then
                Form.InitCombo(CboProjects, "SELECT * from tbProjekt", mydb.conn, "Nazwa", "Id")
                mydb.CloseConn()
                'ChangeControlsLanguage()
                Trans = New Transport
                Trans.ProjectId = CboProjects.SelectedValue
                RefreshGridAfterProjectIdChanged()
                AddHandler CboProjects.SelectedIndexChanged, AddressOf CboProjects_SelectedIndexChanged
End Try
    End Sub

PopulateGrid方法:

Public Sub PopulateGrid()
        Try
            If IsNothing(Trans.ListByProjectId_checkifanyrows()) Then
  Me.BeginInvoke(New MethodInvoker(AddressOf Me.Close))
Else
 Else
                dgvTransport.DataSource = Nothing   'Clean datagrid before new data

                dgvTransport.Refresh()
                dgvTransport.DataSource = Trans.ListByProjectId()
                AlignGrid()
                PlaceImages()
            End If
  End Try
    End Sub

对齐方法 - 放置在PopulateGrid方法中,如您所见dgvTransport设置为nohing,然后将其detasource设置为新数据:Trans.ListByProjectId()现在我正在对齐它 - 隐藏一些列等...正如你所看到的,我正在添加这个新的图像列...:

 Private Sub AlignGrid()
        Try
            dgvTransport.Columns(0).Visible = False         'Project id
            dgvTransport.Columns(1).Visible = False         'Id
            dgvTransport.Columns(2).Visible = True          'Lp                                                      
            dgvTransport.Columns(3).Visible = True          'Status                                                  
            dgvTransport.Columns(4).Visible = True          'Dataprzyjazdu                                           
            dgvTransport.Columns(5).Visible = True          'DataRozpoczeciaZaladunku                                

          dgvTransport.Columns(6).Visible = True          'DataOdjazdu                                             
            dgvTransport.Columns(7).Visible = True          'TypTransportu                                                   dgvTransport.Columns(8).Visible = False          'TypKontenera  (reprezentacja liczbowa z tabeli tbTransport)    
            dgvTransport.Columns(9).Visible = True          'NumerKontenera                                          
            dgvTransport.Columns(10).Visible = True         'NumerCiezarowki                                         
            dgvTransport.Columns(11).Visible = True         'Plomba   
            dgvTransport.Columns(12).Visible = False        'Kierowca
            dgvTransport.Columns(13).Visible = False         'Opis
            dgvTransport.Columns(14).Visible = False        'Nazwa (nazwa projektu)
            dgvTransport.Columns(15).Visible = True        'TypKontenera (reprezentacja za pomoca nazwy ze zlaczenia INNER JOIN))

            Dim img As DataGridViewImageColumn = New DataGridViewImageColumn()
            img.HeaderText = "Status2"
            img.Name = "Status2"
            dgvTransport.Columns.Insert(16, img)

            'potrzebny aby zadzialalo: DgvMach.ColumnHeadersDefaultCellStyle.BackColor = Color.Gold 
            dgvTransport.EnableHeadersVisualStyles = False

            'headers look
            With dgvTransport.ColumnHeadersDefaultCellStyle
                'The way to do this is to set the EnableHeadersVisualStyles flag for the data grid view to False, and set the background colour via the ColumnHeadersDefaultCellStyle.BackColor property. For example, to set the background colour to blue, use the following (or set in the designer if you prefer):
                'If you do not set the EnableHeadersVisualStyles flag to False, then the changes you make to the style of the header will not take effect, as the grid will use the style from the current users default theme. The MSDN documentation for this property is here.
                .BackColor = Color.White

                .ForeColor = Color.Black
                .Font = New Font("Ariel", 10, FontStyle.Regular)
                .Alignment = DataGridViewContentAlignment.MiddleCenter
            End With

            dgvTransport.AllowUserToAddRows = False
            dgvTransport.[ReadOnly] = True
            dgvTransport.MultiSelect = False
            dgvTransport.SelectionMode = DataGridViewSelectionMode.FullRowSelect        'zaznacza caly wiersz po kliknieciu


            dgvTransport.AutoSizeColumnsMode = DataGridViewAutoSizeColumnMode.Fill      'WAZNE!!!: RESIZUJE CALY CONTENT GRIDA NIE ZOSSTAWIAJAC CIEMNEGO TLA !!!
            dgvTransport.AutoSizeRowsMode = DataGridViewAutoSizeRowsMode.AllCells

            With dgvTransport.DefaultCellStyle
                .BackColor = Color.White
                .ForeColor = Color.Black
                .Font = New Font("Ariel", 9, FontStyle.Regular)
                .Alignment = DataGridViewContentAlignment.MiddleCenter
            End With
'This will disable row autosizing and manual row resizing. To set the row height you can use the Height and MinimumHeight properties of the RowTemplate.
            dgvTransport.AutoSizeRowsMode = DataGridViewAutoSizeRowsMode.None
            dgvTransport.AllowUserToResizeRows = False
    End Sub

FrmTransportView_Shown事件:

Private Sub FrmTransportView_Shown(sender As Object, e As EventArgs) Handles MyBase.Shown
        AddHandler dgvTransport.SelectionChanged, AddressOf dgvTransport_SelectionChanged
    End Sub

RefreshGridAfterProjectIdChanged

Public Sub RefreshGridAfterProjectIdChanged()
        Trans.ProjectId = CboProjects.SelectedValue
        PopulateGrid()
    End Sub

CboProjects_SelectedIndexChanged

Private Sub CboProjects_SelectedIndexChanged(sender As Object, e As EventArgs) Handles CboProjects.SelectedIndexChanged
        RefreshGridAfterProjectIdChanged()
    End Sub

PlaceImages

 Private Sub PlaceImages()
            For i As Integer = 0 To dgvTransport.Rows.Count - 1
                Dim sHeader As String = dgvTransport.Columns(16).Name

                If sHeader = "Status2" Then
                    Dim LINK = dgvTransport.Rows(i).Cells(8).Value

                    If LINK.ToString.Contains("1") Then
                        Dim Img As New DataGridViewImageCell
                        Img.Value = My.Resources._1
                        dgvTransport.Rows(i).Cells(16).Value = Img.Value
                    End If
                    If LINK.ToString.Contains("2") Then
                        Dim Img As New DataGridViewImageCell
                        Img.Value = My.Resources._2
                        dgvTransport.Rows(i).Cells(16).Value = Img.Value
                    End If
                    If LINK.ToString.Contains("3") Then
                        Dim Img As New DataGridViewImageCell
                        Img.Value = My.Resources._3
                        dgvTransport.Rows(i).Cells(16).Value = Img.Value
                    End If
                End If
            Next
        End Sub

它是如何查找第一个表单加载:(奇怪的是图像列(Status2)的列索引为0 - 应为16,因为我将该列的索引设置为16在Align方法..正如你所看到的那样,图像不会出现......

enter image description here

现在我要更改组合框中的选择:

当然,您可以从代码中注意到此方法处理程序被调用:CboProjects_SelectedIndexChanged

然后再将网格数据源设置为Nothing,再次填充,然后对齐网格..

以及后面显示的内容:(参见索引 - 也显示一些图像和列#34; Status2"已移至左侧..)

enter image description here

第3组合框改变等......

enter image description here

希望找到一个人来帮助我,因为我真的不知道如何解决这个问题。希望一切都清楚。

1 个答案:

答案 0 :(得分:0)

抱歉这有点晚了。只是一个建议 - 我没有试验它。在编辑其他列的可见性之前,请先尝试插入DataGridImageColumn。

不太相关,但在你的AlignGrid sub中,可以在设计上设置静态属性,如“.BackColor”,只是为了减少代码。

让我们知道您的观察结果。

由于