This document provides steps to copy data from one table to another in Visual Studio. It begins by creating a new project and adding buttons and a data grid view. It then connects to a database, creates a table, and adds sample data. Code is written to retrieve data from the first table and display it. Additional code copies selected rows from the first grid to the second when a button is clicked. Testing confirms the process works as intended to move data between tables.
1. Copy data from table to another
ATP
Vocational Pedagogy
Student
202622013
Saleh Abalkhail
Application Development
Semester 6
Lecturer
Mohamed M.Elmaghawri
2. Copy data from table to another Vocational Pedagogy Application Development
1
Contents
Table of Figures.............................................................................................................................................2
Content Overview.........................................................................................................................................3
Specific Objectives ........................................................................................................................................3
Introduction ..................................................................................................................................................4
Data Grid View Definition .............................................................................................................................4
Database program used with visual studio...................................................................................................4
The tools for creating table in visual studio..................................................................................................4
Copy data from table to another..................................................................................................................5
Step 1: create new project in visual studio...............................................................................................5
Step 2: drag and drop two button and data grid view from toolbox into form .......................................5
Step 3: create database ............................................................................................................................6
Step 4: create table...................................................................................................................................7
Step 5: add data into table........................................................................................................................8
Step 6: add columns into table .................................................................................................................9
Step 7: write code for the table..............................................................................................................10
Step 8: import SQL library.......................................................................................................................11
Step 9: Write the SQL Connection ..........................................................................................................12
Step 10: write the code for buttons........................................................................................................12
Step 11: test the program.......................................................................................................................14
Conclusion...................................................................................................................................................14
References ..................................................................................................................................................15
3. Copy data from table to another Vocational Pedagogy Application Development
2
Table of Figures
Figure 1 Toolbox............................................................................................................................................4
Figure 2 create new project in visual studio.................................................................................................5
Figure 3 drag and drop two button and data grid view from toolbox into form..........................................5
Figure 4 create database...............................................................................................................................6
Figure 5 choose data Source.........................................................................................................................6
Figure 6 click Ok ............................................................................................................................................7
Figure 7 create table.....................................................................................................................................7
Figure 8 right table columns, table name, then click update .......................................................................8
Figure 9 open data view................................................................................................................................8
Figure 10 write data in the table...................................................................................................................9
Figure 11 add columns..................................................................................................................................9
Figure 12 uncheck checkbox.......................................................................................................................10
Figure 13 write code for the table ..............................................................................................................10
Figure 14 write the code.............................................................................................................................11
Figure 15 import SQL library.......................................................................................................................11
Figure 16 Write the SQL Connection ..........................................................................................................12
Figure 17 write the code for buttons..........................................................................................................12
Figure 18 The codes ....................................................................................................................................13
Figure 19 test the program .........................................................................................................................14
4. Copy data from table to another Vocational Pedagogy Application Development
3
Content Overview
• Data grid view.
• SQL commands.
• Fixed values.
Specific Objectives
By the end of the lesson the students will be able to:
LO1 Comprehend relation between Visual Studio and database.
LO2 Retrieve data from database using Select command.
LO3 Create data table in Visual Studio.
LO4 Copy data from table to another.
5. Copy data from table to another Vocational Pedagogy Application Development
4
Introduction
visual studio is a programming language which can be used to create application for windows operating
system, it is containing application tools such as button to do action when click on it, data grid view to
show data in table, all application tools can be drag and drop from toolbox in to forms.
Data Grid View Definition
The Data Grid View control provides a customizable table for displaying data. The Data Grid View class
allows customization of cells, rows, columns, and borders through the use of properties such as Default
Cell Style, Column Headers Default Cell Style, Cell Border Style, and Grid Color.
Database program used with visual studio
• Microsoft SQL server
• MYSQL
• PHP SQL
The tools for creating table in visual studio
• Button
• data grid view
• Form
Figure 1 Toolbox
6. Copy data from table to another Vocational Pedagogy Application Development
5
Copy data from table to another
Step 1: create new project in visual studio
Figure 2 create new project in visual studio
Open visual studio, next click on new project, next write any name, then click ok, Figure 2.
Step 2: drag and drop two button and data grid view from toolbox into form
Figure 3 drag and drop two button and data grid view from toolbox into form
7. Copy data from table to another Vocational Pedagogy Application Development
6
Step 3: create database
Figure 4 create database
Open server explorer toolbar, next right clicks on data connections, then click on add connection.
Figure 5 choose data Source
8. Copy data from table to another Vocational Pedagogy Application Development
7
Figure 6 click Ok
Step 4: create table
Figure 7 create table
Right click on table, next clicks on add new table
9. Copy data from table to another Vocational Pedagogy Application Development
8
Figure 8 right table columns, table name, then click update
Step 5: add data into table
Figure 9 open data view
Right click on table name, then click on show table data.
10. Copy data from table to another Vocational Pedagogy Application Development
9
Figure 10 write data in the table
Step 6: add columns into table
Figure 11 add columns
11. Copy data from table to another Vocational Pedagogy Application Development
10
Figure 12 uncheck checkbox
uncheck the adding, editing, deleting like in Figure 12.
Step 7: write code for the table
Figure 13 write code for the table
Right click on the table, next click on properties, then double click on mouse Click.
12. Copy data from table to another Vocational Pedagogy Application Development
11
Figure 14 write the code
Write the following code inside the brackets:
if ((bool)dataGridView1.SelectedRows[0].Cells[0].Value == false)
{
dataGridView1.SelectedRows[0].Cells[0].Value = true;
}
else
{
dataGridView1.SelectedRows[0].Cells[0].Value = false;
}
Step 8: import SQL library
Figure 15 import SQL library
Write the following code like in the Figure 15:
using System.Data.SqlClient;
13. Copy data from table to another Vocational Pedagogy Application Development
12
Step 9: Write the SQL Connection
Figure 16 Write the SQL Connection
Write the following code like in the Figure 16:
SqlConnection con = new SqlConnection(@"Data
Source=(LocalDB)MSSQLLocalDB;AttachDbFilename=C:UsersGansourcereposWindowsFormsApp1
WindowsFormsApp1transferData1.mdf;Integrated Security=True;Connect Timeout=30");
Step 10: write the code for buttons
Figure 17 write the code for buttons
14. Copy data from table to another Vocational Pedagogy Application Development
13
Figure 18 The codes
Double click on get Data button, then write the following code inside the brackets like in Figure 18:
SqlDataAdapter sda = new SqlDataAdapter("select * from informatiom", con);
DataTable dt = new DataTable();
sda.Fill(dt);
foreach (DataRow item in dt.Rows)
{
int n = dataGridView1.Rows.Add();
dataGridView1.Rows[n].Cells[0].Value = false;
dataGridView1.Rows[n].Cells[1].Value = item["Id"].ToString();
dataGridView1.Rows[n].Cells[2].Value = item["Name"].ToString();
}
Double click on copy button, then write the following code inside the brackets like in Figure 18:
dataGridView2.Rows.Clear();
foreach (DataGridViewRow item in dataGridView1.Rows)
{
if ((bool)item.Cells[0].Value == true)
{
int n = dataGridView2.Rows.Add();
dataGridView2.Rows[n].Cells[0].Value =
item.Cells[1].Value.ToString();
dataGridView2.Rows[n].Cells[1].Value =
item.Cells[2].Value.ToString();
}
}
15. Copy data from table to another Vocational Pedagogy Application Development
14
Step 11: test the program
Figure 19 test the program
Conclusion
To conclude, I have Introduce visual studio and the Data Grid View, I have explained the steps to copy
data from table into another through eleven steps, this project can be used to create application like
moves list, items list and employees list.
16. Copy data from table to another Vocational Pedagogy Application Development
15
References
Liberty, J. (2005). Programming C#: Building. NET Applications with C. " O'Reilly Media, Inc.".
Nagel, C., Evjen, B., Glynn, J., Skinner, M., & Watson, K. (2011). Professional C# 2008. John Wiley &
Sons.
Video: C# Copy Selected Rows in One Datagridview to another gridview
Retrieved from: https://www.youtube.com/watch?v=_iJuxh08TdQ
Date of access: 6/2/2018