Friday, April 19, 2013

How to connect a SQL Server database with a DataGridView with C# (Visual Studio)

Note: Click the link if you want to know how to use a SQL Server database with a multiline textbox.

This tutorial will show you how to show data in a DataGridView from a SQL Server database. Pretty simple. Just in case, the screenshots show Visual Studio in Spanish. That's the only version I have, but I think you can deduce the names of the buttons in English. It's very easy watching the screenshots.

  • Visual Studio 2012 (it's what I used).  You can get the express version here.
  • SQL Server 2012.  Get the express version here.  This is not necessary, but I recommend to install SQL Server Management Studio. I'm more comfortable using that tools to manage my databases in SQL Server. To get SQL Server Management Studio along with the other components, download ENU\x86\SQLEXPRADV_x86_ENU.exe or ENU\x64\SQLEXPRADV_x64_ENU.exe, depending of the architecture of your computer.

1. Create a new database and fill it with some data

1.1 Open SQL Server Management Studio and connect to it.
1.2 Expand [Server]>Databases
1.3 Right click Databases>New Database...
I'll call my new database "TestoolsDB". Press OK.
1.4 Create some a table with a couple of columns. Create 3 rows of data. Of course, this is just an example:
The was supoused to be called "dummy_data", with double m, but doesn't matter for this example.

2. Create a Windows Form

2.1 Open Visual Studio
2.2 Click on New Project
2.3 Select "Windows Forms", set a name for the solution and press Accept.  I named mine "GridExample":
2.4 Add a DataGridView (look for it in the Tool Box) to the Form.
2.5 Give it a name you can recognize. For example, "testGrid".
3. Create a new connection

3.1 Go to Project>Add new data source...
3.2 Select Database and press Next twice.

3.3 Press New Connection
3.4 Press Browse and select the database you created. It is possible that it is located in the default folder: C:\Program Files\Microsoft SQL Server\MSSQL11.SQLEXPRESS\MSSQL\DATA

3.5 Select your database and press Open. If you have problems with permissions or messages saying that the database is in use, check this. Anyway, all you have to do is grant permission for the .mdf and .ldf files.  If you see a message indicating that the database is in use, just enable the "auto close" feature in the database properties with SQL Server Management Studio. You can see a detailed explanation in this video.
3.6 Press Accept
3.7 Press Next in this screen:
3.8 Now, you will see this (well, in English):
Press Yes to coy the database to the project folder, or No to use it where it is right now. It's up to you. In this example, I'll press Yes.

3.9 Press Next. Now just wait.
3.10 Check "Tables" and press Finish
3.11 Open Server Explorer.
3.12 Highlight your database.
3.13 Copy the connection string in the properties of the new connection.
4. Write the code

4.1 Double click on the form (not the grid). This means that the code will be executed when the form loads. If you want to use a button, double click the button.
4.2 Write the following code. I'm showing the complete code, just in case you have any doubt:

I'm not going to explain the code, but you can read the comments and I'm sure you'll understand what is all about.

Once you have written the code, press F5 to see the results:
And there you have it!!! This simple trick can lead you to make more complex programs.


  1. This is amazing, Thank you!

    I had one problem that I solved, for some reason I could not connect the DB to my VS project because my project was in a folder that was accessible from my homegroup. I moved the folder to my C drive instead and it worked!

  2. well explained,
    how can i Add/update/delete rows from the DataGridView?

  3. Youtube
    Sign Up Here: ...

    In this video, I am going to show you what are the options that you have when it comes to VMWare Training.

    There are five ways to get VMWare trained:

    What is fist Option that you have? It's a 5 day instructor-led VMWare training.

    Online classes are live, Instructor-Led training sessions, usually from 9 to 5 - delivered online. The course diration is Five days - 3 days of which are lectures and 2 days of instructor assisted hands-on lab, and at the end of the week extra days of self managed practice. And and extra week for VMWare Practice.

    Now... what is the soluton? You need VMWare Training that is more conducive to effective learning and covers more content than the official training offered by VMWare!

    We focus on making sure you have the skills critical to get the job you want and in the same time be capable of supporting critical cloud initiatives and datacenter infrastructure.

    Our teachers are highly qualified, each with years of experience. All hold VCP and CVIA certificates. More importantly, they love teaching VMWare online and strive to make your experience as fun, enjoyable and effective as possible.

    Instead of spending thousands of dollars for the official course you will be better of to Sign up for one of our online VMWare training classes and get top quality training for a fraction of the cost.

    Now, Option #2: You can use Personal, One on One VMWare Tutoring services, they are inexpensive and a great fit for busy professionals.

    Now let's take a look at what does the "One on One VMWare Training" session look like?

    Well, you pick up the topics and we deliver the training according to your schedule! You Have a personal VMWare Trainer which will instantly speed up your learning by responding to your individual training needs and questions.

    Sounds great - isn't it...? Today - You can learn VMWare online with the help of Skype and Team Viewer and enjoy high quality training given by our trusted and certified trainers - no matter where in the world you are.

    You need to get the training, but you have to work around busy work and family schedules. Not a problem! Our teachers are available 24 x 7, Monday to Friday and over the weekends! And because each class is only one hour or less, depending on your needs, you can choose to take class 2 or 3 times a week, without significant cost.

    Option #3 VMWare Practice Lab.

    Option #4: If you are preparing yourself for one of the VMWare Exams, and you are not sure what to expect at the exam, there is an Exam preparation training to get you ready for the exam.

    Option #5: Training videos. We created hundreds of videos that you can use to quickly learn and

    Now you can learn VMWare on your own terms, where you want and how you want.

    What I believe is...VMWare vSphere Training has to be affordable, practical and comprehensive. We offer just that with our set of VMWare Training sessions, practice lab and exam preparation.

    Our VMWare Training has been Specifically Designed to develop your VMWare Administrator qualities along with your ability to deal with real-life system problems.

    So don't wait for the last moment click on the link below and sign up for VMWare Training today!

  4. Thanks for sharing this with so much of detailed information, its much more to learn from your article. Keep sharing such good stuff.
    SQL Course