Aspdotnet-Suresh

aspdotnet-suresh offers C#.net articles and tutorials,csharp dot net,asp.net articles and tutorials,VB.NET Articles,Gridview articles,code examples of asp.net 2.0 /3.5,AJAX,SQL Server Articles,examples of .net technologies

how to bind Datagridview in windows application using c#.net

Dec 27, 2010
Introduction

Here I will explain how to bind Datagridview in windows application using c#.net.

Description

I have started working on windows application at that time I don’t know how to use Datagridview and other controls. I face some problems to bind data to Datagridview and to other controls that’s why I am writing this post to explain how to bind data to Datagridview in windows application. In windows application binding Datagridview is somewhat different when compared with web application Datagridview binding.

First create one windows forms application now form.cs will open after that drag and drop Datagridview from All windows form category after that write the following code in code behind to bind data to Datagridview.


//This connection string is used to connect to database
String strConnection = "Data Source=MYSYSTEM;Initial Catalog=MySamplesDB;Integrated Security=True";
//Establish SQL Connection
SqlConnection con = new SqlConnection(strConnection);
//Open database connection to connect to SQL Server
con.Open();
//Data table is used to bind the resultant data
DataTable dtusers = new DataTable();
// Create a new data adapter based on the specified query.
SqlDataAdapter da = new SqlDataAdapter("Select * from User_Information", con);
//SQl command builder is used to get data from database based on query
SqlCommandBuilder cmd = new SqlCommandBuilder(da);
//Fill data table
da.Fill(dtusers);
//assigning data table to Datagridview
dataGridView1.DataSource = dtusers;
//Resize the Datagridview column to fit the gridview columns with data in datagridview
dataGridView1.AutoResizeColumns(DataGridViewAutoSizeColumnsMode.AllCells);
con.Close();

After bind data to datagridview that should be like this

Demo



If you enjoyed this post, please support the blog below. It's FREE!

Get the latest Asp.net, C#.net, VB.NET, jQuery, Plugins & Code Snippets for FREE by subscribing to our Facebook, Twitter, RSS feed, or by email.

subscribe by rss Subscribe by RSS subscribe by email Subscribe by Email

37 comments :

Unknown said...

thanks sir......

it helps me a lot.....

Anonymous said...

Thank you Sir....
Please sir try to provide more sample codes.
Thank`s for previous codes.

Anonymous said...

thanks a lot

-subodh

Anonymous said...

Hi Sir,
Could I attach your blog into my blog?

-subodh

Suresh Dasari said...

hi subodh copying the content from this blog is not allowed

Unknown said...

Thank You Suresh

Memtech said...
This comment has been removed by a blog administrator.
Anonymous said...

Superb....:)

Anonymous said...

hi..
i tried this code but it is not binding values to the datagridview,displaying empty gridview.

Anonymous said...

set AutoGenerateColumns before binding to datasource

GV.AutoGenerateColumns = true;

Anonymous said...

how to move the single or multiple listitems from one listbox to another listbox using c#.net
and/or
how to move the single or multiple listitems from one gridview to another listbox using c#.net

Suresh Dasari said...

Check this post to move listitems from one listbox to another listbox http://www.aspdotnet-suresh.com/2010/12/how-to-move-listitems-from-one-listbox.html

Anonymous said...

kkk

Unknown said...

but how to delete that selected value in that Grid ????

Anonymous said...

one form contain two grid view 1st grid view contain one column that column name is inventoryno,it is a primarykey if double clik one row in that grid view then based on inventoryno all data display in another grid view, after display in that 1st grid view selected row should be clear after saving/which row we selected in inventory column how to write this coding using WCF in ASP.NET(Windows based c#)

Anonymous said...

thank you......

Unknown said...

how to add a row dynamically after pressing enter key in asp.net

Unknown said...

How to update and also insert the data in this Grid from UI

Unknown said...

exilent sir,i m so happy..

thanks sir................

hermes said...

Very insightful! Thanks, Sir!

Unknown said...

can any body add combobox to datagrid and bind data in that combo...?

Anonymous said...

i used above method. but not desired output. when i debug, dataset gets filled, but i cant see on grid view... :(

Unknown said...

try
{
MailMessage mail = new MailMessage();
SmtpClient SmtpServer = new SmtpClient("smtp.gmail.com");

// mail.From = new MailAddress(textBox2.Text);

mail.To.Add(dataGridView1.Rows[i].Cells["e_Mail"].ToString());

mail.Subject = textBox3.Text;
mail.Body = richTextBox1.Text;

SmtpServer.Port = 25;
SmtpServer.Credentials = new NetworkCredential("myemail@gmail.com", "password");
SmtpServer.EnableSsl = true;

SmtpServer.Send(mail);
MessageBox.Show("mail Send");
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}

Unknown said...

plz i send multiple mail, but not to add in email adddress of my datagrid view
it's a databind
help me

Veera Mudgal said...

while scrolling rows of grid view are getting overlapped

Unknown said...

thank you very much sir..your code helps me alot

Mercy said...

Hi its a nice blog!!1

Anonymous said...

sir i have binded datagridview with dataset(using database commands , plz can u help me that how can i pick few rows from that datagridview using check boxes and put them into another table in database..in application regarding Purchase and Purchase Return
thank you

Rajeev NR said...

hai sir,... i tried this code. bt i cant use 'datatable'..getting this error ---> " The type or namespace name 'DataTable' could not be found (are you missing a using directive or an assembly reference?) "
please help me...what can i do ? im a child in asp.net. pls reply me.... thanku....

celebrity said...

I have solve the Datagridview problem, thanks!

Unknown said...

Sir,I am trying to bind datagridview with followig code but after execution it is not binding values to the datagridview,displaying empty gridview. please help me.

SqlConnection sqlCon = new SqlConnection("server=.;database=mydatabase;integrated security=true");
SqlCommand cmd = new SqlCommand("select * from studentAttendence", sqlCon);
DataSet ds = new System.Data.DataSet();
SqlDataAdapter sqlDa = new SqlDataAdapter(cmd);
cmd.Connection = sqlCon;
sqlDa.Fill(ds);
dataGridView1.AutoGenerateColumns = true;
dataGridView1.DataSource = ds;

Unknown said...

Sir,I am trying to bind datagridview with followig code but after execution it is not binding values to the datagridview,displaying empty gridview. please help me.

SqlConnection sqlCon = new SqlConnection("server=.;database=mydatabase;integrated security=true");
SqlCommand cmd = new SqlCommand("select * from studentAttendence", sqlCon);
DataSet ds = new System.Data.DataSet();
SqlDataAdapter sqlDa = new SqlDataAdapter(cmd);
cmd.Connection = sqlCon;
sqlDa.Fill(ds);
dataGridView1.AutoGenerateColumns = true;
dataGridView1.DataSource = ds;

hi said...

how to bind nested datagridcombocolumn in window applicaton pls help...@@@

Unknown said...

it helped me today

SATEESH said...

i want editable textbox gridview. means if am clicking on gridview cell am writing something and update it at outside button.

SATEESH said...

like below link in asp.net

http://msdn.microsoft.com/en-us/library/ms972948.aspx(FIGURE 41)

Narendra Kumar said...

Hello sir,
how i can show image in datagridview from database in windows application

Give your Valuable Comments

Note: Only a member of this blog may post a comment.

© 2015 Aspdotnet-Suresh.com. All Rights Reserved.
The content is copyrighted to Suresh Dasari and may not be reproduced on other websites without permission from the owner.