This blog targets those developers who are kind of NewBie in the .NET domain. Even though the implementation is fairly easy, I often find people asking in various forums on how to pass the data between two forms. And even for our support queries, occasionally we do come across similar questions, where user wants to know how to transfer row data between two grids which are present in two different forms. To make things easier for our customers who are just starting up on the .NET knowledge along with our controls, this blog will demonstrate the transfer of Row Data between two C1TrueDBGrid controls from different forms. In this example, I am considering a Main Form with C1TrueDBGrid1 which has to be filled with rows from Data Form which contains another C1TrueDBGrid2 loaded with data. On double clicking any of the rows in C1TrueDBGrid2, data is shifted to the grid in Main Form. So whats the basic trick to send a data from one Form to another form. General solution is to expose a Property for the form and access them in the second form. But in this case, we can simply pass the reference to the C1TrueDBGrid in the constructor call for Data Form. Lets see the implementation. C1TrueDBGrid in Main Form is in Unbound mode. To fill the data in Unbound mode, we have to define the columns. You can do so in Designer or you can add Columns through code as well. You can refer to the documentation for runtime creation of columns in C1TrueDBGrid. To add data to this grid, we have the Data Form which opens on a button click event. Add a Button to Main Form and use the following code in its click event.
Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click Dim chFrm As New DataForm(C1TrueDBGrid1) chFrm.ShowDialog() End Sub
Now in the above code, you would observe that we have passed a reference to C1TrueDBGrid1 in the constructor of DataForm. This provides the access to the grid in DataForm where we can directly append the data. Given code snippet is for DataForm which also implements the DoubleClick event for C1TrueDbGrid in DataForm.
Public Class DataForm Dim c1Tdbgrid As C1.Win.C1TrueDBGrid.C1TrueDBGrid Public Sub New(ByVal c1TDB As C1.Win.C1TrueDBGrid.C1TrueDBGrid) InitializeComponent() c1Tdbgrid = c1TDB C1TrueDBGrid1.DataSource = <set the Datasource> End Sub Private Sub C1TrueDBGrid1_MouseDoubleClick(sender As Object, e As System.Windows.Forms.MouseEventArgs) Handles C1TrueDBGrid1.MouseDoubleClick Dim dr As System.Data.DataRowView = CType(Me.C1TrueDBGrid1(Me.C1TrueDBGrid1.Row), System.Data.DataRowView) Dim rowValues As New StringBuilder For i As Integer = 0 To dr.Row.ItemArray().ToList().Count - 1 rowValues.Append(dr.Row.Item(i).ToString()) If (i < dr.Row.ItemArray.ToList().Count - 1) Then rowValues.Append(";") End If Next c1Tdbgrid.AddRow(rowValues.ToString()) End Sub End Class
This approach can also be used for another ComponentOne grid i.e. C1FlexGrid. In fact, this is a generic approach applicable to any Winforms .NET control. Download the attached samples for complete implementation. Download Sample C# Download Sample VB