Google Search

Friday

C# Populate GridView With Sorting Facility

 

 

This sample code is for populating a GridView from an oracle table.

It uses

OracleConnection

OracleCommand

OracleDataAdapter

DataSet

GridView Control

 

 

Code for

PopulateGridViewEx1.aspx

 

 

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="PopulateGridViewEx1.aspx.cs" Inherits="PopulateGridViewEx1" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:GridView ID="GridView1" runat="server">
</asp:GridView>

</div>
</form>
</body>
</html>

C# code for PopulateGridViewEx1.aspx.cs

using System;
using System.Collections.Generic;

using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.OracleClient;
//the above line is must for oracleconnection,oraclecmd
using System.Data;
//for DataSet Objects, the above line is needed

public partial class PopulateGridViewEx1 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
FillGridView1();
}
}
private void FillGridView1()
{
GridView1.DataSource = GetDataSource();
//GridView1.DataTextField = "emp_name";
//GridView1.DataValueField = "emp_id";
GridView1.DataBind();
}
private DataSet GetDataSource()
{
String oradb = System.Configuration.ConfigurationSettings.AppSettings["OpenLocalConn"];

OracleConnection conn = new OracleConnection(oradb);
conn.Open();
string sql = "select * from emp";
OracleCommand cmd = new OracleCommand(sql, conn);

OracleDataAdapter adapter = new OracleDataAdapter(cmd);
DataSet ds = new DataSet();
adapter.Fill(ds);
return ds;
}
}

When you are running the above code, you may get the following error:

Compiler Error Message: CS0234: The type or namespace name 'OracleClient' does not exist in the namespace 'System.Data'

For this error, you have to add reference to the oracle client by doing the following clicks: Website menu>Add Reference..>.Net Tab >
System.Data.OracleClient >OK

When the above thing is done, the following line is automatically included in your web.config file.

<add assembly="System.Data.OracleClient, Version=2.0.0.0, Culture=neutral, PublicKeyToken=B77A5C561934E089"/></assemblies>

 

Code of web.config

<configuration>
<system.web>
<pages enableViewState="false" enableViewStateMac="false" />
<customErrors mode="Off"/>
<compilation debug="true">
<assemblies>
<add assembly="System.Data.OracleClient, Version=2.0.0.0, Culture=neutral, PublicKeyToken=B77A5C561934E089"/></assemblies></compilation>
</system.web>
<appSettings>
<add key="OpenLocalConn" value="Data Source=SALES;User Id=sellme;Password=sellme123;"/>
</appSettings>
</configuration>

 

Once the above code was made working, you can format the following thigs:

1. The Header Values of the grid are Just the Field Names of the Database Table. (eg. emp_id,emp_name). Instead of Emp_Name I want to display NAME OF EMPLOYEE. How this can be done.

2. Then,do you want to allow SORTING FACILITY

In the propertites, just make ALLOWSORTNIG=TRUE

Now the Datagrid source code will look like this:

<asp:GridView ID="GridView1" runat="server" AllowPaging="True"
AllowSorting="True" OnSorting="GridView1_Sorting">
</asp:GridView>

But, when you click the sorting link,you may get the error:

The GridView 'GridView1' fired event Sorting which wasn't handled

If you are able to set the DataSourceID property of GridView, this problem will not be there. But I am using oracleclient and using the codes such as

GridView1.DataSource=ds;

GridView1.DataBind();
So you got this error.

<asp:GridView ID="GridView1" runat="server" AllowPaging="True"
AllowSorting="True" OnSorting="GridView1_Sorting">
</asp:GridView>

But this is not Enough

 

3. Then do you want to display the report page by page when records are more.

In the properties, just make ALLOWPAGING=TRUE



 

 

 

 

No comments:

Post a Comment

 
Google Search