开发者

Set DetailsView as selected row of GridView

I am creating a GridView/DetailsView page. I have a grid that displays a bunch of rows, when a row is selected it uses a DetailsView to allow for Insert/Update.

My question is what is the best way to link these? I do not want to reach out to the web service again, all the data i need is in the selected grid view row. I basically have 2 separate data sources that share the same "DataObjectTypeName", the first data source retrieves the data, and the other to do the CRUD.

What is the best way to transfer the Selected Grid View row to the Details View? Am I going to have to manualy handle the Insert/Update events and call the data source myself?

Is there no way to link these two so they use the same data source ?

  <asp:GridView ID="gvDetails" runat="server" DataKeyNames="ID, Code"
                DataSourceID="odsSearchData" >
   <Columns>
        <asp:BoundField DataField="RowA" HeaderText="A" SortExpression="RowA" />
        <asp:BoundField DataField="RowB" HeaderText="B" SortExpression="RowB" />
        <asp:BoundField DataField="RowC" HeaderText开发者_如何转开发="C" SortExpression="RowC" />



    ....Code...

 <asp:DetailsView ID="dvDetails" runat="server" DataKeyNames="ID, Code"
                DataSourceID="odsCRUD" GridLines="None" DefaultMode="Edit" AutoGenerateRows="false"
                Visible="false" Width="100%">
         <Fields>
            <asp:BoundField DataField="RowA" HeaderText="A" SortExpression="RowA" />
           <asp:BoundField DataField="RowB" HeaderText="B" SortExpression="RowB" />
           <asp:BoundField DataField="RowC" HeaderText="C" SortExpression="RowC" />

...


The standard way to do it would be to have the selected item of the griview be a control parameter to the objectdatasource you have wired up to the detailsview. I would probably not worry too much about the overhead of retreiving data that you already have unless you are catering to users with such slow connections that you want to avoid roundtrips to the webserver at all costs.

If you really want to avoid that thenyou could pull the data out of the gridview using javascript/jquery and then do your inserts/updates via ajax calls. It would require lots more coding though.


This is a really old thread, but in case anyone came here looking for an answer like I did, a simple solution is to add this function to your code:

(Note that this only works if the rows in your GridView match the entries in your DetailsView.)

protected void GridView1_OnSelectedIndexChanged(object sender, EventArgs e)
{
     DetailsView1.SetPageIndex(GridView1.SelectedIndex);
}

And modify the GridView and DetailsView to include these settings:

<asp:GridView ... OnSelectedIndexChanged="GridView1_OnSelectedIndexChanged" ... >
<asp:DetailsView ... AllowPaging="True" ... >

This will make the selected page in the DetailsView match the selected index in the GridView.

You can hide the paging options in the DetailsView properties if you dont want users to navigate using paging in the DetailsView.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜