J
jrl
I'm beginning with linq using C#, and I'm looking for the correct approach
to drawing data into a gridview.
In one case, the gridview is using a datasourceID because I want it to
respond to where parameters from a drop down list.
<asp:GridView ID="GridView1" DataSourceID="LinqDataSource1"> (edited for
brevity)
<asp:LinqDataSource ID="LinqDataSource1" runat="server"
ContextTypeName="PagevisitDataContext" EnableDelete="True"
EnableInsert="True"
EnableUpdate="True" TableName="pagevisits" Where="ip == @ip">
<WhereParameters>
<asp:ControlParameter ControlID="DropDownList1" Name="ip"
PropertyName="SelectedValue" Type="String" />
</WhereParameters>
</asp:LinqDataSource>
This part works fine, and now I want to use buttons to delete the group that
is selected above, or to show all the database entries (rather than filtered
as in the first case). The button to show all looked like the following:
protected void Button1_Click(object sender, EventArgs e)
{
PagevisitDataContext db = new PagevisitDataContext();
var visits = from visit in db.pagevisits
select visit;
GridView1.DataSource = visits;
GridView1.DataBind();
}
This uses the DataSource. But I find I have to choose one or the other
(DatasourceID or DataSource). So, how then could I manage to provide these
two functions? I don't think I can convert the functions under the
Button1_Click so that it uses DatasourceID. And the gridview has to use
DatasourceID in order to link up with the drop down list. So I'm stuck.
I suppose it would be possible to use two gridviews, one useing datasourceID
and the other using DataSource, but I think it would be much better if I
could learn an approach that allows me to operate on the database with one
gridview. What is the recommended approach when one wants both the benefits
of DataSourceID (parameter control displays) as well as DataSource (select
where filtering)?
to drawing data into a gridview.
In one case, the gridview is using a datasourceID because I want it to
respond to where parameters from a drop down list.
<asp:GridView ID="GridView1" DataSourceID="LinqDataSource1"> (edited for
brevity)
<asp:LinqDataSource ID="LinqDataSource1" runat="server"
ContextTypeName="PagevisitDataContext" EnableDelete="True"
EnableInsert="True"
EnableUpdate="True" TableName="pagevisits" Where="ip == @ip">
<WhereParameters>
<asp:ControlParameter ControlID="DropDownList1" Name="ip"
PropertyName="SelectedValue" Type="String" />
</WhereParameters>
</asp:LinqDataSource>
This part works fine, and now I want to use buttons to delete the group that
is selected above, or to show all the database entries (rather than filtered
as in the first case). The button to show all looked like the following:
protected void Button1_Click(object sender, EventArgs e)
{
PagevisitDataContext db = new PagevisitDataContext();
var visits = from visit in db.pagevisits
select visit;
GridView1.DataSource = visits;
GridView1.DataBind();
}
This uses the DataSource. But I find I have to choose one or the other
(DatasourceID or DataSource). So, how then could I manage to provide these
two functions? I don't think I can convert the functions under the
Button1_Click so that it uses DatasourceID. And the gridview has to use
DatasourceID in order to link up with the drop down list. So I'm stuck.
I suppose it would be possible to use two gridviews, one useing datasourceID
and the other using DataSource, but I think it would be much better if I
could learn an approach that allows me to operate on the database with one
gridview. What is the recommended approach when one wants both the benefits
of DataSourceID (parameter control displays) as well as DataSource (select
where filtering)?