57Richardb
New Member
I have a grid view and i have bonded sql data source to that. on page i have drop down, as soon as selected value of drop down get changes, i change the select query of sql data source and again binding the data to grid view. before doing this if i update the grid view row its get updated but after doing previous process my update is not working. it don't show me any error. it don't take edited values.my grid view\[code\]<asp:GridView ID="gvTests" runat="server" AutoGenerateColumns="False" EmptyDataText="Testes are not assigned to this sample type." CellPadding="4" CssClass="border" DataKeyNames="TestId" DataSourceID="SqlDS" AlternatingRowStyle-BackColor="#E0ECF8" HeaderStyle-Height="20px" ForeColor="#333333" HeaderStyle-HorizontalAlign="Left" GridLines="None" Width="100%" OnRowCommand="gvTests_RowCommand" AllowPaging="True" AllowSorting="True" OnRowDataBound="gvTests_RowDataBound"> <AlternatingRowStyle /> <Columns> <asp:TemplateField HeaderText="TestId" InsertVisible="False" SortExpression="TestId" Visible="False"> <EditItemTemplate> <asp:Label ID="lblId" runat="server" Text='<%# Eval("Id") %>'></asp:Label> </EditItemTemplate> <ItemTemplate> <asp:Label ID="lblId" runat="server" Text='<%# Bind("Id") %>'></asp:Label> </ItemTemplate> </asp:TemplateField> <asp:TemplateField HeaderText="Test" SortExpression="TestName" HeaderStyle-HorizontalAlign="Center"> <EditItemTemplate> <asp:TextBox CssClass="smallinput_t200" Width="100px" Text='<%# Bind("Name") %>' ID="txtTestName" runat="server"></asp:TextBox> </EditItemTemplate> <ItemTemplate> <asp:Label ID="Label1" runat="server" Text='<%# Bind("Name") %>'></asp:Label> </ItemTemplate> </asp:TemplateField> <asp:TemplateField ShowHeader="False"> <EditItemTemplate> <asp:LinkButton ID="LinkButtonUpdate" runat="server" CausesValidation="True" CommandName="Update" Text="Update"></asp:LinkButton> <asp:LinkButton ID="LinkButtonCancel" runat="server" CausesValidation="False" CommandName="Cancel" Text="Cancel"></asp:LinkButton> </EditItemTemplate> <ItemTemplate> <asp:LinkButton ID="LinkButtonEdit" runat="server" CausesValidation="False" CommandName="Edit" Text="Edit"></asp:LinkButton> </ItemTemplate> </asp:TemplateField> <asp:TemplateField ShowHeader="False"> <ItemTemplate> <asp:LinkButton ID="LinkButtonDelete" runat="server" CausesValidation="False" CommandName="Del" Text="Delete" ></asp:LinkButton> </ItemTemplate> </asp:TemplateField> <asp:TemplateField ShowHeader="False"> <ItemTemplate> <asp:LinkButton ID="LinkButtonSelect" runat="server" CausesValidation="False" CommandName="Select" Text="Edit"></asp:LinkButton> </ItemTemplate> </asp:TemplateField> </Columns> <FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" /> <HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" /> <PagerStyle BackColor="#6B89AD" ForeColor="White" HorizontalAlign="Center" /> <RowStyle CssClass="mytr" /> <SelectedRowStyle BackColor="#6B89AD" ForeColor="White" /> <SortedAscendingCellStyle BackColor="#E9E7E2" /> <SortedAscendingHeaderStyle BackColor="#506C8C" /> <SortedDescendingCellStyle BackColor="#FFFDF8" /> <SortedDescendingHeaderStyle BackColor="#6F8DAE" /></asp:GridView>\[/code\]sql data source is like this\[code\]<asp:SqlDataSource ID="SqlDS" runat="server" ConnectionString="<%$ ConnectionStrings:ApplicationServices %>" SelectCommand="select * from tests" UpdateCommand="Update [Tests] set [Name]=@TestName where [Id]=@TestId" > <FilterParameters> </FilterParameters> <UpdateParameters> <asparameter Name="Name" DbType="String" /> <asparameter Name="Id" DbType="Int32" /> </UpdateParameters></asp:SqlDataSource>\[/code\]my drop down is as\[code\]<aspropDownList ID="ddlType" runat="server" DataSourceID="SqlDS" DataTextField="Name" DataValueField="Id" OnSelectedIndexChanged="ddlType_SelectedIndexChanged" AutoPostBack="True" OnDataBound="ddlSampleType_DataBound"> </aspropDownList> \[/code\]index change code is as\[code\]protected void ddlType_SelectedIndexChanged(object sender, EventArgs e) { SqlDS.SelectCommand = "select * from Tests t where t.Id in (1,2,3,4,5)"; SqlDS.Select(DataSourceSelectArguments.Empty); gvTests.EditIndex = -1; gvTests.DataBind(); }\[/code\]Row edit code is as\[code\]protected void gvTests_RowCommand(object sender, GridViewCommandEventArgs e) { if (e.CommandName == "Update") { GridViewRow row = (GridViewRow)((LinkButton)e.CommandSource).NamingContainer; SqlDSTests.UpdateParameters["Name"].DefaultValue = http://stackoverflow.com/questions/14065483/(row.FindControl("txtName") as TextBox).Text; SqlDSTests.UpdateParameters["Id"].DefaultValue = http://stackoverflow.com/questions/14065483/(row.FindControl("lblId") as Label).Text; SqlDSTests.Update(); SqlDS.Select(DataSourceSelectArguments.Empty); gvTests.EditIndex = -1; gvTests.DataBind(); } }\[/code\]