ARTICLE

GridView operations with example

Posted by Deepak Rai Articles | ASP.NET Controls in C# September 30, 2010
In this article I am showing you an example of different operations in GridView.
Reader Level:

Example 1 -

This is a example of upadating a GridView at run time.

Step 1:

Drag one GridView Control and one SqlDatasurce from toolbox onto your aspx page..

Step 2:

Create a table in Sql Server, details as follows-

Sql Table Fields.

Table Name -Customers

Column Name-

1:-CustomerID-Varchar(50) with Primary Key
2:-ContactName-Varchar(50)with NoT Null
3:-CompanyName-Varchar(50)

Step 3:

Go to aspx part of asp.net web page-

And add the following mannually

Note:- You can write a custom connection String in place of

ConnectionString="<%$ ConnectionStrings:empConnectionString %>"

Like............

ConnectionString="server=yourservername or use dot(.) only on place of server name;database=databasename;Integrated Security=true or SSPI"

<form id="form1" runat="server">
    <asp:SqlDataSource ID="ds1" runat="server"
       ConnectionString="<%$ ConnectionStrings:empConnectionString %>"
       SelectCommand="SELECT * FROM [Customers]"
       UpdateCommand="Update [Customers] set [ContactName]=@ContactName,[CompanyName]=@CompanyName
       WHERE ([CustomerID] = @CustomerID)">
        <UpdateParameters>
        <asp:Parameter Name="CustomerId" Type="String" />
        <asp:Parameter Name="ContactName" Type="String" />
        </UpdateParameters>
    </asp:SqlDataSource>

Step 4:

Set DataKeyNames="CustomerID" in Grid View Control.

      <asp:GridView ID="GridView1" Runat="server"
      DataSourceID="ds1" DataKeyNames="CustomerID"
      AutoGenerateColumns="False" AllowPaging="True" >
      <Columns>
         <asp:CommandField ShowEditButton="True"></asp:CommandField>
         <asp:BoundField ReadOnly="True" HeaderText="CustomerId"    InsertVisible="False" DataField="CustomerID" SortExpression="CustomerID">
         </asp:BoundField>
         <asp:BoundField HeaderText="ContactName" DataField="ContactName" SortExpression="ContactName">
         </asp:BoundField>
         <asp:BoundField HeaderText="CompanyName" DataField="CompanyName" SortExpression="CompanyName">              
         </asp:BoundField>                                        
      </Columns>
 </asp:GridView>
</form>

Output:

1.gif

Example 2:

This is an example of selecting record from a GridView at run time using details view.

Step 1:

Drag one GridView Control, one Details View Control and Two SqlDatasource from toolbox onto your aspx page..

Step 2:

Create a table in Sql Server, details as follows-

Sql Table Fields.

Table Name -Customers

Coloumn Name-

1:-CustomerID-Varchar(50) with Primary Key
2:-ContactName-Varchar(50)with NoT Null
3:-CompanyName-Varchar(50)

Step 3:

Go to aspx part of asp.net web page-

And add the following mannually

Note:- You can write a custom connection String in place of

ConnectionString="<%$ ConnectionStrings:empConnectionString %>"

Like............

ConnectionString="server=yourservername or use dot(.) only in place of server name;database=databasename;Integrated Security=true or SSPI"

<form id="form1" runat="server">
<asp:SqlDataSource ID="ds1" runat="server"
 ConnectionString="<%$ ConnectionStrings:empConnectionString %>"
 SelectCommand="SELECT [CustomerID] FROM [Customers]">
</asp:SqlDataSource>

Set DataKeyNames="CustomerID" in Grid View Control.

<asp:gridview ID="Gridview1" runat="server" AutoGenerateSelectButton="True"
        DataSourceID="ds1" DataKeyNames="CustomerID">
       </asp:gridview>
        <asp:SqlDataSource ID="ds2" runat="server"
ConnectionString="<%$ ConnectionStrings:empConnectionString2 %>" SelectCommand="SELECT * FROM [Customers] WHERE ([CustomerID] = @CustomerID)">
       <SelectParameters>
        <asp:ControlParameter ControlID="Gridview1" Name="CustomerID"
        PropertyName="SelectedValue" Type="String" />
        </SelectParameters>
      </asp:SqlDataSource>
<asp:DetailsView ID="DetailsView1" runat="server" DataSourceID="ds2">
</asp:DetailsView>
</form>     

Output
 
2.gif

Login to add your contents and source code to this article
post comment
     
COMMENT USING
PREMIUM SPONSORS
Over-C is a holistic consortium of communications and technology specialists. We build, deploy and market both business as well as consumer products and solutions.
SPONSORED BY
  • PDF reports have never been easier to create. With our included WYSIWYG Designer, you can layout your reports, set up your data source and let DynamicPDF ReportWriter do the rest.
Join a Chapter