Answered by:
import column from dataGridView to another

Question
-
Dear all ,
I have 2 datagridviews , one of them connects to database and it has columns like (ID,Name,address,Phone Number,...)
another one is empty ..
now I need :
when I press a Button import just(Name and address ) columns from first datagridview to the empty one , not all of them.
please help me ..
Sunday, September 9, 2012 11:03 PM
Answers
-
Here is one where the clone DataGridView has a DataTable for a data source, the other DataGridView mimics loading data from a database. If the user tries to add a row more than once they are told they cannot as this would duplicate the row.
You could do the clone DataGridView as non-datatable too if you wanted too.
VS2010 VB.NET
Public Class Form1 WithEvents bsData As New BindingSource Private Sub Form1_Load( ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load Dim dtFromDatabase As New DataTable dtFromDatabase.Columns.Add( New DataColumn With { .ColumnName = "Identifier", .DataType = GetType(System.Int32) } ) dtFromDatabase.Columns.Add( New DataColumn With { .ColumnName = "Name", .DataType = GetType(System.String) } ) dtFromDatabase.Columns.Add( New DataColumn With { .ColumnName = "Address", .DataType = GetType(System.String) } ) dtFromDatabase.Columns.Add( New DataColumn With { .ColumnName = "Phone", .DataType = GetType(System.String) } ) dtFromDatabase.Columns("Identifier").AutoIncrement = True dtFromDatabase.Columns("Identifier").AllowDBNull = False dtFromDatabase.Columns("Identifier").ReadOnly = True dtFromDatabase.Columns("Identifier").AutoIncrementSeed = 1 dtFromDatabase.Rows.Add(New Object() _ {Nothing, "Kevin", "112 Apple Way", "5555555555"}) dtFromDatabase.Rows.Add(New Object() _ {Nothing, "Mary", "444 Orange Way", "6666666666"}) dtFromDatabase.Rows.Add(New Object() _ {Nothing, "Bob", "10 Milk Street", "9999999999"}) bsData.DataSource = dtFromDatabase dgvDatabase.DataSource = bsData Dim dtClone As New DataTable dtClone.Columns.Add( New DataColumn With { .ColumnName = "Name", .DataType = GetType(System.String) } ) dtClone.Columns.Add( New DataColumn With { .ColumnName = "Address", .DataType = GetType(System.String) } ) dgvClone.DataSource = dtClone End Sub Private Sub Button1_Click( ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click Dim CurrentRow = CType(bsData.Current, DataRowView) Dim dtBottom = CType(dgvClone.DataSource, DataTable) Dim Row = dtBottom.Select( <T>Name='<%= CurrentRow.Item("Name").ToString %>' and Address='<%= CurrentRow.Item("Address").ToString %>'</T>.Value ) If Row.Count = 0 Then dtBottom.Rows.Add(New Object() _ { CurrentRow.Item("Name").ToString, CurrentRow.Item("Address").ToString } ) Else MessageBox.Show("Not adding, already exists.") End If End Sub End Class
KSG
- Marked as answer by Bob Wu-MT Tuesday, September 25, 2012 2:43 AM
Monday, September 10, 2012 9:42 PM
All replies
-
Hi Ahmed,
If you want to add some of datagridview columns from one datagridview to another, you can get it according to its column name, clone it and then add the cloned column to another datagridview. For example,
DataGridViewColumn col = this.dataGridView1.Columns["Name"]; this.dataGridView2.Columns.Add((DataGridViewColumn)col.Clone());
Best Regards,
Bob Wu [MSFT]
MSDN Community Support | Feedback to us
Monday, September 10, 2012 7:31 AM -
Hi,
//make dt1 as public
DataTable dt1=new DataTable();
DataGrid1.DataSource=dt1;
// write the below code in button click event
DataTable dt2=new DataTable()l
dt.Columns.Add("name",typeOf(System.String));
dt.Columns.Add("address",typeOf(System.String));
for(int i=0;i<dt1.Rows.Count-1;i++)
{
dt2.Rows.Add(dt1.Rows[i]["name"].ToString(),dt1.Rows[i]["address"].ToString());
}
//Bind dataTable2 to the 2nd DataGrid
DataGrid2.DataSource=dt2
PS.Shakeer Hussain
Monday, September 10, 2012 9:27 AM -
You can do a loop through rows of DGV1, and pass only the data in specific columns to DGV2:
dgv2.Rows.Clear(); //precaution to clear it! int rowNo = 0; for(int i=0;i<dgv1.Rows.Count;i++) { dgv2.Rows.Add(); dgv2["Name", i].Value = dgv1["Name", i].Value; dgv2["Address", i].Value = dgv1["Address", i].Value; }
Mitja
Monday, September 10, 2012 4:11 PM -
Dear Bob Wu-MT and Syed Shakeer Hussain >>> I am using Vb.net not C# and I tried your codes but it is not working . please help me it is very necessary .. thanks
- Edited by Ahmed.Rayan Monday, September 10, 2012 4:22 PM
Monday, September 10, 2012 4:22 PM -
Here is one where the clone DataGridView has a DataTable for a data source, the other DataGridView mimics loading data from a database. If the user tries to add a row more than once they are told they cannot as this would duplicate the row.
You could do the clone DataGridView as non-datatable too if you wanted too.
VS2010 VB.NET
Public Class Form1 WithEvents bsData As New BindingSource Private Sub Form1_Load( ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load Dim dtFromDatabase As New DataTable dtFromDatabase.Columns.Add( New DataColumn With { .ColumnName = "Identifier", .DataType = GetType(System.Int32) } ) dtFromDatabase.Columns.Add( New DataColumn With { .ColumnName = "Name", .DataType = GetType(System.String) } ) dtFromDatabase.Columns.Add( New DataColumn With { .ColumnName = "Address", .DataType = GetType(System.String) } ) dtFromDatabase.Columns.Add( New DataColumn With { .ColumnName = "Phone", .DataType = GetType(System.String) } ) dtFromDatabase.Columns("Identifier").AutoIncrement = True dtFromDatabase.Columns("Identifier").AllowDBNull = False dtFromDatabase.Columns("Identifier").ReadOnly = True dtFromDatabase.Columns("Identifier").AutoIncrementSeed = 1 dtFromDatabase.Rows.Add(New Object() _ {Nothing, "Kevin", "112 Apple Way", "5555555555"}) dtFromDatabase.Rows.Add(New Object() _ {Nothing, "Mary", "444 Orange Way", "6666666666"}) dtFromDatabase.Rows.Add(New Object() _ {Nothing, "Bob", "10 Milk Street", "9999999999"}) bsData.DataSource = dtFromDatabase dgvDatabase.DataSource = bsData Dim dtClone As New DataTable dtClone.Columns.Add( New DataColumn With { .ColumnName = "Name", .DataType = GetType(System.String) } ) dtClone.Columns.Add( New DataColumn With { .ColumnName = "Address", .DataType = GetType(System.String) } ) dgvClone.DataSource = dtClone End Sub Private Sub Button1_Click( ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click Dim CurrentRow = CType(bsData.Current, DataRowView) Dim dtBottom = CType(dgvClone.DataSource, DataTable) Dim Row = dtBottom.Select( <T>Name='<%= CurrentRow.Item("Name").ToString %>' and Address='<%= CurrentRow.Item("Address").ToString %>'</T>.Value ) If Row.Count = 0 Then dtBottom.Rows.Add(New Object() _ { CurrentRow.Item("Name").ToString, CurrentRow.Item("Address").ToString } ) Else MessageBox.Show("Not adding, already exists.") End If End Sub End Class
KSG
- Marked as answer by Bob Wu-MT Tuesday, September 25, 2012 2:43 AM
Monday, September 10, 2012 9:42 PM -
in vb:
dgv2.Columns.Add("Name", "Name") dgv2.Columns.Add("Addresws", "Address") For i As Integer = 0 To dgv1.Rows.Count - 1 dgv2.Rows.Add() dgv2("Name", i).Value = dgv1("Name", i).Value dgv2("Address", i).Value = dgv1("Address", i).Value Next
Mitja
Tuesday, September 11, 2012 4:17 AM