getdata() - where is this to be defined?

J

jrl

I am working with a code snippet from a site
(http://www.gridviewguy.com/ArticleDetails.aspx?articleID=176_Sorting_GridView_Manually!)
as follows:

private void SortGridView(string sortExpression,string direction)
{

// You can cache the DataTable for improving performance

DataTable dt = GetData().Tables[0];

DataView dv = new DataView(dt);

dv.Sort = sortExpression + direction;

GridView1.DataSource = dv;

GridView1.DataBind();

}

But I can't get it to compile (in C#) because it doesn't understand the
GetData() function in the line:
DataTable dt = GetData().Tables[0];

What would I need to include or change to help it understand this line?
 
M

Marc Gravell

How about looking at the GetData() method, the first thing in the
page?

btw, since DataView supports IBindingList for sorting, there may be
alternatives to strings [just for completeness]:

DataTable dt = null; // todo...
IBindingList bl = dt.DefaultView;
ListSortDirection direction = ListSortDirection.Ascending;
PropertyDescriptor property =
TypeDescriptor.GetProperties(bl)["SomeColumn"];
bl.ApplySort(property, direction);
// now use as a data source

Marc
 
I

Ignacio Machin \( .NET/ C# MVP \)

Hi,


You need the code for that method. from the code you posted it seems that it
returns a dataset from where you are getting the first table. Most probably
it use DataAdapter.Fill to get data from a database
 
S

Steven Cheng[MSFT]

Hi Jrl,

As other members have mentioned, the "GetData" method is just self defined
function for return the datasource objects(here it return DataTable
collection). You can define such a "GetData" function in your page's
codebehind class(or you can also define it in a dedicated utility class as
static or member function).

I've read the article you mentioned, it seems it has provided you a sample
implemenation of the "GetData" function as below:

============
private DataSet GetData()
{

SqlConnection myConnection = new SqlConnection(ConnectionString);

SqlDataAdapter ad = new SqlDataAdapter("SELECT * FROM Categories",
myConnection);

DataSet ds = new DataSet();

ad.Fill(ds);

return ds;

}
============

You can write your own one and put it at the place I mentioned above.

Sincerely,

Steven Cheng

Microsoft MSDN Online Support Lead


This posting is provided "AS IS" with no warranties, and confers no rights.

--------------------
From: "jrl" <[email protected]>
Newsgroups: microsoft.public.dotnet.languages.csharp
Subject: getdata() - where is this to be defined?
Date: Thu, 24 Jan 2008 09:41:06 GMT
I am working with a code snippet from a site
(http://www.gridviewguy.com/ArticleDetails.aspx?articleID=176_Sorting_GridV iew_Manually!)
as follows:

private void SortGridView(string sortExpression,string direction)
{

// You can cache the DataTable for improving performance

DataTable dt = GetData().Tables[0];

DataView dv = new DataView(dt);

dv.Sort = sortExpression + direction;

GridView1.DataSource = dv;

GridView1.DataBind();

}

But I can't get it to compile (in C#) because it doesn't understand the
GetData() function in the line:
DataTable dt = GetData().Tables[0];

What would I need to include or change to help it understand this line?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top