Friday, 6 June 2014

Bind DataGridView In Windows Application Using C#

6/06/2014 - By Pranav Singh 0

Whenever create a windows application on that case we always face some situation where we needed to display record in grid form. So in this application I will show you how you can bind a datagridview control in your windows application using C#. In this I will use ms access database to access the data.

In my previous article I have shown your how you can bind a gridview control in asp.net application check the article Binding Gridview By Access DataBase Using C#.Net in Asp.Net.



Now for this article first we will create a new windowsapplication and add a DataGridView Control in it.
Now check the below code
C#.Net
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.OleDb;
using System.Drawing;
using System.Windows.Forms;

namespace DemoWindowsApplication
{
       /// <summary>
       /// Description of MainForm.
       /// </summary>
       public partial class MainForm : Form
       { 
              public string connectionstring = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=bookstore.mdb;Persist Security Info=False;";
              /// <summary>
              /// Default constructor for main form
              /// </summary>
              public MainForm()
              {
                     InitializeComponent();
              }
                          
              void MainFormLoad(object sender, EventArgs e)
              {
                     DataTable _objdt = new DataTable();
            _objdt = GetDataFromDataBase();
            if (_objdt.Rows.Count > 0)
            {
                dataGridView1.DataSource = _objdt;
            }             
              }
               /// <summary>
        /// Function for binding retriving the data from database
        /// </summary>
        /// <returns></returns>
        public DataTable GetDataFromDataBase()
        {
            DataTable _objdt = new DataTable();
            string querystring = "select author_name,title,publication_year from Books;";
            OleDbConnection _objcon=new OleDbConnection(connectionstring);
            OleDbDataAdapter _objda = new OleDbDataAdapter(querystring, _objcon);
            _objcon.Open();
            _objda.Fill(_objdt);
            return _objdt;
        }
       }
}

VB.Net
Imports System
Imports System.Collections.Generic
Imports System.Data
Imports System.Data.OleDb
Imports System.Drawing
Imports System.Windows.Forms

Namespace DemoWindowsApplication
    ''' <summary>
    ''' Description of MainForm.
    ''' </summary>
    Partial Public Class MainForm
        Inherits Form
        Public connectionstring As String = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=bookstore.mdb;Persist Security Info=False;"
        ''' <summary>
        ''' Default constructor for main form
        ''' </summary>
        Public Sub New()
            InitializeComponent()
        End Sub

        Private Sub MainFormLoad(ByVal sender As Object, ByVal e As EventArgs)
            Dim _objdt As New DataTable()
            _objdt = GetDataFromDataBase()
            If _objdt.Rows.Count > 0 Then
                dataGridView1.DataSource = _objdt
            End If
        End Sub
        ''' <summary>
        ''' Function for binding retriving the data from database
        ''' </summary>
        ''' <returns></returns>
        Public Function GetDataFromDataBase() As DataTable
            Dim _objdt As New DataTable()
            Dim querystring As String = "select author_name,title,publication_year from Books;"
            Dim _objcon As New OleDbConnection(connectionstring)
            Dim _objda As New OleDbDataAdapter(querystring, _objcon)
            _objcon.Open()
            _objda.Fill(_objdt)
            Return _objdt
        End Function
    End Class
End Namespace

Now in above code first we have created a function whose return type is DataTale and on page load we have bind the datagridview control.

Now run the application


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