- How to connect with database
- How to get data from table
- How to display table data in DataGridView
For achieving this we need
- Visual Studio 2013 Professional
- SQL Server
- SQL Management Studio
- .NET Framework
- Knowledge of programming in C#
So, first of all we need to know what is Data Binding
DATA BINDING:
Data binding is the process to connect application user interface with business logic. In simple words you get the data from database using linq to SQL and bind it as per requirement and display on user interface of application.
Now, lets do some programming to achieve the target
STEP-1:
Create new Windows Form Project and create new form named data.cs and then open toolbox (Ctrl+Alt+X) and search DataGridView drag this element to the window screen.
ASSUMPTIONS:
Before start coding we assume that
- We have DataConText Class named abcDataContext
- We have table named customers
- DataGridView named dataGridView1
DATA FETCHING AND DATA BINDING:
In Linq to SQL data context object is responsible for establishing a connection with a data source (database).
//create datacontext object
abcDataConText dc = new abcDataContext();
// create object of table customers
customers customer = new customers();
// now assign data to this table customer using linq to sql query
var allcustomers = from c in dc.GetTable<customers>() select c;
// now bind data
customerBindingSource.DataSource = allcustomers;
//now assign this data source to data grid view
dataGridView1.DataSource = customerBindingSource;
That's all, when you execute the source program you will get the result.
Thank you.
No comments:
Post a Comment