how to pass a value to datagrid view? [Resolved]

Posted by Vforvijay under VB.NET on 5/3/2011 | Points: 10 | Views : 3408 | Status : [Member] | Replies : 3
hi. i have created two form...In form 1 i added datagridview ,when i press function key in current cell form 2 will open now i need to pass a string value to data grid current cell from Form 2..
how can i achieve this.........




Responses

Posted by: Ndebata on: 5/3/2011 [Member] Starter | Points: 50

Up
0
Down

Resolved
Hi

Sorry to say i don't have VB.Net installed on my machine, Please open the .cs file of both form1 and form2 you will get it, it is very simple.
If you have any concern just let me know.

Hey I have converted the source code
Code behind file for Form1 in VB.Net

Namespace PassValue

Public Class Form1
Inherits Form

Public Sub New()
MyBase.New
InitializeComponent
End Sub

Private Sub Form1_Load(ByVal sender As Object, ByVal e As EventArgs)
'add some raw data to data grid
Dim dt As DataTable = New DataTable
dt.Columns.Add("Column1")
dt.Columns.Add("Column2")
dt.Columns.Add("Column3")
dt.Rows.Add(New Object() {1, 2, 3})
dt.Rows.Add(New Object() {2, 3, 4})
dt.Rows.Add(New Object() {4, 7, 9})
dt.Rows.Add(New Object() {9, 4, 10})
'
dg.DataSource = dt
End Sub

Private Sub dg_KeyDown(ByVal sender As Object, ByVal e As KeyEventArgs)
If (e.KeyValue = 113) Then
Dim f2 As Form2 = New Form2
f2.ValuePassed = (f2.ValuePassed + New PassStringDelegate(f2_ValuePassed))
f2.ShowDialog
End If
End Sub

Private Sub f2_ValuePassed(ByVal cellvalue As String)
If (Not (dg.CurrentCell) Is Nothing) Then
dg.CurrentCell.Value = cellvalue
End If
End Sub
End Class
End Namespace

That of Form2 in VB.Net


Namespace PassValue

Public Delegate Sub PassStringDelegate(ByVal cellvalue As String)

Public Class Form2
Inherits Form

Public Sub New()
MyBase.New
InitializeComponent
End Sub

Public Event ValuePassed As PassStringDelegate

Private Sub btnOK_Click(ByVal sender As Object, ByVal e As EventArgs)
If (ValuePassed Is Nothing) Then
Throw New ArgumentNullException("Event not registered")
Else
ValuePassed(textBox1.Text)
Me.Close
End If
End Sub
End Class
End Namespace



Thanks,
Debata

Vforvijay, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Ndebata on: 5/3/2011 [Member] Starter | Points: 25

Up
0
Down
Hi

You can add an event in the page from which you want to pass the data and register the same in the form where you are recieving the data and update it.
Take a look at the attcahed sample.

Thanks,
Debata
 Download source file

Vforvijay, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Vforvijay on: 5/3/2011 [Member] Starter | Points: 25

Up
0
Down
hi..thanks for your source..but i need code in vb ...is it possible plz send me vb code

Vforvijay, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response