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. |
|||
|
|||
37 comments :
thanks sir......
it helps me a lot.....
Thank you Sir....
Please sir try to provide more sample codes.
Thank`s for previous codes.
thanks a lot
-subodh
Hi Sir,
Could I attach your blog into my blog?
-subodh
hi subodh copying the content from this blog is not allowed
Thank You Suresh
Superb....:)
hi..
i tried this code but it is not binding values to the datagridview,displaying empty gridview.
set AutoGenerateColumns before binding to datasource
GV.AutoGenerateColumns = true;
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
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
kkk
but how to delete that selected value in that Grid ????
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#)
thank you......
how to add a row dynamically after pressing enter key in asp.net
How to update and also insert the data in this Grid from UI
exilent sir,i m so happy..
thanks sir................
Very insightful! Thanks, Sir!
can any body add combobox to datagrid and bind data in that combo...?
i used above method. but not desired output. when i debug, dataset gets filled, but i cant see on grid view... :(
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());
}
plz i send multiple mail, but not to add in email adddress of my datagrid view
it's a databind
help me
while scrolling rows of grid view are getting overlapped
thank you very much sir..your code helps me alot
Hi its a nice blog!!1
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
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....
I have solve the Datagridview problem, thanks!
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;
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;
how to bind nested datagridcombocolumn in window applicaton pls help...@@@
it helped me today
i want editable textbox gridview. means if am clicking on gridview cell am writing something and update it at outside button.
like below link in asp.net
http://msdn.microsoft.com/en-us/library/ms972948.aspx(FIGURE 41)
Hello sir,
how i can show image in datagridview from database in windows application
Note: Only a member of this blog may post a comment.