[RESOLVED] selected Datagridview rows in one form to another form
Hello all,
I am new to this forum and also to vb so pl. excuse me if i do any mistakes...
I am doing a project in window application (VB 2008) where I have 2 forms (form1 & form2) in form1 I place a empty DGV and a button, and in form2 i placed another DGV which i connects to MS access DB, in form2 DGV I add a checkbox column and i need to do is if i click the button in form1 it should show the form2 DGV (till this i got it) and when i checked the rows in form2 DGV the checked rows should shown in form1 DGV. and i also want to add the sum in bottom. pl... pl... guide me.....
Re: selected Datagridview rows in one form to another form
Originally Posted by make me rain
Check this & reply
Pl. find my application in attachment...
This what i required actually... sorry I cant able to explain what i need, so i am attaching my application itself... Please help me to complete this task... And thanks for your quick reply and I also trying your code in my appln...
I have no word to thank u and also this forums people they are doing excellent contribution to people like me.
Last edited by tnncprojects; Oct 17th, 2011 at 02:23 AM.
Re: selected Datagridview rows in one form to another form
Originally Posted by make me rain
see this & reply
You solved my task..... Great
I have no words to express my happiness... U make me happiest person in this world and i don't know how to appreciate your work and I am very very small person in front of you... "Make me rain" you "made me to showers in my work"..... Thanks Thanks Thanks... nth times thanks.....
and I will continue to ask my doubts to u... please don't get hesitate I know you don't....
Re: selected Datagridview rows in one form to another form
Originally Posted by tnncprojects
Hello all,
I am new to this forum and also to vb so pl. excuse me if i do any mistakes...
I am doing a project in window application (VB 2008) where I have 2 forms (form1 & form2) in form1 I place a empty DGV and a button, and in form2 i placed another DGV which i connects to MS access DB, in form2 DGV I add a checkbox column and i need to do is if i click the button in form1 it should show the form2 DGV (till this i got it) and when i checked the rows in form2 DGV the checked rows should shown in form1 DGV. and i also want to add the sum in bottom. pl... pl... guide me.....
Re: selected Datagridview rows in one form to another form
You will need to loop through the rows, check the value of the checkbox and if true then you can add to your sum and also add to the grid or create a datatable add a row to that then bind to the grid.
Re: selected Datagridview rows in one form to another form
Here is a full demo where both DataGridView controls are on the same form so for your project simple adapt to referening the controls.
Two methods are shown, first by CheckBox column checked using the following extension method to get checked rows.
Code:
<Runtime.CompilerServices.Extension()> _
Public Function GetChecked( _
ByVal sender As DataGridView, _
ByVal ColumnName As String) As List(Of DataGridViewRow)
Return (From Rows In sender.Rows.Cast(Of DataGridViewRow)() _
Where CBool(Rows.Cells(ColumnName).Value) = True).ToList
End Function
If you only want a count of checked (by column index but you can overload this function to reference by column name)
Code:
<Runtime.CompilerServices.Extension()> _
Public Function CheckBoxCount( _
ByVal sender As DataGridView, _
ByVal ColumnIndex As Integer, _
ByVal Checked As Boolean) As Integer
Return (From Rows In sender.Rows.Cast(Of DataGridViewRow)() _
Where CBool(Rows.Cells(ColumnIndex).Value) = Checked).Count
End Function
The following is not needed if you have columns defined in the target DataGridView.
Code:
DataGridView2.CloneColumns(DataGridView1)
Since I dynamcially add columns in the target DataGridView it adds the checkbox column, I hide it in the event it is not needed or you could have an overloaded Clone function to not include a specific column or columns.
The following adds the checked rows in the source DataGridView to the target DataGridView (first the rows are cleared which is optional)
Code:
DataGridView2.Rows.Clear()
' If this is not done it is possible to miss a row as it has not been updated
DataGridView1.EndEdit()
Dim CheckedRows = DataGridView1.GetChecked(CheckBoxColumnName).ToArray
For Each row As DataGridViewRow In CheckedRows
row.CloneRowWithValues(DataGridView2)
Next
Re: selected Datagridview rows in one form to another form
pl. explain this line "Me.DataGridView1.DataSource = Dt" and what Dt means, FYI... I am using MS access as database to view rows in form2 DGV and in form1 DGV it is empty.
Re: selected Datagridview rows in one form to another form
Originally Posted by tnncprojects
pl. explain this line "Me.DataGridView1.DataSource = Dt" and what Dt means, FYI... I am using MS access as database to view rows in form2 DGV and in form1 DGV it is empty.
Me.DataGridView1.DataSource = Dt, that is not from my code.
In regards to your project loading data from MS-Access, no problem, simply alter my form code and code modules as shown below. Note I use a BindingSource for obtaining row position of a Checked Row. I always ended up using a BindingSource as it makes life easier when working with data, at least for me.
Form code
Code:
Public Class Form1
Private CheckBoxColumnName As String = "SelectedColumn"
WithEvents bsData As New BindingSource
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim FirstNameColumn As New DataGridViewTextBoxColumn With {.HeaderText = "First", .Name = "FirstName", .DataPropertyName = "FirstName"}
Dim LastNameColumn As New DataGridViewTextBoxColumn With {.HeaderText = "Last", .Name = "LastName", .DataPropertyName = "LadtName"}
Dim CheckedColumn As New DataGridViewCheckBoxColumn With {.HeaderText = "Select", .Name = CheckBoxColumnName, .DataPropertyName = "Process"}
DataGridView1.Columns.AddRange(New DataGridViewColumn() {CheckedColumn, FirstNameColumn, LastNameColumn})
Dim Data = MimicLoadingFromDatabase()
bsData.DataSource = Data
DataGridView1.DataSource = bsData
Dim CloneStructure = Data.Clone
DataGridView2.DataSource = CloneStructure
DataGridView2.Columns(0).Visible = False
End Sub
Private Function MimicLoadingFromDatabase() As DataTable
Dim dt As New DataTable With {.TableName = "Jobs"}
With dt.Columns
.AddRange(New DataColumn() _
{ _
New DataColumn("Process", GetType(System.Boolean)), _
New DataColumn("FirstName", GetType(System.String)), _
New DataColumn("LastName", GetType(System.String)) _
} _
)
End With
dt.Rows.Add(New Object() {False, "John", "Smith"})
dt.Rows.Add(New Object() {True, "Mary", "Doe"})
dt.Rows.Add(New Object() {True, "Piper", "Wittman"})
dt.Rows.Add(New Object() {True, "Tom", "Wittman"})
Return dt
End Function
Private Sub AllCheckedToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles AllCheckedToolStripMenuItem.Click
DataGridView1.EndEdit()
Dim TargetTable = DirectCast(DataGridView2.DataSource, DataTable)
TargetTable.Rows.Clear()
Dim CheckedRows = DataGridView1.GetChecked(CheckBoxColumnName).ToArray
For Each row As DataGridViewRow In CheckedRows
TargetTable.ImportRow(bsData.ItemRow(row.Index))
Next
End Sub
Private Sub CloseDemoToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CloseDemoToolStripMenuItem.Click
Close()
End Sub
End Class
Module code
Code:
Module Module1
<Runtime.CompilerServices.Extension()> _
Public Function GetChecked(ByVal sender As DataGridView, ByVal ColumnName As String) As List(Of DataGridViewRow)
Return (From Rows In sender.Rows.Cast(Of DataGridViewRow)() _
Where CBool(Rows.Cells(ColumnName).Value) = True).ToList
End Function
<Runtime.CompilerServices.Extension()> _
Public Function CheckBoxCount(ByVal sender As DataGridView, ByVal ColumnIndex As Integer, ByVal Checked As Boolean) As Integer
Return (From Rows In sender.Rows.Cast(Of DataGridViewRow)() Where CBool(Rows.Cells(ColumnIndex).Value) = Checked).Count
End Function
<System.Diagnostics.DebuggerStepThrough()> _
<System.Runtime.CompilerServices.Extension()> _
Public Function ItemRow(ByVal sender As BindingSource, ByVal Index As Integer) As DataRow
Return DirectCast(sender.Item(Index), DataRowView).Row
End Function
End Module