Connecting Tech Pros Worldwide Forums | Help | Site Map

Data Grid, SQL database

Newbie
 
Join Date: Oct 2009
Posts: 1
#1: 4 Weeks Ago
How to get data into datagrid from SQL database using SQl language...in visual studio...pplz help me out...thnx in advance

tlhintoq's Avatar
Moderator
 
Join Date: Mar 2008
Location: Arizona, USA
Posts: 1,791
#2: 4 Weeks Ago

re: Data Grid, SQL database


How To Use A Database In Your Program
How To Use A Database In Your Program Part II
Frinavale's Avatar
Site Moderator
 
Join Date: Oct 2006
Location: The Great White North
Posts: 5,168
#3: 4 Weeks Ago

re: Data Grid, SQL database


Once you've retrieved the data from the database you can set the GridView's DataSource Property. Please research how to do this using google or the MSDN library. There is a nice example in the MSDN library on how to do this here: Grid View Examples...

-Frinny
ThatThatGuy's Avatar
Member
 
Join Date: Jul 2009
Location: Mumbai--India
Posts: 48
#4: 4 Weeks Ago

re: Data Grid, SQL database


Follow this example for loading a data from database to datagrid or gridview

First you need to connect to the database using instance of SqlConnection class... im doing it in C#
Expand|Select|Wrap|Line Numbers
  1. SqlConnection sqlcon=new SqlConnection("enter the query string here");
  2.  
Now you need to use the SqlDataAdapter object
Expand|Select|Wrap|Line Numbers
  1. SqlDataAdapter ada=new SqlDataAdapter("select string",sqlCon);
  2.  
now that you have loaded the data from the databse in the adapter
you now need to have the dataset to fill the data retrieved

Expand|Select|Wrap|Line Numbers
  1. DataSet dSet=new DataSet();
  2.  
Now use the adapter's fill method to fill the data to the dataset

Expand|Select|Wrap|Line Numbers
  1. ada.Fill(dSet);
  2.  
Now assign the dataset to the gridview or datagrid

Expand|Select|Wrap|Line Numbers
  1. GridView1.DataSource=dSet;
  2.  
and the nyou need to bind the datagrid to the control
Expand|Select|Wrap|Line Numbers
  1. GridView1.DataBind();
  2.  
Reply

Tags
datagrid