How to refresh GridView after data insert?
14,373
In your method btnInsertOrderSignal_Click you are adding a new item and databinding again. This is correct but you are not updating your datasource before databinding(). This means that it will bind to the original datasource.
It seems that what populates your CustomControl needs to be called again. GetOrderSignals()?
Author by
Mark Allison
Updated on June 05, 2022Comments
-
Mark Allison almost 2 years
I have a GridView and when some data has been inserted I have called the DataBind() method on the GridView and the ObjectDataSource the GridView is bound to, but the GridView does not display the new data. How do I get it to refresh?
Here's my ASP.NET code:
<asp:GridView ID="grdOrderSignals" runat="server" DataSourceID="srcOrderSignals" DataKeyNames="signal" EmptyDataText="There are no signals for this order" AutoGenerateColumns="False"> <Columns> <asp:BoundField DataField="signal" HeaderText="Signal Id" ReadOnly="True" /> <asp:BoundField DataField="description" HeaderText="Description" ReadOnly="True" /> <CustomControls:DeleteButtonField ConfirmText="Delete this order signal?" Text="Del" /> </Columns> </asp:GridView> <CustomControls:CustomObjectDataSource id="srcOrderSignals" TypeName="DatabaseComponent.DBUtil" SelectMethod="GetOrderSignals" DeleteMethod="DeleteOrderSignal" EnableCaching="false" runat="server"> <SelectParameters> <asp:ControlParameter Name="orderId" ControlID="grdOrders" PropertyName="SelectedValue" /> </SelectParameters> <DeleteParameters> <asp:ControlParameter Name="orderId" ControlID="grdOrders" PropertyName="SelectedValue" /> <asp:ControlParameter Name="signal" ControlID="grdOrderSignals" PropertyName="SelectedValue" /> </DeleteParameters> </CustomControls:CustomObjectDataSource> <asp:Button ID="btnAddOrderSignal" runat="server" Text="Add Order Signal" OnClick="btnAddOrderSignal_Click" /> <asp:DropDownList ID="ddlSignals" runat="server" Visible="false" /> <asp:Button ID="btnInsertOrderSignal" runat="server" Text="Add" Visible="false" OnClick="btnInsertOrderSignal_Click" />
Here's my code-behind to handle the click events:
protected void btnAddOrderSignal_Click(object sender, EventArgs e) { FillSignals(); btnAddOrderSignal.Visible = false; ddlSignals.Visible = true; btnInsertOrderSignal.Visible = true; } protected void btnInsertOrderSignal_Click(object sender, EventArgs e) { InsertOrderSignal(); btnAddOrderSignal.Visible = true; ddlSignals.Visible = false; btnInsertOrderSignal.Visible = false; srcOrderSignals.DataBind(); grdOrderSignals.DataBind(); } protected void InsertOrderSignal() { DBUtil DB = new DBUtil(); int orderId = (int)grdOrders.SelectedValue; string signalId = ddlSignals.SelectedValue; DB.InsertOrderSignal(orderId, signalId); } protected void FillSignals() { DBUtil DB = new DBUtil(); DataTable od = DB.GetOrderDetails((int)grdOrders.SelectedValue); int strategyId = (int)od.Rows[0]["strategyId"]; ddlSignals.DataSource = DB.GetSignals(strategyId); ddlSignals.DataTextField = "signalId"; ddlSignals.DataValueField = "signalId"; ddlSignals.DataBind(); }
Do I need to call DataBind() on both objects? What have I done wrong here?
Thanks!
-
Mark Allison over 13 yearsThanks, but I thought I was doing that with srcOrderSignals.DataBind()? Did you have some other method in mind to update the data source?
-
Mark Allison over 13 yearsWorked it out - I called the Select() method on the DataSource and it works now. Thanks for looking.
-
Kamal over 13 yearsGreat. The select method is the method that updates the datasource by 'doing the select again'. As I understand it a simple databind will not call the SelectMethod again.