gridview switching datasources in the codebehind. ... Delete no longer happens.

J

jobs

I've got a gridview that does not have a datasourceid assigned in the
markup.

I'd like to switch between two datasources in the codebehind. when I do
switch, I first reset the the
gridviewx.datasource = nothing and gridviewx.datasourceid = nothing
before setting it to gridviewx.datasource = newdatasoruceid and then
rebinding.

All appears to work well until I try to excute a delete command that
otherwise works great when the datasource is set fixed in the markup.
Except now, nothing happens.

I've tried adding ondeleting and ondeleted, the code never gets to
ondeleted when I switch datasources in the codebehind.

I there something else I need to set, or are things broken beyond
repair at that point. If so, shame as the potential of muliple
datasources offers a lot of options that help simplify sql.

Thanks for any help or information.
 
M

Mohsin Rizvi

Hello jobs!

Thanks for posting a topic.

It could be difficult to understand what actually you are trying to
accomplish.

Kindly paste the code here so that the problem could be resolved.

Regards,
Mohsin Rizvi
 
J

jobs

have a simple gridview that I need to switch datasources on in the
codebehind. Everything works great with it until I switch. For example,
Delete, does not actually delete - it calls the on ondeleting sub, but
never actually deletes. If set the datasource in the markup and don't
switch, delete works fine.

<asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1"
runat="Server">
<br />
<br />
<asp:CheckBox ID="AllCheck" runat="server" AutoPostBack="True" />
<br />
<br />
<asp:GridView ID="RouteGridView" runat="server"
AllowPaging="True" AllowSorting="True"
AutoGenerateColumns="False" DataKeyNames="destId"
SkinID="GridView"
DataSourceId="s1" OnRowDeleting="myrowdeleting"
OnRowDeleted="myrowdeleted">
<Columns>
<asp:CommandField ButtonType="Image"
ShowDeleteButton="true" ShowSelectButton="True"
SelectImageUrl="../App_Themes/WinXP_Silver/Images/edit.gif"
DeleteImageUrl="../App_Themes/WinXP_Silver/Images/delete.gif"/>
<asp:BoundField DataField="PlanCode"
ItemStyle-Width="20" HeaderText="Plan Code" SortExpression="Plancode"
/>
<asp:BoundField DataField="DestCode"
ItemStyle-Width="20" HeaderText="Dest Code" SortExpression="routeCode"
/>
<asp:BoundField DataField="routeCode"
ItemStyle-Width="20" HeaderText="Route Code"
SortExpression="routeCode" />
</Columns>
</asp:GridView>

<asp:SqlDataSource ID="s1" runat="server"
ConnectionString="<%$ ConnectionStrings:RTEConnectionString %>"
SelectCommand="select * from gen_routeinfo_vw where
routecode = 'JCP'"
DeleteCommand="delete route where destid=@destId;delete
destination where destid=@destid"
<DeleteParameters>
<asp:parameter Name="destId" />
</DeleteParameters>

</asp:SqlDataSource>

<asp:SqlDataSource ID="s2" runat="server"
ConnectionString="<%$ ConnectionStrings:RTEConnectionString %>"
SelectCommand="select * from gen_routeinfo_vw where
routecode = 'JCP'"
DeleteCommand="delete route where destid=@destId;delete
destination where destid=@destid">
<DeleteParameters>
<asp:parameter Name="destId" />
</DeleteParameters>
</asp:SqlDataSource>
<br />
<br />
<asp:Label ID="StatusLabel" runat="server" Height="109px"
Text="Label" Width="484px"></asp:Label><br />
<br />
<br />

</asp:Content>




.... codebehind :


Partial Class Delete
Inherits System.Web.UI.Page
Protected Overrides Sub OnPreRender(ByVal e As EventArgs)
If IsPostBack Then
RouteGridView.DataSourceID = Nothing
RouteGridView.DataSource = Nothing
If AllCheck.Checked Then
RouteGridView.DataSource = s2
Else
RouteGridView.DataSource = s1
End If
RouteGridView.DataBind()
End If


End Sub
Protected Sub Allcheck_CheckedChanged(ByVal sender As Object, ByVal
e As System.EventArgs) Handles AllCheck.CheckedChanged
RouteGridView.DataBind()
End Sub

Protected Sub myRowDeleted(ByVal sender As Object, ByVal e As
GridViewDeletedEventArgs) Handles RouteGridView.RowDeleted
If e.Exception IsNot Nothing Then
StatusLabel.Text = e.Exception.ToString.Substring(0, 250) +
"<br>" + StatusLabel.Text
e.ExceptionHandled = True
Else
StatusLabel.Text = "Record Deleted"
RouteGridView.DataBind()
End If
End Sub

Protected Sub myRowDeleting(ByVal sender As Object, ByVal e As
GridViewDeleteEventArgs) Handles RouteGridView.RowDeleting
'RouteGridView.DataBind()
End Sub



End Class
 
J

jobs

Apparently switching datasources in codebehind messes up other things
like sorting and paging too...

The GridView 'RateGridView' fired event Sorting which wasn't handled.

Unless somebody knows how to do this I will need to duplicate the
grids and have one for every source.
 

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

Members online

Forum statistics

Threads
473,995
Messages
2,570,230
Members
46,819
Latest member
masterdaster

Latest Threads

Top