Thursday, 23 June 2016

Bind DataGridView With Sql Server Table Using C#.Net In Windows Application

6/23/2016 - By Pranav Singh 0

This article will show you how you can bind datagridview with sql server table using c#.net in windows application. In this I have used data table to store the data of sql table and then display it in gridview by C#.net          .
So for this article first we will create a new windows application and add the datagridview control in the form.



Now add the below code into the form.

using System;
using System.Data;
using System.Data.SqlClient;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
    public partial class Form3 : Form
    {
        public Form3()
        {
            InitializeComponent();
        }

        private void Form3_Load(object sender, EventArgs e)
        {
            DataTable dt = new DataTable();
            dt = GetDataFromDataBase();
            dataGridView1.DataSource = dt;
        }
        /// <summary>
        /// Get you data
        /// </summary>
        /// <returns></returns>
        public DataTable GetDataFromDataBase()
        {
            DataTable dt = new DataTable();
            SqlConnection con = new SqlConnection("----Your connection string-----");
            string query = "Select * from CountryTable;";
            SqlDataAdapter da = new SqlDataAdapter(query, con);
            da.Fill(dt);
            return dt;
        }
    }
}

In above code I have used a function GetDataFromDataBase. This function will access the data from the sql database table. Now we will call the data function to bind the data to datagridview. Now we have done run the application to check the output.


About the Author

We are the group of people who are expertise in different Microsoft technology like Asp.Net,MVC,C#.Net,VB.Net,Windows Application,WPF,jQuery,Javascript,HTML. This blog is designed to share the knowledge.

Get Updates

Subscribe to our e-mail newsletter to receive updates.

Share This Post

0 comments:

Please let me know your view

Free Ebooks


About Us

We are the group of people who are expertise in different Microsoft technology like Asp.Net,MVC,C#.Net,VB.Net,Windows Application,WPF,jQuery,Javascript,HTML. This blog is designed to share the knowledge.

Contact Us

For writing article in this website please send request by your

GMAIL ID: dotnetpools@gmail.com

Bugs and Suggestions

As we all know that this website is for sharing knowledge and providing proper solution. So while reading the article is you find any bug or if you have any suggestion please mail us at contact@aspdotnet-pools.com.

Partners


Global Classified : Connectseekers.com
© 2014 aspdotnet-pools.com Designed by Bloggertheme9.
back to top