Hello
I got 2 gridviews on a page, each with its own ObjectDataSource. The first gridview fills itself just normally, no problem there. The second gridview needs to fill itself with other data, where the primary key is the primary key of the selected row of the first gridview.
schematically:
gridview1
row: IDfield, x, y, z
row: IDfield, x, y, z (-> this row is selected)
row: IDfield, x, y, zgridview2
row:IDfield_of_selected_row_in_gridview1, a, b, c
I tried to do this by configuring the ObjectDataSource behind gridview2 with a parameter pointing to gridview1.selectedValue. (by using the configuration 'wizard')
This doesn't work, probably because the ObjectDataSource only gets filled once, when the page loads, but there is no row selected then so gridview2 remains empty. All the time.
When I select a row from my first gridview, nothing happens to gridview2.I know that I can do this manually, the databinding (via the codebehind, GridView1_SelectedIndexChanged) and this works, it shows me the data. But then I get the problem that I can't edit it.
So, how do I make it so that whenever I select a row from my first gridview, the second ObjectDataSource refreshes itself and gets the data from my database where the primary key is the primary key of my selected gridview1 row?
I don't know if it helps, but i'll add my sourcecode.
Thanks
Jee
1 <%@ Page Language="C#" MasterPageFile="~/admin/admin.master" AutoEventWireup="true" CodeFile="VoegBestellingToe.aspx.cs" Inherits="admin_VoegDataToe_VoegBestellingToe" Title="Untitled Page" %> 2 <asp:Content ID="Content1" ContentPlaceHolderID="cphMain" Runat="Server"> 3 <table style="width: 100%"> 4 <tr> 5 <td style="text-align: left; vertical-align: top; width: 500px;"> 6 <h2>Bestellingen</h2> 7 <asp:GridView ID="GridView1" runat="server" AllowPaging="True" AllowSorting="True" 8 AutoGenerateColumns="False" CellPadding="4" DataKeyNames="BestellingID" DataSourceID="ObjectDataSourceBestellingen" 9 ForeColor="#333333" GridLines="None" 10 onselectedindexchanged="GridView1_SelectedIndexChanged"> 11 <FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" /> 12 <RowStyle BackColor="#F7F6F3" ForeColor="#333333" /> 13 <Columns> 14 <asp:CommandField ShowDeleteButton="True" ShowEditButton="True" 15 ValidationGroup="editBestelling" ShowSelectButton="True" /> 16 <asp:BoundField DataField="BestellingID" HeaderText="BestellingID" InsertVisible="False" 17 ReadOnly="True" SortExpression="BestellingID" /> 18 <asp:TemplateField HeaderText="KlantID" SortExpression="KlantID"> 19 <EditItemTemplate> 20 <asp:DropDownList ID="DropDownList2" runat="server" DataSourceID="ObjectDataSourceKlantNaam" 21 DataTextField="Naam" DataValueField="KlantID" SelectedValue='<%# Bind("KlantID") %>'> 22 </asp:DropDownList> 23 </EditItemTemplate> 24 <ItemTemplate> 25 <asp:Label ID="Label1" runat="server" Text='<%# Bind("KlantID") %>'></asp:Label> 26 </ItemTemplate> 27 </asp:TemplateField> 28 <asp:TemplateField HeaderText="Besteldatum" SortExpression="Besteldatum"> 29 <EditItemTemplate> 30 <asp:TextBox ID="TextBox2" runat="server" 31 Text='<%# Bind("Besteldatum", "{0:d}") %>' ValidationGroup="editBestelling"></asp:TextBox> 32 <asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" 33 ControlToValidate="TextBox2" CssClass="error" 34 ErrorMessage="Vul een datum in aub." ValidationGroup="editBestelling">*</asp:RequiredFieldValidator> 35 <asp:RegularExpressionValidator ID="RegularExpressionValidator1" runat="server" 36 ControlToValidate="TextBox2" CssClass="error" 37 ErrorMessage="Geef een correcte datum in aub." 38 ValidationExpression="\d{1,2}\/\d{2}\/\d{4}" ValidationGroup="editBestelling">*</asp:RegularExpressionValidator> 39 </EditItemTemplate> 40 <ItemTemplate> 41 <asp:Label ID="Label2" runat="server" Text='<%# Bind("Besteldatum", "{0:d}") %>'></asp:Label> 42 </ItemTemplate> 43 </asp:TemplateField> 44 </Columns> 45 <PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" /> 46 <SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" /> 47 <HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" /> 48 <EditRowStyle BackColor="#999999" /> 49 <AlternatingRowStyle BackColor="White" ForeColor="#284775" /> 50 </asp:GridView> 51 <br /> 52 <asp:ValidationSummary ID="ValidationSummary1" runat="server" CssClass="error" 53 ValidationGroup="editBestelling" /> 54 <br /> 55 <asp:DetailsView ID="DetailsView1" runat="server" AutoGenerateRows="False" CellPadding="4" 56 DataKeyNames="BestellingID" DataSourceID="ObjectDataSourceBestellingen" DefaultMode="Insert" 57 ForeColor="#333333" GridLines="None" Height="50px" Width="125px" > 58 <FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" /> 59 <CommandRowStyle BackColor="#E2DED6" Font-Bold="True" /> 60 <RowStyle BackColor="#F7F6F3" ForeColor="#333333" /> 61 <FieldHeaderStyle BackColor="#E9ECF1" Font-Bold="True" /> 62 <PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" /> 63 <Fields> 64 <asp:BoundField DataField="BestellingID" HeaderText="BestellingID" InsertVisible="False" 65 ReadOnly="True" SortExpression="BestellingID" /> 66 <asp:TemplateField HeaderText="KlantID" SortExpression="KlantID"> 67 <EditItemTemplate> 68 <asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("KlantID") %>'></asp:TextBox> 69 </EditItemTemplate> 70 <InsertItemTemplate> 71 <asp:DropDownList ID="DropDownList1" runat="server" DataSourceID="ObjectDataSourceKlantNaam" 72 DataTextField="Naam" DataValueField="KlantID" SelectedValue='<%# Bind("KlantID") %>'> 73 </asp:DropDownList> 74 </InsertItemTemplate> 75 <ItemTemplate> 76 <asp:Label ID="Label1" runat="server" Text='<%# Bind("KlantID") %>'></asp:Label> 77 </ItemTemplate> 78 </asp:TemplateField> 79 <asp:TemplateField HeaderText="Besteldatum" SortExpression="Besteldatum"> 80 <EditItemTemplate> 81 <asp:TextBox ID="TextBox2" runat="server" Text='<%# Bind("Besteldatum") %>'></asp:TextBox> 82 </EditItemTemplate> 83 <InsertItemTemplate> 84 <asp:Calendar ID="Calendar1" runat="server" BackColor="White" BorderColor="White" 85 BorderWidth="1px" Font-Names="Verdana" Font-Size="9pt" ForeColor="Black" Height="190px" 86 NextPrevFormat="FullMonth" SelectedDate='<%# Bind("Besteldatum") %>' Width="350px"> 87 <SelectedDayStyle BackColor="#333399" ForeColor="White" /> 88 <TodayDayStyle BackColor="#CCCCCC" /> 89 <OtherMonthDayStyle ForeColor="#999999" /> 90 <NextPrevStyle Font-Bold="True" Font-Size="8pt" ForeColor="#333333" VerticalAlign="Bottom" /> 91 <DayHeaderStyle Font-Bold="True" Font-Size="8pt" /> 92 <TitleStyle BackColor="White" BorderColor="Black" BorderWidth="4px" Font-Bold="True" 93 Font-Size="12pt" ForeColor="#333399" /> 94 </asp:Calendar> 95 </InsertItemTemplate> 96 <ItemTemplate> 97 <asp:Label ID="Label2" runat="server" Text='<%# Bind("Besteldatum") %>'></asp:Label> 98 </ItemTemplate> 99 </asp:TemplateField> 100 <asp:CommandField ShowInsertButton="True" ValidationGroup="AddBestelling" /> 101 </Fields> 102 <HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" /> 103 <EditRowStyle BackColor="#999999" /> 104 <AlternatingRowStyle BackColor="White" ForeColor="#284775" /> 105 </asp:DetailsView> 106 <br /> 107 <asp:ValidationSummary ID="ValidationSummary2" runat="server" ValidationGroup="AddBestelling" /> 108 <br /> 109 <asp:ObjectDataSource ID="ObjectDataSourceBestellingen" runat="server" DeleteMethod="Delete" 110 InsertMethod="Insert" OldValuesParameterFormatString="original_{0}" SelectMethod="GetBestellingen" 111 TypeName="webshopTableAdapters.BestellingTableAdapter" UpdateMethod="Update"> 112 <DeleteParameters> 113 <asp:Parameter Name="Original_BestellingID" Type="Int32" /> 114 </DeleteParameters> 115 <UpdateParameters> 116 <asp:Parameter Name="KlantID" Type="Int32" /> 117 <asp:Parameter Name="Besteldatum" Type="DateTime" /> 118 <asp:Parameter Name="Original_BestellingID" Type="Int32" /> 119 </UpdateParameters> 120 <InsertParameters> 121 <asp:Parameter Name="KlantID" Type="Int32" /> 122 <asp:Parameter Name="Besteldatum" Type="DateTime" /> 123 </InsertParameters> 124 </asp:ObjectDataSource> 125 <asp:ObjectDataSource ID="ObjectDataSourceKlantNaam" runat="server" DeleteMethod="Delete" 126 InsertMethod="Insert" OldValuesParameterFormatString="original_{0}" SelectMethod="GetKlantnaam" 127 TypeName="webshopTableAdapters.KlantTableAdapter" UpdateMethod="Update"> 128 <DeleteParameters> 129 <asp:Parameter Name="Original_KlantID" Type="Int32" /> 130 <asp:Parameter Name="Original_Voornaam" Type="String" /> 131 <asp:Parameter Name="Original_Achternaam" Type="String" /> 132 <asp:Parameter Name="Original_Geslacht" Type="String" /> 133 <asp:Parameter Name="Original_Straat" Type="String" /> 134 <asp:Parameter Name="Original_Nr" Type="Int16" /> 135 <asp:Parameter Name="Original_Bus" Type="String" /> 136 <asp:Parameter Name="Original_Postcode" Type="String" /> 137 <asp:Parameter Name="Original_Plaats" Type="String" /> 138 <asp:Parameter Name="Original_Telefoon_privé" Type="String" /> 139 <asp:Parameter Name="Original_Telefoon_mobiel" Type="String" /> 140 <asp:Parameter Name="Original_Telefoon_werk" Type="String" /> 141 <asp:Parameter Name="Original_Email" Type="String" /> 142 </DeleteParameters> 143 <UpdateParameters> 144 <asp:Parameter Name="Voornaam" Type="String" /> 145 <asp:Parameter Name="Achternaam" Type="String" /> 146 <asp:Parameter Name="Geslacht" Type="String" /> 147 <asp:Parameter Name="Straat" Type="String" /> 148 <asp:Parameter Name="Nr" Type="Int16" /> 149 <asp:Parameter Name="Bus" Type="String" /> 150 <asp:Parameter Name="Postcode" Type="String" /> 151 <asp:Parameter Name="Plaats" Type="String" /> 152 <asp:Parameter Name="Telefoon_privé" Type="String" /> 153 <asp:Parameter Name="Telefoon_mobiel" Type="String" /> 154 <asp:Parameter Name="Telefoon_werk" Type="String" /> 155 <asp:Parameter Name="Email" Type="String" /> 156 <asp:Parameter Name="Original_KlantID" Type="Int32" /> 157 <asp:Parameter Name="Original_Voornaam" Type="String" /> 158 <asp:Parameter Name="Original_Achternaam" Type="String" /> 159 <asp:Parameter Name="Original_Geslacht" Type="String" /> 160 <asp:Parameter Name="Original_Straat" Type="String" /> 161 <asp:Parameter Name="Original_Nr" Type="Int16" /> 162 <asp:Parameter Name="Original_Bus" Type="String" /> 163 <asp:Parameter Name="Original_Postcode" Type="String" /> 164 <asp:Parameter Name="Original_Plaats" Type="String" /> 165 <asp:Parameter Name="Original_Telefoon_privé" Type="String" /> 166 <asp:Parameter Name="Original_Telefoon_mobiel" Type="String" /> 167 <asp:Parameter Name="Original_Telefoon_werk" Type="String" /> 168 <asp:Parameter Name="Original_Email" Type="String" /> 169 </UpdateParameters> 170 <InsertParameters> 171 <asp:Parameter Name="Voornaam" Type="String" /> 172 <asp:Parameter Name="Achternaam" Type="String" /> 173 <asp:Parameter Name="Geslacht" Type="String" /> 174 <asp:Parameter Name="Straat" Type="String" /> 175 <asp:Parameter Name="Nr" Type="Int16" /> 176 <asp:Parameter Name="Bus" Type="String" /> 177 <asp:Parameter Name="Postcode" Type="String" /> 178 <asp:Parameter Name="Plaats" Type="String" /> 179 <asp:Parameter Name="Telefoon_privé" Type="String" /> 180 <asp:Parameter Name="Telefoon_mobiel" Type="String" /> 181 <asp:Parameter Name="Telefoon_werk" Type="String" /> 182 <asp:Parameter Name="Email" Type="String" /> 183 </InsertParameters> 184 </asp:ObjectDataSource> 185 </td> 186 <td style="text-align: left; vertical-align: top; padding-left: 50px;"> 187 <h2>Bestelinhoud</h2> 188 <asp:GridView ID="GridView2" runat="server" AllowPaging="True" 189 AllowSorting="True" AutoGenerateColumns="False" CellPadding="4" 190 DataKeyNames="BestellingID,ArtikelID" 191 DataSourceID="ObjectDataSourceBestellingInhoud" ForeColor="#333333" 192 GridLines="None" onrowediting="GridView2_RowEditing" Visible="False"> 193 <FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" /> 194 <RowStyle BackColor="#F7F6F3" ForeColor="#333333" /> 195 <Columns> 196 <asp:CommandField ShowDeleteButton="True" ShowEditButton="True" /> 197 <asp:BoundField DataField="BestellingID" HeaderText="BestellingID" 198 ReadOnly="True" SortExpression="BestellingID" /> 199 <asp:BoundField DataField="ArtikelID" HeaderText="ArtikelID" ReadOnly="True" 200 SortExpression="ArtikelID" /> 201 <asp:BoundField DataField="Aantal" HeaderText="Aantal" 202 SortExpression="Aantal" /> 203 </Columns> 204 <PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" /> 205 <SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" /> 206 <HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" /> 207 <EditRowStyle BackColor="#999999" /> 208 <AlternatingRowStyle BackColor="White" ForeColor="#284775" /> 209 </asp:GridView> 210 <asp:ObjectDataSource ID="ObjectDataSourceBestellingInhoud" runat="server" 211 DeleteMethod="Delete" InsertMethod="Insert" 212 OldValuesParameterFormatString="original_{0}" 213 SelectMethod="GetBestellingInhoudByBestellingID" 214 TypeName="webshopTableAdapters.BestellingInhoudTableAdapter" 215 UpdateMethod="Update"> 216 <DeleteParameters> 217 <asp:Parameter Name="Original_BestellingID" Type="Int32" /> 218 <asp:Parameter Name="Original_ArtikelID" Type="Int32" /> 219 <asp:Parameter Name="Original_Aantal" Type="Int32" /> 220 </DeleteParameters> 221 <UpdateParameters> 222 <asp:Parameter Name="Aantal" Type="Int32" /> 223 <asp:Parameter Name="Original_BestellingID" Type="Int32" /> 224 <asp:Parameter Name="Original_ArtikelID" Type="Int32" /> 225 <asp:Parameter Name="Original_Aantal" Type="Int32" /> 226 </UpdateParameters> 227 <SelectParameters> 228 <asp:ControlParameter ControlID="GridView1" Name="BestellingID" 229 PropertyName="SelectedValue" Type="Int32" /> 230 </SelectParameters> 231 <InsertParameters> 232 <asp:Parameter Name="BestellingID" Type="Int32" /> 233 <asp:Parameter Name="ArtikelID" Type="Int32" /> 234 <asp:Parameter Name="Aantal" Type="Int32" /> 235 </InsertParameters> 236 </asp:ObjectDataSource> 237 </td> 238 </tr> 239 </table> 240 241 </asp:Content>
![]() |
0 |
![]() |
You should be doing this via the code-behind. What happens when you try to edit?
Thanks, Ed
Microsoft MVP - ASP/ASP.NET
![]() |
0 |
![]() |
I don't know how to do this, I don't have any code in my "GridView2_RowEditing" method yet...What I have so far is that I can show data in my GridView2 using following code:1 protected void GridView1_SelectedIndexChanged(object sender, EventArgs e) 2 { 3 GridView gridview = (GridView) sender; 4 int ID = (int) gridview.SelectedValue; 5 6 webshopTableAdapters.OrderContentTableAdapter OrderContentTableAdapter = new webshopTableAdapters.OrderContentTableAdapter(); 7 DataTable dt = OrderContentTableAdapter.GetOrderContentByID(ID); 8 9 if (dt.Rows.Count != 0){ 10 GridView2.DataBind(); 11 GridView2.Visible = true; 12 } 13 else 14 GridView2.Visible = false; 15 }Also, I still have my ObjectDataSource like in my previous post. With the code behind my "GridView1_SelectedIndexChanged" and my ObjectDataSource, GridView2 gets populated and when I select 'edit' it gets editable.
When I click update however, it doesn't save the data. It just keeps the value it had before I started updating.ThanksJee
![]() |
0 |
![]() |
All you should have to do within your SelectedIndexChanged handler is to call the DataBind method of GridView2. The ObjectDataSource will handle the rest. If you need to be able to hide the GridView, then do this within the ObjectDataSource.Selected event handler.
Thanks, Ed
Microsoft MVP - ASP/ASP.NET
![]() |
0 |
![]() |
When I remove all my code out of the GridView1_SelectedIndexChanged method and just leave in "Gridview2.DataBind", nothing happens when I select a row from my gridview1.
The ObjectDataSource bound to my gridview2 is still set up the same as in my first post.Any idea's why this doesn't work?
![]() |
0 |
![]() |
My apologies, I was a bit to hasty.
I forgot to turn on the gridview2 's visibility, so obviously I didn't see any results.
Like you said, only the databind() method is needed for gridview2 to show what I want.
The fields also get editable when I click "edit" on my gridview2, but when I say "update" it doesn't update. The edited field just reverts to its old value.
I tried putting the databind() method in the GridView2_RowEditing method, but that doens't cut it.Any suggestions?
Thanks
Jee
![]() |
0 |
![]() |
Hi Grizly ,
I don't think you need to write any code when you connect GridView with objectdatasource.
You said you forget to turn on the gridview2's visibility, so try to remove the databind() method.
If you put one break point in Update( int Aantal,int Original_BestellingID, int Original_ArtikelID, int Original_Aantal ) method , does it retrieve the correct value ? I think it should work , these parameters begin with Original will get original value and the Aantal parameter will retrieve new value !
Samu Zhang
Microsoft Online Community Support
Please remember to click “Mark as Answer” on the post that helps you, and to click “Unmark as Answer” if a marked post does not actually answer your question.
![]() |
0 |
![]() |
Hi Samu Zhang
You're right about the fact that I can just leave out the databind() method in my gridview1_selectedIndexChanged() method. I thought this was necessary because when I load my page, there is no row selected and so the objectdatasource bound to my detailsview2 doesn't get any data making it necessary to databind everytime a row got selected in my gridview1.
Which Update method do you mean? The "Updated" / "Updating" method of my ObjectDataSource2 or the "RowUpdating" / "RowUpdated" method of my GridView2?
Right now I thought I needed to write "databind();" in my gridview2_rowEDITING method, but this isn't the case then I guess.
Thanks
Jee
![]() |
0 |
![]() |
I fixed it
Don't ask me how... But it just checked everything in my DataSet and ObjectDataSource and it just works now... Really bizarre... But nevertheless, great
Thanks
Jee
![]() |
0 |
![]() |