Вы находитесь на странице: 1из 76

MAURICE MUTETI

Everything Goes Here

HOME SAMPL E PAG E

How To Connect To Access Donate

Database And Display Data And Your donation helps to


maintain the servers and
Images In Datagridview In C content creation and will be
much appreciated.
Sharp Windows Application
 by admin  January 7, 2020

How To Connect To Access Database And Search … 

Display Data And Images In Datagridview


In C# Windows Application
Recent Posts

C Sharp And Ms Access


Database Tutorial 23 How To
Connect To Ms Access
Database Insert Update
Delete Clear Print Export
Import Excel Display In
Datagridview Demo Video

C Sharp And Ms Access


Database Tutorial 22 How To

Table of contents : Show Datagridview Selected


Rows To Another Forms
Datagridview
1. HOW TO CREATE MICROSOFT ACCESS DATABASE AND TABLE
C Sharp And Ms Access
2. MS ACCESS DATABASE STRUCTURE.
Database Tutorial 21 How To
3. HOW TO ADD MS ACCESS DATABASE TO VISUAL STUDIO 2010
Show Datagridview Selected
(.ACCDB FILE)
Row Data In Another Form
4. HOW TO CONNECT TO MICROSOFT ACCESS DATABASE.
C Sharp And Ms Access
5. HOW TO INSERT DATA AND IMAGES INTO MICROSOFT ACCESS
Database Tutorial 20 How To
DATABASE.
Refresh Datagridview
6. HOW TO DISPLAY DATA AND IMAGES IN DATAGRIDVIEW FROM
MICROSOFT ACCESS ON FORM LOAD. C Sharp And Ms Access
Database Tutorial 19 How To
7. HOW TO UPDATE SELECTED DATAGRIDVIEW ROW WITH TEXTBOX
Clear Datagridview
AND PICTURE BOX USING C#
8. HOW TO DELETE SELECTED ROWS FROM DATAGRIDVIEW AND Recent Comments
MICROSOFT ACCESS DATABASE IN C# VISUAL STUDIO
9. HOW TO CONVERT IMAGE TO BYTE ARRAY.
Archives
10. HOW TO CONVERT BYTE ARRAY TO IMAGE.
11. HOW TO CHOOSE IMAGE FROM COMPUTER AND DISPLAY IT ON
February 2020
PICTUREBOX.
January 2020
12. HOW TO DISPLAY SELECTED ROW FROM DATAGRIDVIEW TO
TEXTBOXES IN IN C# WINDOW APPLICATION ON CELL CLICK. December 2019
13. HOW TO DISPLAY SELECTED ROW FROM DATAGRIDVIEW TO
October 2019
PICTUREBOX IN C# WINDOW APPLICATION ON CELL CLICK.
14. HOW TO CLEAR INPUT FIELDS, TEXT BOXES AND PICTURE BOX. September 2019

15. HOW TO ADD MICROSOFT.OFFICE.INTEROP.EXCEL REFERENCE IN July 2019


VISUAL STUDIO 2010.
June 2019
16. HOW TO IMPORT EXCEL FILE TO DATAGRIDVIEW IN C# WINDOWS
APPLICATION.
Categories
17. HOW TO EXPORT DATAGRIDVIEW TO EXCEL FILE IN C# WINDOWS
APPLICATION. Uncategorized
18. HOW TO PRINT DATAGRIDVIEW IN C# WINDOWS APPLICATION.
19. HOW TO CLEAR DATAGRIDVIEW IN C# WINDOWS APPLICATION.
20. HOW TO REFRESH DATAGRIDVIEW IN C# WINDOWS
APPLICATION.
21. HOW TO SHOW DATAGRIDVIEW SELECTED ROW DATA IN
ANOTHER FORM USING C#
22. HOW TO SHOW DATAGRIDVIEW SELECTED ROWS TO ANOTHER
FORMS DATAGRIDVIEW USING C#
23. HOW TO CONNECT TO MS ACCESS DATABASE INSERT UPDATE
DELETE CLEAR PRINT EXPORT IMPORT EXCEL DISPLAY IN
DATAGRIDVIEW IN C# WINDOWS APPLICATION (DEMO VIDEO)
24. COMPLETE VIDEO TUTORIAL

This Tutorial Shows How To Connect To Access Database And Display


Data And Images In Datagridview In C Sharp Windows Application. This
C# application inserts data entered by user to ms access database.
Also the images loaded from pc and displayed on picturebox are
inserted in to the database as byte array. Then images are first
converted to byte arrays before being stored in access database. The
first database column is of type integer which increments automatically
whenever you insert a new row.
CREATING MICROSOFT ACCESS
DATABASE AND TABLE

Go to start menu and search “Microsoft Access”. Then select it from the
list.
HOW TO CREATE MICROSOFT ACCESS DATABASE AND TABLE

Click On Blank Database


HOW TO CREATE MICROSOFT ACCESS DATABASE – CLICK ON BLANK DATABASE

A New Database and table named “Database2” and “Table1” Gets


created.
HOW TO CREATE MICROSOFT ACCESS DATABASE – NEW DATABASE AND TABLE CREATED

Right click on table name and select design view.

HOW TO CREATE MICROSOFT ACCESS DATABASE – DESIGN VIEW

Add Field names like (


FullName,EmailAddress,PhoneNumberP,LanguageP,
CountryP,GenderP,ImagePath, ImageFile etc.) and Datatypes.
HOW TO CREATE MICROSOFT ACCESS DATABASE – ADD FIELD NAMES AND DATATYPES

The newly created database is saved in documents folder unless you


specified a different directory when creating the database.
HOW TO CREATE MICROSOFT ACCESS DATABASE – DATABASE SAVED IN DOCUMENTS FOLDER

VIDEO TUTORIAL

C# And Ms Access Database Tutorial #1 - …


Watch later Share
C# And Ms Access Database Tutorial #1 – How To Create Ms Access Database In C#

MS access database Structure

The database has the following fields,


FullName,EmailAddress,PhoneNumberP,LanguageP,
CountryP,GenderP,ImagePath, ImageFile. All of them are of datatype
text except imagefile. The imagefile is of type Ole Object , Which comes
in handy for files.(Images, Pdf, etc).
How to connect to MS access database in c#

VIDEO TUTORIAL

C# And Ms Access Database Tutorial #2 - …


Watch later Share
ADDING MS ACCESS DATABASE TO
VISUAL STUDIO 2010 (.ACCDB FILE)

On tools menu, Select Connect To Database.


How To Add Ms Access Database To Visual Studio 2010

In data source section “Microsoft Access Database File (OLE DB)” is


selected by default.
How To Add Ms Access Database To Visual Studio 2010 – Microsoft Access Database File OLE DB
Data Source

Choose Browse next to Database file name, and then navigate to your
.accdb file and choose Open.
How To Add Ms Access Database To Visual Studio 2010 – Choose Browse next to Database file
name

Browse the file name. This is the location where your database is
stored. i.e. – (C:\Users\Authentic\Documents\Database1.accdb).
How To Add Ms Access Database To Visual Studio 2010 – Connection String Database File Name

Click Test Connection To Test Connection to the database.


How To Add Ms Access Database To Visual Studio 2010 – Test Connection

If the connection is successful click Ok on the message box.


How To Add Ms Access Database To Visual Studio 2010 – Test Microsoft Access Database
Connection

If You want to get database connection string and other database


properties go to “Advanced”.
How To Add Ms Access Database To Visual Studio 2010 – Advanced

This is the connection string you will use programmatically to connect to


ms access database.
How To Add Ms Access Database To Visual Studio 2010 – Provider And DataSource Connection
String

Now go to server explorer window and you will see the newly added
database.
How To Add Ms Access Database To Visual Studio 2010 – Microsoft Access Database Added To
Project Server Explorer

VIDEO TUTORIAL

C# And Ms Access Database Tutorial #3 - …


Watch later Share

C# And Ms Access Database Tutorial #3 – How To Add Ms Access Database To Visual Studio 2010
(.accdb file)

CONNECTING TO THE DATABASE

Connection is initialized in form1 Constructor.


CODE SNIPPET C#
1 OleDbConnection accessDatabaseConnection = null;
?

2 //MS Access Database Connection String


3 string connectionSttring = @"Provider=Microsoft
4 public Form1()
5 {
6 //Initializing MS Access Database Connection
7 accessDatabaseConnection = new OleDbConnecti
8 InitializeComponent();
9 }

VIDEO TUTORIAL

C# And Ms Access Database Tutorial #4 - …


Watch later Share

C# And Ms Access Database Tutorial #4 – How To Connect To Ms Access Database In C#


INSERTING DATA AND IMAGES INTO MS-
ACCESS DATABASE

How To Insert Data Into Ms Access Database Using C Sharp

When the data is inserted you get a dialog box informing you that the
data has been added to ms-access database successfully.
How To Insert Data Into Ms Access Database Using C Sharp And Display In Datagridview

When you click OK button on the message box, the box closes and
datagridview refreshes with a new inserted row at the bottom.
How To Display Data In Datagridview From Access Database C Sharp

The data reflects in ms access database instantly.

How To Insert Data Into Ms Access Database Using CSharp – Database

All fields are inserted to ms access database at once when you click
insert button. The image from picturebox is added to the database as
byte array. The Id Column, Which is the first one, Is Auto Incremented
whenever you insert a new row to the database.

CODE SNIPPET C#
1 //Inserting data into microsoft access database
?

2 private void btnInsert_Click(object sender, Eve


3 {
4 try
5 {
6 //Check If One Or More Fields Are Empty
7 if (txtFullName.Text == String.Empty |
8 {
9 MessageBox.Show("One Or More Empty
10 }
11 else
12 {
13 OleDbCommand insertDataIntoMSAccess
14 insertDataIntoMSAccessDataBaseOleDb
15 insertDataIntoMSAccessDataBaseOleDb
16 insertDataIntoMSAccessDataBaseOleDb
17 insertDataIntoMSAccessDataBaseOleDb
18 insertDataIntoMSAccessDataBaseOleDb
19 insertDataIntoMSAccessDataBaseOleDb
20 insertDataIntoMSAccessDataBaseOleDb
21 insertDataIntoMSAccessDataBaseOleDb
22 //Opening Access Database Connectio
23 accessDatabaseConnection.Open();
24 int insertDataToAccessDatabase = in
25 //If data Has been inserted to the
26 if (insertDataToAccessDatabase > 0)
27 {
28 MessageBox.Show("Data Inserted
29 }
30 }
31
32 }
33 catch (Exception ex)
34 {
35 MessageBox.Show(ex.StackTrace);
36 }
37 finally
38 {
39 //Finally Close MS Access Database Conn
40 if (accessDatabaseConnection != null)
41 {
42 accessDatabaseConnection.Close();
43 }
44 }
45 //Refreshing Datagridview after inserting n
46 populateDataGridView();
47 //Clear Input Fields PictureBox And Textbox
48 clearInputFields();
49 }

VIDEO TUTORIAL

C# And Ms Access Database Tutorial #5 - …


Watch later Share

C# And Ms Access Database Tutorial #5 – How To Insert Data And Images Into Microsoft Access
Database

DISPLAYING DATA AND IMAGES IN


DATAGRIDVIEW FROM MS-ACCESS ON
FORM LOAD
Display Access Table Records In Datagridview In C Sharp

All rows form ms-access database are displayed in datagridview when


you run the application. The first row which is (ID) is not displayed.
Image row is the last one. It displays images which are converted(cast)
explicitly to byte array. The Code runs inside form load event.

CODE SNIPPET C#
1 //Function for retrieving data from ms access ?d
2 public void populateDataGridView()
3 {
4 //First, clear all rows before populating d
5 if (dataGridView1.Rows.Count > 0)
6 {
7 dataGridView1.Rows.Clear();
8 }
9
10 try
11 {
12 accessDatabaseConnection.Open();
13 //OleDbDataAdapter adapter = new OleDbD
14 OleDbCommand command = new OleDbCommand
15 OleDbDataReader reader = command.Execut
16
17 while (reader.Read())
18 {
19 //Console.WriteLine(reader[8].GetTy
20 dataGridView1.Rows.Add(reader[0].To
21 }
22
23 reader.Close();
24 }
25 catch (Exception ex)
26 {
27 MessageBox.Show(ex.StackTrace);
28 }
29 finally
30 {
31 //Finally Close MS Access Database Conn
32 if (accessDatabaseConnection != null)
33 {
34 accessDatabaseConnection.Close();
35 }
36
37
38 }
39 }

VIDEO TUTORIAL

C# And Ms Access Database Tutorial #6 - …


Watch later Share
C# And Ms Access Database Tutorial #6 – How To Display Data And Images In Datagridview From
Microsoft Access On Form Load

UPDATING SELECTED DATAGRIDVIEW


ROW WITH TEXTBOX AND PICTUREBOX
USING C#
How To Update Row In Datagridview And Ms Access In C Sharp Windows Application

The data is updated based on the selected datagridview row ID. To use
update feature click on datagridview row, And the input fields gets filled
with selected datagridview row data. Then change few fields and click
update button.

CODE SNIPPET C#
1 //Update data in microsoft access database and?
2 private void button1_Click(object sender, Event
3 {
4 if (txtID.Text == String.Empty)
5 {
6 MessageBox.Show("First Click On Datagri
7 }
8 else
9 {
10 try
11 {
12 //Check If One Or More Fields Are E
13 if (txtFullName.Text == String.Empt
14 {
15 MessageBox.Show("One Or More Em
16 }
17 else
18 {
19 OleDbCommand updateDataInMSAcce
20 updateDataInMSAccessDatabaseOle
21 updateDataInMSAccessDatabaseOle
22 updateDataInMSAccessDatabaseOle
23 updateDataInMSAccessDatabaseOle
24 updateDataInMSAccessDatabaseOle
25 updateDataInMSAccessDatabaseOle
26 updateDataInMSAccessDatabaseOle
27 updateDataInMSAccessDatabaseOle
28 updateDataInMSAccessDatabaseOle
29 //Opening Access Database Conne
30 accessDatabaseConnection.Open()
31 int insertDataToAccessDatabase
32 //If data Has been inserted to
33 if (insertDataToAccessDatabase
34 {
35 MessageBox.Show("Data Updat
36 //Clear Input fields After
37 clearInputFields();
38 }
39 }
40
41 }
42 catch (Exception ex)
43 {
44 MessageBox.Show(ex.StackTrace);
45 }
46 finally
47 {
48 //Finally Close MS Access Database
49 if (accessDatabaseConnection != nul
50 {
51 accessDatabaseConnection.Close
52 }
53 }
54 //Refreshing Datagridview after Updatin
55 populateDataGridView();
56 }
57
58 }

VIDEO TUTORIAL

C# And Ms Access Database Tutorial #7 - …


Watch later Share

C# And Ms Access Database Tutorial #7 – How To Update Selected Datagridview Row With Textbox
And Picture Box
Deleting Selected Rows From Datagridview
And Microsoft Access Database In C#
Visual Studio

How To Delete Record From Ms-access Database And Datagridview In C Sharp

Then a Message Box Appears showing that the selected row has been
deleted successfully.
How To Delete Record From Ms-access Database And Datagridview In C Sharp Windows Application

The Row Gets deleted when you click Delete button. This removes
selected row from both MS Access Database and Datagridview.

CODE SNIPPET C#
1 //Delete From Microsoft access Database ?

2 private void btnDelete_Click(object sender, Eve


3 {
4 if (txtID.Text == String.Empty)
5 {
6 MessageBox.Show("First Click On Datagri
7 }
8 else
9 {
10 try
11 {
12 OleDbCommand deleteDataFromMSAccess
13 deleteDataFromMSAccessDatabaseOleDb
14 //Opening Access Database Connectio
15 accessDatabaseConnection.Open();
16 int deleteDataFromMSAccessDatabase
17 //If data Has been Deleted from the
18 if (deleteDataFromMSAccessDatabase
19 {
20 MessageBox.Show("Data Deleted F
21 //Clear Input fields After Dele
22 clearInputFields();
23 }
24
25
26 }
27 catch (Exception ex)
28 {
29 MessageBox.Show(ex.StackTrace);
30 }
31 finally
32 {
33 //Finally Close MS Access Database
34 if (accessDatabaseConnection != nul
35 {
36 accessDatabaseConnection.Close
37 }
38 }
39 //Refreshing Datagridview after Deletin
40 populateDataGridView();
41 }
42 }

VIDEO TUTORIAL

C# And Ms Access Database Tutorial #8 - …


Watch later Share
C# And Ms Access Database Tutorial #8 – How To Delete Selected Rows From Datagridview And
Microsoft Access Database

CONVERTING IMAGE TO BYTE ARRAY

Images from picturebox are converted to byte array before being


inserted to ms access database.

CODE SNIPPET C#
1 //Function For Converting Image To Byte Array ?
2 public byte[] convertImageToByteArray(Image img)
3 {
4 MemoryStream ms = new MemoryStream();
5 img.Save(ms, img.RawFormat);
6 return ms.ToArray();
7 }

VIDEO TUTORIAL
C# And Ms Access Database Tutorial #9 - …
Watch later Share

C# And Ms Access Database Tutorial #9 – How To Convert Image To Byte Array

CONVERTING BYTE ARRAY TO IMAGE

Images are converted from byte array to Image before displaying them
on picturebox from ms-access database.

CODE SNIPPET C#
1 //Function for Converting Byte Array To Image ?
2 public Image convertByteArrayToImage(byte[] img)
3 {
4 MemoryStream ms = new MemoryStream(img);
5
6 return Image.FromStream(ms);
7
8 }

VIDEO TUTORIAL

C# And Ms Access Database Tutorial #10 …


Watch later Share

C# And Ms Access Database Tutorial #10 – How To Convert Byte Array To Image

CHOOSING IMAGE FROM COMPUTER


AND DISPLAYING IT ON PICTUREBOX
How To Choose And Show Image In Picturebox In C Sharp Using Openfiledialog

When you click choose image button the Dialog open giving you an
option to choose image from your computer. When you choose image
and click open button the image is displayed on PictureBox ready to be
inserted into the database. Also full image path is displayed in the
appropriate TextBox.

CODE SNIPPET C#
1 //Display image on picture box on button click?
2 private void btnChooseImage_Click(object sender
3 {
4 OpenFileDialog chooseImage = new OpenFileDi
5 chooseImage.Title = "Choose Image";
6 DialogResult chooseImageDialog = chooseImag
7
8 if (chooseImageDialog == DialogResult.OK)
9 {
10 txtImagePath.Text = chooseImage.FileNam
11 //Displaying image from Pc On PictureBo
12 pictureBox1.Image = Image.FromFile(choo
13 }
14 }

VIDEO TUTORIAL

C# And Ms Access Database Tutorial #11 …


Watch later Share

C# And Ms Access Database Tutorial #11 – How To Choose Image From Computer And Display It On
Picturebox

DISPLAYING SELECTED ROW FROM


DATAGRIDVIEW TO TEXTBOXES
How To Display Selected Row Text And Image From Datagridview To Picturebox And Text Boxes In C
Sharp

Each Cell content From a specific row get assigned to appropriate


textbox.Text Value when you click on datagridview cell.

CODE SNIPPET C#
1 private void dataGridView1_CellContentClick(obj
?

2 {
3 //Check if the row cell content is clicked
4 if (e.RowIndex >= 0)
5 {
6 DataGridViewRow datagridviewrow = dataG
7 //Assigning Textboxes and picturebox wi
8 txtID.Text = datagridviewrow.Cells[0].V
9 txtFullName.Text = datagridviewrow.Cell
10 txtEmail.Text = datagridviewrow.Cells[2
11 txtPhoneNumber.Text = datagridviewrow.C
12 txtLanguage.Text = datagridviewrow.Cell
13 txtCountry.Text = datagridviewrow.Cells
14 txtGender.Text = datagridviewrow.Cells
15 txtImagePath.Text = datagridviewrow.Cel
16 //First cast image from datagridview ce
17 pictureBox1.Image = convertByteArrayToI
18 }
19 }

VIDEO TUTORIAL

C# And Ms Access Database Tutorial #12 …


Watch later Share

C# And Ms Access Database Tutorial #12 – How To Display Selected Row From Datagridview To
Textboxes On Cell Click

DISPLAYING SELECTED ROW FROM


DATAGRIDVIEW TO PICTUREBOX
How To Display Selected Row Text And Image From Datagridview To Picturebox And Text Boxes In C
Sharp

First i had to cast the image from datagridview cell to byte array and
then converted it to Image and finally assigned it to picturebox.Image
Value.

CODE SNIPPET C#
1 private void dataGridView1_CellContentClick(obj
?

2 {
3 //Check if the row cell content is clicked
4 if (e.RowIndex >= 0)
5 {
6 DataGridViewRow datagridviewrow = dataG
7
8 //First cast image from datagridview ce
9 pictureBox1.Image = convertByteArrayToI
10 }
11 }

VIDEO TUTORIAL

C# And Ms Access Database Tutorial #13 …


Watch later Share

C# And Ms Access Database Tutorial #13 – How To Display Selected Row From Datagridview To
Picturebox Cell Click

CLEARING INPUT FIELDS TEXT BOXES


AND PICTURE BOX
How To Clear Datagridview In C Sharp Windows Application

All input fields(textboxes, picturebox) gets cleared when you click Clear
button.

CODE SNIPPET C#
1 public void clearInputFields() ?

2 {
3 //Clearing Textfields
4 txtID.Text = String.Empty;
5 txtFullName.Text = String.Empty;
6 txtEmail.Text = String.Empty;
7 txtPhoneNumber.Text = String.Empty;
8 txtLanguage.Text = String.Empty;
9 txtCountry.Text = String.Empty;
10 txtGender.Text = String.Empty;
11 txtImagePath.Text = String.Empty;
12 //Clearing pictureBox
13 pictureBox1.Image = null;
14 }
15
16 private void btnClear_Click(object sender, Even
17 {
18 clearInputFields()
19 }

VIDEO TUTORIAL

C# And Ms Access Database Tutorial #14 …


Watch later Share

C# And Ms Access Database Tutorial #14 – How To Clear Input Fields Text Boxes And Picture Box

ADDING
MICROSOFT.OFFICE.INTEROP.EXCEL
REFERENCE IN VISUAL STUDIO 2010
Right-click on “References” and select “Add Reference”.

Adding Microsoft Office Interop Excel – Right-click on References and select Add Reference

Select the “.NET” tab.


Adding Microsoft Office Interop Excel – Select the Dot NET tab

Look for Microsoft.Office.Interop.Excel. Select it.

Click Ok.

VIDEO TUTORIAL

C# And Ms Access Database Tutorial #15 …


Watch later Share
C# And Ms Access Database Tutorial #15 – How To Add Microsoft Office Interop Excel Reference In
Visual Studio 2010

EXPORTING DATAGRIDVIEW TO EXCEL IN


C SHARP

How To Export Data From Datagridview To Excel In C#.Net Windows Application


The data is exported to excel file when you click export button, Also The
dialog box appears prompting you to enter excel file name and select
save location.

How To Export Data From Datagridview To Excel In Csharp – Excel File


Exporting Datagridview to Excel in C Sharp

CODE SNIPPET C#
1 //Exporting DataGridView To Excel ?

2 private void btnExportDataGridViewToExcel_Click


3 {
4 try
5 {
6 Microsoft.Office.Interop.Excel._Applica
7 Microsoft.Office.Interop.Excel._Workboo
8 Microsoft.Office.Interop.Excel._Workshe
9 exportDataGridViewToExcelApplication.Vi
10 exportDataGridViewToExcelWorksheet = ex
11 exportDataGridViewToExcelWorksheet = ex
12 exportDataGridViewToExcelWorksheet.Name
13
14 try
15 {
16 for (int i = 0; i < dataGridView1.C
17 {
18 exportDataGridViewToExcelWorksh
19 }
20 for (int i = 0; i < dataGridView1.R
21 {
22 for (int j = 0; j < dataGridVie
23 {
24 if (dataGridView1.Rows[i].C
25 {
26 exportDataGridViewToExc
27 }
28 else
29 {
30 exportDataGridViewToExc
31 }
32 }
33 }
34
35 //Dialog Box For Saving Excel File
36 SaveFileDialog exportDataGridViewTo
37 //Dialog Box Title
38 exportDataGridViewToExcelSaveDialog
39 //Supported Excel File Extensions
40 exportDataGridViewToExcelSaveDialog
41 //Default Excel File Extension
42 exportDataGridViewToExcelSaveDialog
43 //Excel File Name
44 exportDataGridViewToExcelSaveDialog
45
46 if (exportDataGridViewToExcelSaveDi
47 {
48 exportDataGridViewToExcelWorkbo
49 MessageBox.Show("DataGridView E
50 }
51 }
52 catch (System.Exception ex)
53 {
54 MessageBox.Show(ex.Message);
55 }
56
57 finally
58 {
59 //Release Resources
60 exportDataGridViewToExcelWorksheet
61 exportDataGridViewToExcelWorkbook.C
62 exportDataGridViewToExcelApplicatio
63
64 }
65 }
66 catch (Exception ex)
67 {
68 MessageBox.Show(ex.Message.ToString());
69 }
70 }

VIDEO TUTORIAL

C# And Ms Access Database Tutorial #17 …


Watch later Share

C# And Ms Access Database Tutorial #17 – How To Export Datagridview To Excel File In C# Windows
Application
IMPORTING DATA FROM EXCEL TO
DATAGRIDVIEW IN C#

How To Import Excel File To Datagridview In C Sharp Windows Application

Excel File gets imported to datagridview when you click Import Button.
You can clear datagridview rows before importing to see this feature in
Action.

CODE SNIPPET C#
1 //Import Data From Excel To DataGridView ?

2 private void btnImportExcelToDataGridView_Click


3 {
4 Microsoft.Office.Interop.Excel.Application
5 Range ShtRange;
6 try
7 {
8 OpenFileDialog importExcelToDataGridVie
9 //Supported File Extensions
10 importExcelToDataGridViewOpenFileDialog
11 //Dialog Title
12 importExcelToDataGridViewOpenFileDialog
13 DialogResult importExcelToDataGridViewD
14
15 if (importExcelToDataGridViewDialogResu
16 {
17 Microsoft.Office.Interop.Excel.Work
18 Microsoft.Office.Interop.Excel.Work
19 ShtRange = importExcelToDataGridVie
20 //Looping Through excel Cells
21 for (int Rnum = 2; Rnum <= ShtRange
22 {
23 //Getting Image From excel path
24 Image imageFromExcel = Image.Fr
25 //Converting Image To Byte Arra
26 byte[] byteImageFromExcel = con
27 //Populating DataGridView From
28 dataGridView1.Rows.Add(importEx
29 //for (int i = 0; i < ShtRange
30 //{
31 // dataGridView1.Rows[Rnum]
32 //}
33 }
34 //Release Resources
35 importExcelToDataGridViewWorkbook.C
36 importExcelToDataGridViewApplicatio
37 }
38
39 }
40 catch (Exception ex)
41 {
42 MessageBox.Show(ex.Message.ToString());
43 }
44 finally
45 {
46
47 }
48 }

VIDEO TUTORIAL

C# And Ms Access Database Tutorial #16 …


Watch later Share

C# And Ms Access Database Tutorial #16 – How To Import Excel File To Datagridview In C#
Windows Application

PRINTING DATAGRIDVIEW IN C# BY
CLICKING ON PRINT BUTTON

To print DataGridView Drag PrintDocument from toolbox to your form


application. As Show Below.
How To Print Datagridview In C Sharp – Drag PrintDocument from toolbox to your form application
How To Print Data From Datagridview In C Sharp Windows Application

How To Print Datagridview In C# By Clicking On Print Button

CODE SNIPPET C#
1 private void printDocument1_PrintPage(object se
?

2 {
3 Bitmap bm = new Bitmap(this.dataGridView1.W
4 dataGridView1.DrawToBitmap(bm, new System.D
5 e.Graphics.DrawImage(bm, 0, 0);
6 }
7
8 private void btnPrintDataGridView_Click(object
9 {
10 //Open the print dialog
11 PrintDialog printDialog = new PrintDialog()
12 printDialog.Document = printDocument1;
13 printDialog.UseEXDialog = true;
14 //Print document
15 if (DialogResult.OK == printDialog.ShowDial
16 {
17 //Document Name
18 printDocument1.DocumentName = "Printing
19 //Print Function
20 printDocument1.Print();
21 MessageBox.Show("Document Printed!!!..
22 }
23 }

VIDEO TUTORIAL

C# And Ms Access Database Tutorial #18 …


Watch later Share
C# And Ms Access Database Tutorial #18 – How To Print Datagridview In C# Windows Application

CLEARING DATAGRIDVIEW ROWS IN C#


WINDOWS APPLICATION

How To Clear Datagridview In C Sharp Windows Application

First am checking if the DataGridView rows are empty before clearing


them.

CODE SNIPPET C#
1 //Clearing DataGridView ?

2 private void btnclearDataGridView_Click(object


3 {
4 //Checking if datagridview rows are empty b
5 if (dataGridView1.Rows.Count > 0)
6 {
7 //Clear All Rows
8 dataGridView1.Rows.Clear();
9 }
10 else
11 {
12 MessageBox.Show("DataGridView Is Empty
13 }
14 }

VIDEO TUTORIAL

C# And Ms Access Database Tutorial #19 …


Watch later Share

C# And Ms Access Database Tutorial #19 – How To Clear Datagridview In C# Windows Application
REFRESHING DATAGRIDVIEW IN C#
WINDOWS APPLICATION

How To Refresh Datagridview In C# Windows Application

Clearing rows from the datagridview doesn’t delete them from MS


Access. Thus, Refreshing it restore cleared rows.

Refresh Feature calls populateDataGridView Function, which populates


datagridview with data from MS Access Database.

CODE SNIPPET C#
1 private void btnRefreshDataGridView_Click(object
?
2 {
3 populateDataGridView();
4 }

VIDEO TUTORIAL

C# And Ms Access Database Tutorial #20 …


Watch later Share

C# And Ms Access Database Tutorial #20 – How To Refresh Datagridview In C# Windows


Application

DISPLAY DATAGRIDVIEW SELECTED ROW


DATA IN ANOTHER FORM USING C#
How To Show Selected Datagridview Row Data In Another Form In C# Windows Application

Show Datagridview Selected Row Data In Another Form

CODE SNIPPET C#
1 //Show Datagridview Selected Row Data In Anothe
?

2 private void btnViewSelectedRowOnNewForm_Click


3 {
4
5 // Make sure user selects at least 1 row
6 if (dataGridView1.SelectedRows.Count == 1 &
7 {
8 ShowDatagridviewSelectedRowDataInAnothe
9 showDatagridviewSelectedRowDataInAnothe
10 showDatagridviewSelectedRowDataInAnothe
11 showDatagridviewSelectedRowDataInAnothe
12 showDatagridviewSelectedRowDataInAnothe
13 showDatagridviewSelectedRowDataInAnothe
14 showDatagridviewSelectedRowDataInAnothe
15 showDatagridviewSelectedRowDataInAnothe
16 showDatagridviewSelectedRowDataInAnothe
17 showDatagridviewSelectedRowDataInAnothe
18 //Show New Form With Data From Another
19 showDatagridviewSelectedRowDataInAnothe
20 }
21 else if (dataGridView1.Rows.Count == 0)
22 {
23 MessageBox.Show("DataGridView Is Empty
24 }
25 else if (dataGridView1.SelectedRows.Count =
26 {
27 MessageBox.Show("DataGridView Has Data
28
29 }
30 else if (dataGridView1.SelectedRows.Count >
31 {
32 MessageBox.Show("You Have Selected More
33
34 }
35 else
36 {
37 MessageBox.Show("Unknown Error Try Agai
38 }
39 }
VIDEO TUTORIAL

C# And Ms Access Database Tutorial #21 …


Watch later Share

C# And Ms Access Database Tutorial #21 – How To Show Datagridview Selected Row Data In
Another Form Using C#

DISPLAY DATAGRIDVIEW SELECTED ROWS


TO ANOTHER FORMS DATAGRIDVIEW
USING C#
How To Show Datagridview Selected Rows Data In Another Forms Datagridview In C# Windows
Application

How To Show Datagridview Selected Rows Data In Another Form Datagridview


CODE SNIPPET C#
1 //Show Datagridview Selected Rows In Another Fo
?

2 private void btnbtnViewSelectedRowOnNewDGV_Clic


3 {
4 // Make sure user selects at least 1 row
5 if (dataGridView1.SelectedRows.Count > 0 &&
6 {
7 ShowDatagridviewSelectedRowsInAnotherFo
8 for (int i = 0; i < dataGridView1.Selec
9 {
10 int index = showDatagridviewSelecte
11 showDatagridviewSelectedRowsDataInA
12 showDatagridviewSelectedRowsDataInA
13 showDatagridviewSelectedRowsDataInA
14 showDatagridviewSelectedRowsDataInA
15 showDatagridviewSelectedRowsDataInA
16 showDatagridviewSelectedRowsDataInA
17 showDatagridviewSelectedRowsDataInA
18 showDatagridviewSelectedRowsDataInA
19 showDatagridviewSelectedRowsDataInA
20
21 }
22 showDatagridviewSelectedRowsDataInAnoth
23 }
24 else if (dataGridView1.Rows.Count == 0)
25 {
26 MessageBox.Show("DataGridView Is Empty
27 }
28 else if (dataGridView1.SelectedRows.Count =
29 {
30 MessageBox.Show("DataGridView Has Data
31
32 }
33 else
34 {
35 MessageBox.Show("Unknown Error Try Agai
36 }
37 }

C# And Ms Access Database Tutorial #22 …


Watch later Share

C# And Ms Access Database Tutorial #22 – How To Show Datagridview Selected Rows To Another
Forms Datagridview Using C#

DEMO VIDEO

How To Connect To Ms Access Database …


Watch later Share
How To Connect To Ms Access Database Insert Update Delete Clear Print Export Import Excel
Display In DatagridView In C# Windows Application (Demo Video)

COMPLETE VIDEO TUTORIAL

(CRUD) C# And Microsoft Access Databa…


Databa…
Watch later Share

(CRUD) C# And Microsoft Access Database Tutorial – Insert Update


Delete Select And Display On DataGridView Complete Project Guide
Using Visual Studio 2010
Tags: access database, Clear, CRUD, Database, datagridview, delete, Display, edit, Excel,
Export, Exporting Datagridview to Excel in C Sharp, how to add column in datagridview in c#,
how to add row in datagridview in c#, how to clear datagridview in c# on button click, how to
connect ms access database, How To Connect To Access Database And Display Data And
Images On Datagridview In C# Windows Application, How To Create Ms Access Database,
how to delete data from ms access database using c#.net, how to display data from ms
access database using c#.net, how to export datagridview to excel in c#, how to extract
data from ms access database, how to fetch data from ms access database in c#, how to
filter datagridview in c#, how to get data from ms access database in c#, how to insert data
into ms access database using c#, how to print datagridview in c#, how to read data from
ms access database in c#, how to refresh datagridview in c#, How To Refresh Datagridview
In C# Windows Application, how to retrieve data from ms access database in c#, how to
save and load images from access database using C#, how to select data from ms access
database using c#.net, how to show data in datagridview in c#, how to update data into ms
access database using c#.net, how to update datagridview in c#, Import, Insert, Insert
Image Into Database, microsoft access, print, Refresh, Saving & Reading Images from
database, tutorial, Update

Published by admin

View all posts by admin

Prev Next
Solved!! Can’t read from the source file How To Add And Use Images From
or disk. [Problem Fixed] Resources In C Sharp Using Visual
Studio 2010

Leave a Reply
Your email address will not be published. Required fields are marked *

Comment

Name *

Email *

Website
Save my name, email, and website in this browser for the next time I comment.

POST COMMENT

© Copyright 2020 – Maurice Muteti


Allium Theme by TemplateLens ⋅ Powered by WordPress

PDFmyURL.com - convert URLs, web pages or even full websites to PDF online. Easy API for developers!

Вам также может понравиться