简体   繁体   中英

Refreshing gridview to get latest values after update while using datasourceid

I have a gridview that runs a rowcommand to update values in the DB. everything works, except that after running the update the gridview does not refresh with the newest values of the table. I've seen many solutions to solve this problem, most of them stating simply, "just rebind the gridview"

I've tried just running the binding of the gridview, but this does nothing to refresh the gridview.

I've also tried reassigning the datasource to the objectdatasource used when first creating the table like so:

GridViewHolder.DataSource = MachineDataSet;
GridViewHolder.DataBind();

However, I ended up getting an error saying: that both datasource and the datasourceid is declared and to remove one definition.

I would appreciate it greatly if someone could tell me what i am doing wrong with my gridview?

Below you will find my gridview and codebehind for the rowcommand.

Gridview: note: I removed all the unnecessary context to make it easier to read, if you require more information i will throw up the entire code for the gridview.

<asp:GridView ID="GridViewHolder" 
                      runat="server" 
                      AllowPaging="True" 
                      AutoGenerateColumns="False"                                                                             
                      EnableViewState="False"
                      DataKeyNames="ID"                          
                      OnRowCommand="GridViewHolder_RowCommand" 
                      DataSourceID="MachineDataSet">
            <RowStyle BackColor="Transparent" 
                      HorizontalAlign="Center" />
            <Columns>                    
                <asp:ButtonField ButtonType="Button" Text="Assign New Values" 
                                 CommandName="AssignNewValue" ItemStyle-Wrap="true"
                                 ItemStyle-Width="25px">
                    <ItemStyle Width="25px" Wrap="True" />
                </asp:ButtonField>
            </Columns>
     </asp:GridView>

codebehind:

/// <summary>
    /// Handles the rowcommand event button
    /// </summary>
    /// <param name="sender">The source control event</param>
    /// <param name="e">The <see cref="System.Web.UI.WebControls.GridViewCommandEventArgs"/> instance containing the even data.</param>
    protected void GridViewHolder_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        logger.Debug("Entering Row Command");
        if (e.CommandName.CompareTo("AssignNewValue") == 0)
        {
            try
            {

                logger.Debug("Entering AssignNewValue Command");
                int index = Convert.ToInt32(e.CommandArgument);
                GridView gv = (GridView)Panel1.FindControl("GridViewHolder");
                GridViewRow row = gv.Rows[index];
                Label machineID = (Label)row.FindControl("MachineIDLabel");
                int machineid = Convert.ToInt32(machineID.Text);
                string machinetypeid = MachineTypeComboBox.SelectedValue;
                string machinemodelid = MachineModelComboBox.SelectedValue;
                try
                {
                    if (machinetypeid != "" || machinemodelid != "")
                    {
                        if (machinetypeid != "")
                        {
                            inputsService.UpdateMachineTypes(machineid, machinetypeid);
                        }
                        if (machinemodelid != "")
                        {
                            inputsService.UpdateMachineModels(machineid, machinemodelid);
                        }

                        UpdateSucceed.Visible = true;
                        logger.Debug("AssignNewValue - Database successfully updated!");
                    }
                    else
                    {
                        UpdateFail.Visible = true;
                        logger.Debug("AssignNewValue - Database had no data selected to be updated.");
                    }                        
                }
                catch (Exception ex)
                {
                    logger.ErrorFormat("AssignNewValue - Failed to update the table, ex = {0}", ex);
                }

            }
            catch (Exception ex)
            {
                logger.ErrorFormat("AssignNewValue Failed to complete, {0}", ex);
            }

            logger.Debug("Leaving AssignNewValue Command");
        }
        logger.Debug("Leaving Row Command");
    }

Datasource code:

<asp:ObjectDataSource ID="MachineDataSet"
                               runat="server" 
                               SelectMethod="GetMachineSiteDetails"                                                          
                               TypeName="Datamart.UI.Reporting.Web.FilteredReportInputsSvc.FilteredReportInputsService" 
                               EnableCaching="True">
             <SelectParameters>
                 <asp:Parameter DefaultValue="" Name="siteid" Type="String" />
             </SelectParameters>
         </asp:ObjectDataSource> 

any help or suggestions are greatly appreciated.

Thanks

I see you have this in your code:

GridView gv = (GridView)Panel1.FindControl("GridViewHolder")
GridViewHolder.DataSource = MachineDataSet;
GridViewHolder.DataBind();
UpdateSucceed.Visible = true;

When it should be:

GridView gv = (GridView)Panel1.FindControl("GridViewHolder")
gv.DataSource = MachineDataSet;
gv.DataBind();
UpdateSucceed.Visible = true;

However, I would have this in a function and call it in the page_load event, also on the row_command . But you should not have it on the definition of the gridview:

 DataSourceID="MachineDataSet">

The DataSourceID should be used only when you have a DataSource control in the page.

Edit: Remember, if you Bind your gridview from the code behind, you will have to do a couple of tweaks in the code to be able to do Paging/Sorting in the Gridview. If you need help with that you just need to do a simple search and you will find it for sure. Good luck!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM