Hi,
As for the GridView control, its "DataBind" method will always use call the
GetDataSource function which will determine how to obtain the datasource
for bindcing. Here is the code from reflector:
===================
protected virtual IDataSource GetDataSource()
{
if ((!base.DesignMode && this._currentDataSourceValid) &&
(this._currentDataSource != null))
{
return this._currentDataSource;
}
IDataSource source = null;
string dataSourceID = this.DataSourceID;
if (dataSourceID.Length != 0)
{
Control control = DataBoundControlHelper.FindControl(this,
dataSourceID);
if (control == null)
{
throw new
HttpException(SR.GetString("DataControl_DataSourceDoesntExist", new
object[] { this.ID, dataSourceID }));
}
source = control as IDataSource;
if (source == null)
{
throw new
HttpException(SR.GetString("DataControl_DataSourceIDMustBeDataControl", new
object[] { this.ID, dataSourceID }));
}
}
return source;
}
===================
As you can see, as long as the DataSourceID is not an empty string, it will
use it. For your scenario, I suggest you consider programmatically set the
DataSourceID to empty string when you want not to use the DataSource(use
the manually queried record sets). How do you think?
Sincerely,
Steven Cheng
Microsoft MSDN Online Support Lead
==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx#notif
ications.
Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscriptions/support/default.aspx.
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.
--------------------
>From: "jrl" <jrl@newsgroup.nospam>
>Newsgroups: microsoft.public.dotnet.languages.csharp
>Subject: seeking correct approach to combine features of linq datasource
vs datasourceID
>I'm beginning with linq using C#, and I'm looking for the correct approach
>to drawing data into a gridview.
[quoted text clipped - 40 lines]
>of DataSourceID (parameter control displays) as well as DataSource (select
>where filtering)?
jrl - 05 Feb 2008 05:33 GMT
Steven,
the key part you wrote was:
as long as the DataSourceID is not an empty string, it (the gridview) will
use it (the DataSourceID). For your scenario, I suggest you consider
programmatically set the
DataSourceID to empty string when you want not to use the DataSource
So to summarize my goal again: I want to use the DatasourceID sometimes
(when I bind it to parameters of a drop down list) and to a Datasource at
other times, when I use linq queries.
You recommend that I set DataSourceID to an empty string when I want to
substitute the Datasource as the source for the Gridview.Databind()
function. (I had some trouble understanding you at first, so I hope my
interpretation is what you intended).
Here's my attempt to do what you reccommend:
protected void Button1_Click(object sender, EventArgs e)
{
string temp = GridView1.DataSourceID; // so that I can reset the
name
GridView1.DataSourceID = ""; // set to an empty string as
recommended, now gridview should use datasource instead
PagevisitDataContext db = new PagevisitDataContext();
var visits = from visit in db.pagevisits
select visit;
GridView1.DataSource = visits; // now datasource is set
GridView1.DataBind(); // refresh with new data
GridView1.DataSourceID = temp; // restore the DataSourceID so I can
easily sort and filter
}
When I run my test application, it loads the Gridview normally, using
DataSourceID. As soon as I click button1, (above), I get this error:
Both DataSource and DataSourceID are defined on 'GridView1'. Remove one
definition.
It seems my method of setting the DataSourceID to an empty string, is not
working properly. How would you recommend that I do this so that the
DataSourceID becomes, truly and temporarily, undefined?
Steven Cheng[MSFT] - 07 Feb 2008 07:03 GMT
Hi Jrl,
I think the exception you mentioned is due to you set the DataSourceID
property back after you call "DataBind" method. At that time, GridView has
a certain flag marked and indicate that a DataSource object is used, if you
set a non empty value to DataSourceID, the runtime will think it illegal.
So far my suggestion is as below:
You can always let the Gridview be set to default DatasourceID in Page's
Load event(this is the one you'll use normally) and in the Button's click
event, just change the DataSourceID to the custom one, do not set it back
after yo call databind. e.g.
-======================
public partial class GridViewPage : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
GridView1.DataSourceID = SqlDataSource1.ID;
}
protected void Button1_Click(object sender, EventArgs e)
{
DataTable dt = new DataTable("items");
dt.Columns.Add("id", typeof(long));
dt.Columns.Add("name", typeof(string));
dt.Rows.Add(11, "item11");
GridView1.DataSourceID = string.Empty;
GridView1.DataSource = dt;
GridView1.DataBind();
}
}
=========================
Sincerely,
Steven Cheng
Microsoft MSDN Online Support Lead
This posting is provided "AS IS" with no warranties, and confers no rights.
--------------------
>X-Trace-PostClient-IP: 70.67.40.71
>From: "jrl" <jrl@newsgroup.nospam>
>Newsgroups: microsoft.public.dotnet.languages.csharp
>Subject: Re: seeking correct approach to combine features of linq
datasource vs datasourceID
>Steven,
>
[quoted text clipped - 39 lines]
>working properly. How would you recommend that I do this so that the
>DataSourceID becomes, truly and temporarily, undefined?
jrl - 07 Feb 2008 15:50 GMT
Thanks very much. Your example was very helpful in getting it to work now.
Jrl
> Hi Jrl,
>
[quoted text clipped - 94 lines]
>>working properly. How would you recommend that I do this so that the
>>DataSourceID becomes, truly and temporarily, undefined?