Click here to Skip to main content
15,899,679 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
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..
VB
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


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

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

The easiest way is to add a handler to the first form referencing button1 of the second form.

VB
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
 
Share this answer
 
I used Delegate To solve this problem..
This is code for Child Form

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
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900