-
Nov 29th, 2014, 08:24 AM
#1
Thread Starter
Member
Gridview Data
Hi,
I have 2 datagridview.
DataGridView1 in Form1 and DataGridView2 in Form2.
In DataGridView1 in Form1 i have data and one column contains Checkbox.
What i want is when i click the checkbox in DataGridView1 in Form1 and go to Form2 and press on a button to bring only the data in DataGridView2 which i already checked in DataGridView1.
Thanks.
-
Nov 29th, 2014, 09:31 AM
#2
Re: Gridview Data
Hi,
You can do this this by checking which column has been clicked on the DataGridView and if it’s the CheckBox column in question then you can open another Form and pass the current row of the DataGridView to that form as a Parameter. You do this by creating a Constructor in the second form that can accept the incoming parameter and then its up to you to decide what you want do with it.
As an example:-
Here is some Form2 that accepts an incoming DataGridViewRow as an incoming parameter
vb.net Code:
Public Class Form2 Private Property _CurrentRow As DataGridViewRow Public Sub New() InitializeComponent() End Sub Public Sub New(ByVal currentDGVRow As DataGridViewRow) InitializeComponent() 'When the Form is Instantiated take the incoming parameter and assign it to a Private Property of this Form _CurrentRow = currentDGVRow End Sub Private Sub Form2_Load(sender As Object, e As EventArgs) Handles Me.Load 'Now you can do anything you want with the Current DataGridViewRow that has been passed to the Form MsgBox(_CurrentRow.Cells(1).Value) End Sub End Class
And to call this Form with the Current Row of the DataGridView you can use the CellContentClick Event of the DataGridView.
vb.net Code:
Private Sub SomeDataGridView_CellContentClick(sender As Object, e As DataGridViewCellEventArgs) Handles SomeDataGridView.CellContentClick 'Check which column is being clicked first and if it’s the CheckBox Column then convert the current cell 'to a Boolean Type and See if has been checked. If so, then call Form2 and pass the CurrentRow of the 'DataGridView as a parameter to the Form If e.ColumnIndex = 0 AndAlso CBool(SomeDataGridView.CurrentCell.EditedFormattedValue) Then Using myDetailForm As New Form2(SomeDataGridView.CurrentRow) myDetailForm.ShowDialog() End Using End If End Sub
Hope that helps.
Cheers,
Ian
-
Nov 29th, 2014, 09:57 AM
#3
Re: Gridview Data
Hello,
Note: I can not give you an example in VS2010, only have VS2012-VS2013
The following loads data from a Text file into a DataTable, a BindingSource.DataSource is set to the DataTable, a DataGridView.DataSource is set to the BindingSource on the main form. The child form has a DataGridView with no columns and no code in the child form.
People.txt needs to be in the bin\debug folder
Code:
Aliya,Prix
Anika,Sablon
Alan,Parkes
Emilie,Berger
Amari,Castellanos
Melinda,MacNamara
Matthew,Naik
Annabel,Dresel
Kelvin,Davison
Charlie,Braithwaithe
Marcella,Timmins
Maximo,Hubble
Riley,Bull
Syed,Marcks
Moriah,Paul
Ari,Lindemann
Sydney,Abraham
Kaylee,Stam
Jeffrey,Evans
Cyrus,Erikson
Maritza,Bale
Jonatan,Plum
Kaylah,Hall
Mia,Friend
Alana,Erdon
Code:
Public Class Form1
WithEvents bsPeople As New BindingSource
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Dim dt =
(From line In IO.File.ReadAllLines("People.txt")
Where line.Length > 0
Let Items = line.Split(","c)
Select New With
{
.FirstName = Items(0),
.LastName = Items(1)
}
).ToDataTable
dt.Columns.Add(New DataColumn("ID", GetType(System.Int32)))
dt.Columns.Add(New DataColumn("Process", GetType(System.Boolean)))
For x As Integer = 0 To dt.Rows.Count - 1
dt.Rows(x).SetField(Of Integer)("ID", x + 1)
dt.Rows(x).SetField(Of Boolean)("Process", False)
Next
Dim IdentifierColumn As New DataGridViewTextBoxColumn With
{
.DataPropertyName = "ID",
.Name = "IdentifierCol",
.HeaderText = "ID",
.Visible = False
}
Dim ProcessColumn As New DataGridViewCheckBoxColumn With
{
.Name = "ProcessCol",
.DataPropertyName = "Process",
.HeaderText = "Process"
}
Dim FirstNameColumn As New DataGridViewTextBoxColumn With
{
.DataPropertyName = "FirstName",
.Name = "FirstNameCol",
.HeaderText = "First Name"
}
Dim LastNameColumn As New DataGridViewTextBoxColumn With
{
.DataPropertyName = "LastName",
.Name = "LastNameCol",
.HeaderText = "Last Name"
}
DataGridView1.Columns.AddRange(
New DataGridViewColumn() {IdentifierColumn, ProcessColumn, FirstNameColumn, LastNameColumn})
bsPeople.DataSource = dt
bsPeople.Sort = "LastName"
DataGridView1.DataSource = bsPeople
bsPeople.Position = bsPeople.Find("ID", 4)
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
If DataGridView1.HasCheckedRows("ProcessCol") Then
Dim CurrentCellData = DataGridView1.GetChecked("ProcessCol")
Dim f As New ChildForm
Try
f.DataGridView1.CloneColumns(DataGridView1, "ProcessCol")
If bsPeople.CurrentRowIsValid Then
DataGridView1.CurrentRow.CloneRowWithValues(f.DataGridView1)
f.ShowDialog()
End If
Finally
f.Dispose()
End Try
End If
End Sub
Sub CloneData1(ByVal sender As DataGridView, ByVal e As DataGridViewCellEventArgs)
Dim f As New ChildForm
Try
f.DataGridView1.CloneColumns(DataGridView1, "ProcessCol")
If bsPeople.CurrentRowIsValid Then
DataGridView1.CurrentRow.CloneRowWithValues(f.DataGridView1)
f.ShowDialog()
End If
Finally
f.Dispose()
End Try
End Sub
End Class
Add a code module place the following into it which will be used in the code above.
Code:
<System.Diagnostics.DebuggerStepThrough()> _
<Runtime.CompilerServices.Extension()> _
Public Function GetChecked(ByVal GridView As DataGridView, ByVal ColumnName As String) As DataGridViewRow
Return (From Rows In GridView.Rows.Cast(Of DataGridViewRow)() Where CBool(Rows.Cells(ColumnName).Value) = True).First
End Function
<System.Diagnostics.DebuggerStepThrough()> _
<Runtime.CompilerServices.Extension()> _
Public Function HasCheckedRows(ByVal GridView As DataGridView, ByVal ColumnName As String) As Boolean
Return (From Rows In GridView.Rows.Cast(Of DataGridViewRow)() Where CBool(Rows.Cells(ColumnName).Value) = True).Any
End Function
<Runtime.CompilerServices.Extension()> _
Public Sub CloneColumns(ByVal Self As DataGridView, ByVal CloneFrom As DataGridView, ByVal NameToExclude As String)
If Self.ColumnCount = 0 Then
For Each c As DataGridViewColumn In CloneFrom.Columns
Self.Columns.Add(CType(c.Clone, DataGridViewColumn))
Next
If Self.Columns.Contains(NameToExclude) Then
Self.Columns(NameToExclude).Visible = False
End If
End If
End Sub
<System.Diagnostics.DebuggerStepThrough()> _
<System.Runtime.CompilerServices.Extension()> _
Public Function CurrentRowIsValid(ByVal sender As BindingSource) As Boolean
Return Not (sender.Current Is Nothing)
End Function
<System.Runtime.CompilerServices.Extension()> _
Public Sub CloneRowWithValues(ByVal SingleRow As DataGridViewRow, ByVal Target As DataGridView)
Dim Results As DataGridViewRow = CType(SingleRow.Clone(), DataGridViewRow)
For Row As Int32 = 0 To SingleRow.Cells.Count - 1
Results.Cells(Row).Value = SingleRow.Cells(Row).Value
Next
Target.Rows.Add(Results)
End Sub
-
Nov 30th, 2014, 05:31 AM
#4
Thread Starter
Member
Re: Gridview Data
Thank you IanRyder for your reply.
For the second code, where i should put it?
-
Nov 30th, 2014, 05:33 AM
#5
Thread Starter
Member
Re: Gridview Data
Thank you kevininstructor for your reply.
The output is good as you have shown up, but can i get VS2010 code?
-
Nov 30th, 2014, 05:42 AM
#6
Re: Gridview Data
Hi,
As you can see I wrote my example to execute the opening of Form2 on the Clicking of the CheckBox Cell in the DataGridView which may not be exactly what you want. If you still want to try this method then, as I said, you put the second piece of code in the CellContentClick Event of the DataGridView.
As to kevininstructor’s example, I would suggest that you just give it a try in 2010 since even though I am in the same position as kevinstructor I cannot see any reason why that would not run in 2010.
Good luck and cheers,
Ian
-
Nov 30th, 2014, 06:38 AM
#7
Thread Starter
Member
Re: Gridview Data
I wrote kevininstructor’s example, but it gives me "'TDataTable' is not a member of 'System.Collections.Generic.IEnumerable(Of<anonymouse type>)'" here:
Code:
Dim dt =
(From line In IO.File.ReadAllLines("People.txt")
Where line.Length > 0
Let Items = line.Split(","c)
Select New With
{
.FirstName = Items(0),
.LastName = Items(1)
}
).ToDataTable
-
Nov 30th, 2014, 07:13 AM
#8
Re: Gridview Data
Seems I forget to add ToDataTable which you can located in MSDN Working with language extensions in the project Team Library,
DataExtensions.vb
-
Nov 30th, 2014, 07:14 AM
#9
Re: Gridview Data
Originally Posted by LeoJb
Thank you kevininstructor for your reply.
The output is good as you have shown up, but can i get VS2010 code?
If you please read the first sentence in my first reply.
-
Nov 30th, 2014, 07:31 AM
#10
Thread Starter
Member
Re: Gridview Data
So Shall i download it from the link that you have provided it?
-
Nov 30th, 2014, 07:37 AM
#11
Re: Gridview Data
No reason too, simply copy the ToDataTable from the file I mentioned, paste it into a code module (not a class or form) in the same project and use it.
-
Nov 30th, 2014, 01:46 PM
#12
Thread Starter
Member
Re: Gridview Data
Sorry for disturbing you, but i couldn't find ToDataTable.
What to do exactly?
-
Nov 30th, 2014, 02:41 PM
#13
Thread Starter
Member
Re: Gridview Data
Ok i did it. Still on thing, in the below it gives me "Type 'ChildForm' is not defined"
Code:
Private Sub Button2_Click(ByVal sender As Object, ByVal e As EventArgs) Handles Button1.Click
If dgv_UnbilledVouchers.HasCheckedRows("ProcessCol") Then
Dim CurrentCellData = dgv_UnbilledVouchers.GetChecked("ProcessCol")
Dim f As New ChildForm
Try
f.DataGridView1.CloneColumns(dgv_UnbilledVouchers, "ProcessCol")
If bsPeople.CurrentRowIsValid Then
dgv_UnbilledVouchers.CurrentRow.CloneRowWithValues(f.DataGridView1)
f.ShowDialog()
End If
Finally
f.Dispose()
End Try
End If
End Sub
And here:
Code:
Sub CloneData1(ByVal sender As DataGridView, ByVal e As DataGridViewCellEventArgs)
Dim f As New ChildForm
Try
f.DataGridView1.CloneColumns(dgv_UnbilledVouchers, "ProcessCol")
If bsPeople.CurrentRowIsValid Then
dgv_UnbilledVouchers.CurrentRow.CloneRowWithValues(f.DataGridView1)
f.ShowDialog()
End If
Finally
f.Dispose()
End Try
End Sub
-
Nov 30th, 2014, 04:29 PM
#14
Re: Gridview Data
As stated in an earlier reply, Child Form is a form with a DataGridView. So create a form with a DataGridView and name it as I did.
-
Dec 1st, 2014, 03:35 AM
#15
Thread Starter
Member
Re: Gridview Data
Thank you.
Now when i want to open the Form1 it gives me "Could not find the file 'C:\Users\..\..\..\bin\debug\People.txt'"
Code:
Dim dt =
(From line In IO.File.ReadAllLines("People.txt")
Where line.Length > 0
Let Items = line.Split(","c)
Select New With
{
.FirstName = Items(0),
.LastName = Items(1)
}
).ToDataTable
-
Dec 1st, 2014, 07:33 AM
#16
Re: Gridview Data
You need to create the file and use the data provided in post 3.
-
Dec 2nd, 2014, 03:53 AM
#17
Thread Starter
Member
Re: Gridview Data
But in this way, it will only show the data in the .txt file not from the database!
-
Dec 2nd, 2014, 07:50 AM
#18
Re: Gridview Data
Originally Posted by LeoJb
But in this way, it will only show the data in the .txt file not from the database!
True as this is a simple example, it's your responsibility to work with a database.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|