datagridview in desktop application-showing data

Datagridview in desktop application-showing data

Hello friends, how are you? And how is your programming?

Today we will try to fetch some data from database and will show in the windows form. To do so, we need to use a Data Control.  We will use datagridview control by expanding Data and then  drag the control to the form.  See the image for help.

adding datagridview to my desktop application
adding datagridview to my desktop application

After adding Datagridview control to the form we need to write some code to bind the Datagridview. See the look of the form after adding datagridview control.

datagridview design in desktop application
datagridview design in desktop application

We will use previous example of desktop application database connectivity and the data will be shown relative to user login i.e. when a user will login, we will show their details of registration. For this purpose we need to create an extra table name tblregistrationdetails. Here is the all sql queries:

USE [mydatabase]
CREATE TABLE [dbo].[tblUser](
[id] [bigint] IDENTITY(1,1) NOT NULL,
[username] [varchar](50) NULL,
[password] [varchar](50) NULL
)

 

insert into dbo.tblUser(username,password) values (‘vipin’,’mishra’)
insert into dbo.tblUser(username,password) values (‘user’,’pass’)

If you dont have mydatabase you can create it by using sql query :

create database mydatabase

CREATE TABLE [dbo].[tblRegistrationdetails](
[id] [bigint] IDENTITY(1,1) NOT NULL,
[name] [varchar](200) NULL,
[emailid] [varchar](300) NULL,
[phone] [varchar](100) NULL,
[Address] [varchar](500) NULL,
[Username] [varchar](250) NULL,
CONSTRAINT [PK_tblRegistrationdetails] PRIMARY KEY CLUSTERED
(
[id] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]

insert into dbo.tblRegistrationdetails(username,name,emailid,phone,address) values(‘vipin’,’vipin mishra’,’vipin@pjsindia.com’,’8981881162′,’kolkata’)
insert into dbo.tblRegistrationdetails(username,name,emailid,phone,address) values(‘user’,’another user’,’test@pjsindia.com’,’8981881162′,’kolkata’)

 

In this example we also create a procedure LoadGridviewdata  to bind the gridview in case only if the   user is logged  in. LoadGridviewdata accepts one parameter username to show the relative registration details of that particular user.  To select that particular data we need to use where clause in sql query.

Select * from dbo.tbluserregistrationdetails where username=’username’

 

Now build the project and see the result:

You can see when you will use ‘vipin/mishra’ as username password combination, the datagridview loaded with vipin’s details and when ‘user/pass’ combination is used, the details of the user/pass is loaded.

Here is the coding:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace desktoplogin
{
public partial class Form1 : Form
{
SqlConnection con = new SqlConnection(“Data Source=.\\sqlexpress;Initial Catalog=mydatabase;Integrated Security=True”);
public Form1()
{
InitializeComponent();
}

private void btnLogin_Click(object sender, EventArgs e)
{
DataTable dt = new DataTable();
string UserName = txtUsername.Text.ToString();
string Password = txtPassword.Text.ToString();
SqlCommand cmd = new SqlCommand();
cmd.Connection = con;
cmd.CommandType = CommandType.Text;
cmd.CommandText = “Select * from dbo.tblUser where username='” + UserName+”‘ and password='”+Password+”‘”;
SqlDataAdapter da = new SqlDataAdapter(cmd);
da.Fill(dt);
if (dt != null && dt.Rows.Count > 0)
{
UserName = dt.Rows[0][“username”].ToString();
Password = dt.Rows[0][“password”].ToString();
}
if (txtUsername.Text.Trim() == UserName && txtPassword.Text.Trim() == Password)
{
MessageBox.Show(“Login Successful”);
LoadGridviewdata(UserName);
}
else
{
MessageBox.Show(“wrong username and password combination!!”);
gvData.DataSource = null;
}

}
private void LoadGridviewdata(string username)
{
DataTable dt = new DataTable();

SqlCommand cmd = new SqlCommand();
cmd.Connection = con;
cmd.CommandType = CommandType.Text;
cmd.CommandText = “Select * from dbo.tblRegistrationdetails where username='” + txtUsername.Text.Trim() + “‘”;
SqlDataAdapter da = new SqlDataAdapter(cmd);
da.Fill(dt);
if (dt != null)
{
gvData.DataSource = dt;
}

}
}

}

To download the project click Use of datagridview in desktop application.

Happy coding

3 Replies to “datagridview in desktop application-showing data”

Leave a Reply

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