Idea List  
Easy solution using Grid View
Rate this content:                         
Dear Reader,

I'm taking Northwind as database for this sample application.

 

I have created a simple grid using sql datasource.

 

<asp:GridView ID="grdCategories"

runat="server" AutoGenerateColumns="False" DataKeyNames="CategoryID" DataSourceID="SqlDataSource1">

<Columns>

<asp:BoundField DataField="CategoryName" HeaderText="CategoryName" SortExpression="CategoryName" />

<asp:BoundField DataField="Description" HeaderText="Description" SortExpression="Description" />

<asp:CheckBoxField DataField="IsCategory" HeaderText="IsCategory" SortExpression="IsCategory" />

</Columns>

</asp:GridView>

<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:NorthwindConnectionString %>" <%$ ConnectionStrings:NorthwindConnectionString %>SelectCommand="SELECT [CategoryID], [CategoryName], [Description], [IsCategory] FROM [Categories]"

></asp:SqlDataSource>

 

This example will help us to list all the categories from the database. NorthwindConnectionString is stored in Web.config under connection section.

 

In order use custom value you can use Eval keyword in ItemTemplate.

 

<asp:GridView ID="grdCategories" runat="server" AutoGenerateColumns="False" DataKeyNames="CategoryID"

DataSourceID="SqlDataSource1">

<Columns>

<asp:BoundField DataField="CategoryName" HeaderText="CategoryName" SortExpression="CategoryName" />

<asp:TemplateField HeaderText="Description">

<ItemTemplate>

<asp:Label ID="lblDescription" runat="server" Text='<%# Eval("Description") %>'>asp:Label>

</ItemTemplate>

</asp:TemplateField>

<asp:CheckBoxField DataField="IsCategory" HeaderText="IsCategory" SortExpression="IsCategory" />

</Columns>

</asp:GridView>

<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:NorthwindConnectionString %>"<%$ ConnectionStrings:NorthwindConnectionString %>

SelectCommand="SELECT [CategoryID], [CategoryName], [Description], [IsCategory] FROM [Categories]">

</asp:SqlDataSource>

 

In Eval you can use convert.tostring method to format the text you want to display like formating the input string like Date format or Currency format.

 

In the next page you will know how to edit or delete row in the grid view.

Idea List  
   © 2009 Development Next. All Rights Reserved. | Terms of Use | Trademarks