How to Create DataSource control using Gridview control in ASP.net


In this article I'm trying to explain how to create DataSource controls like LinqDataSource, XMLDataSource etc. Using Gridview control in ASP.net. During this process we can able to learn how to create DataSource controls and how to configure with Gridview control for displaying data into that. This article will help you for beginners.

How to Create DataSource control using Gridview control in ASP.net:



In this article I'm trying to explain how to create DataSource controls like LinqDataSource, XMLDataSource etc.. Using Gridview control in ASP.net. During this process we can able to learn how to create DataSource controls and how to configure with Gridview control for displaying data into that. This article will help you for beginners.

Follow below steps to achieve your goal.

Step-1:



Design a page by right click on project and then choose Add NewItem option then Choose WebForm and give a name for that as GVDataSource.aspx. Refer below screen shot for more information.

1

Step-2:



Then open a designer page and simply drag & drop one Gridview control to the design view and select the Choose Data Source dropdown and then choose New Data Source option for creating New Data Source control purpose, as shown in below fig.

2

Step-3:



Once we choose New Data Source option, then a new Data Source Configuration wizard window will appear on the screen. Choose your Data Source type and click on OK button automatically new Data Source control has been created for that control. In below scenario I choose Linq Data Source control.

3

Step-4:



Choose your Data Context object available in dropdown menu and then click on Next button.

4

Step-5:



When I click on Next button its redirect to new window for configure data selection purpose. Choose your table name and then choose columns what do you want to display in output. In the below screen I just choose my table name as Employee and I choose columns are “employee name, job, department", as shown in below fig.

5

Step-6:



Just click Finish button for creating Data Source control, now it's ready to use that Data Source control while display the information into our Application.

6

Step-7:



Just see the code behind how the code looks become changes.

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"

DataSourceID="LinqDataSource1">


<Columns>
<asp:BoundField DataField="Emp_Name" HeaderText="Emp_Name" ReadOnly="True"
SortExpression="Emp_Name" />
<asp:BoundField DataField="Emp_job" HeaderText="Emp_job" ReadOnly="True"
SortExpression="Emp_job" />
<asp:BoundField DataField="Emp_Dep" HeaderText="Emp_Dep" ReadOnly="True"
SortExpression="Emp_Dep" />
</Columns>
</asp:GridView>

<asp:LinqDataSource ID="LinqDataSource1" runat="server"
ContextTypeName="DataClassesDataContext" EntityTypeName=""
Select="new (Emp_Name, Emp_job, Emp_Dep)" TableName="tblEmployees">
</asp:LinqDataSource>




Step-8:



Now the times to execute the code in browser just press F5 on your keyboard and see the result.

7

Conclusion :



I hope this article will help you those who are looking for the same and those who are new to implement Gridview control to configure with Data Source controls.

About Author:



This is Naveen, I completed my MCA and I have around 3 years of experience in ASP.net while implement Web Applications. For past 3 years I learn no.of Technologies while implement my applications apart from that I try to learn new technologies and I try to implement that while implement my project.


Article by naveensanagasetti
I hope you enjoyed to read my article, If you have any queries out of this then please post your comments.

Follow naveensanagasetti or read 139 articles authored by naveensanagasetti

Comments

No responses found. Be the first to comment...


  • Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
  • No HTML formatting and links to other web sites are allowed.
  • This is a strictly moderated site. Absolutely no spam allowed.
  • Name:
    Email: