本文介绍了我如何从另一个子窗口更改数据网格行的颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从另一个子窗口更改数据网格行的颜色,该子窗口在单击数据网格行的选择按钮时打开..
我该怎么办..

i want to change the data grid row color from another child window which is open on click of select button of data grid row..
how can i do this..

Private Sub dgv_CellContentClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles dgv.CellContentClick
    If e.ColumnIndex = 0 Then
           Dim frm As New Form2
           frm.ShowDialog()
    End If
 End Sub



而form2是子winodw



and form2 is child winodw

Private Sub BtnChangeColor_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
       ''Here i want to change the row color of selected row of data grid in form1
   End Sub


一旦我从form2中选择了颜色,立即form1数据网格中选定的行就会发生变化..


as soon as i select the color from form2 immediately form1 data grid selected row should change..

推荐答案

Private Sub dgv_CellContentClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles dgv.CellContentClick
    If e.ColumnIndex = 0 Then
        Dim frm As New Form2
        AddHandler frm.Button1.Click, AddressOf ChangeColor
        frm.ShowDialog()
    End If
End Sub

Public Sub ChangeColor(sender As Object, e As EventArgs)
    '' Add your color changing code here
End Sub


Public Delegate Sub ChangeColorDelegate(ByVal item As String)
Public Class Form2
    
''Declare delagete callback function, the owner of communication
    Public rowChangeColor As ChangeColorDelegate
  
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        ''Notification subscribers
        rowChangeColor(txtItem.Text)
    End Sub
End Class




这是要更改其行颜色的父表单"中的代码




This is Code from Parent Form of which row color want to change

Dim a As Integer
   Private Sub dgv_CellContentClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles dgv.CellContentClick
       If e.ColumnIndex = 0 Then
           Dim frm As New Form2
           a = e.RowIndex
           frm.rowChangeColor = New ChangeColorDelegate(AddressOf Me.ChangeRowBackColorFn)
           frm.ShowDialog()

       End If
   End Sub
   Private Sub ChangeRowBackColorFn(ByVal color As String)
       If color = "Pink" Then
           dgv.Rows(a).DefaultCellStyle.BackColor = System.Drawing.Color.Pink
       Else
           dgv.Rows(a).DefaultCellStyle.BackColor = System.Drawing.Color.Yellow
       End If
   End Sub


这篇关于我如何从另一个子窗口更改数据网格行的颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-20 22:37