locked
refreshing gridview to get latest values after using rowcommand using datasourceid RRS feed

  • Question

  • User450460044 posted

    So 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 gridveiw?

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

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

    GridViewHolder.DataSource = MachineDataSet;
    GridViewHolder.DataBind();
    
    
    <asp:GridView ID="GridViewHolder" 
                              runat="server" 
                              AllowPaging="True" 
                              emptydatatext="No Machines stored in database."                          
                              AutoGenerateColumns="False"                          
                              PageSize="5" 
                              DataKeyNames="ID"
                              OnRowCommand="GridViewHolder_RowCommand"
    OnPageIndexChanging="GridViewHolder_PageChange"
                              DataSourceID="MachineDataSet">
                    <RowStyle BackColor="Transparent" 
                              HorizontalAlign="Center" />
                    <Columns>
                        <asp:TemplateField HeaderText="ID" 
                                           SortExpression="ID"                                       
                                           Visible="False">
                            <ItemTemplate>
                                <asp:Label ID="MachineIDLabel" 
                                           runat="server" 
                                           Text='<%# Bind("ID") %>'
                                           Visible="false">
                                </asp:Label>
                            </ItemTemplate>
                        </asp:TemplateField>
                        <asp:BoundField DataField="SiteName" 
                                        HeaderText="Site Name" 
                                        SortExpression="SiteName" />
                        <asp:BoundField DataField="Name" 
                                        HeaderText="Machine Name" 
                                        ReadOnly="true" 
                                        SortExpression="Name" />
                        <asp:BoundField DataField="MachineType" 
                                        HeaderText="Machine Type" 
                                        SortExpression="MachineType" />
                        <asp:BoundField DataField="MachineModel" 
                                        HeaderText="Machine Model" 
                                        SortExpression="MachineModel" />
                        <asp:ButtonField ButtonType="Button" 
                                         CommandName="AssignNewValue" 
                                         ItemStyle-Width="25px" 
                                         ItemStyle-Wrap="true" 
                                         Text="Assign New Values">
                            <ItemStyle Width="25px" Wrap="True" />
                        </asp:ButtonField>
                    </Columns>                
             </asp:GridView>
                 <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> 
    
     protected void GridViewHolder_RowCommand(object sender, GridViewCommandEventArgs e)
            {
                UpdateSucceed.Visible = false;
                UpdateFail.Visible = false;
                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);
                                }
                                databind();
                                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");
            }
           private void databind()
            {
                GridView gv = (GridView)Panel1.FindControl("GridViewHolder");
                gv.DataSourceID = "MachineDataSet";
                gv.DataBind();            
            }
    
    
    Any help or suggestions are greatly appreciated,
    
    
    thank you
    Friday, December 9, 2011 1:30 PM

Answers

  • User1228272849 posted

    simply rebind grid view again after updating and deleting.


    GridViewHolder
    .DataSource = MachineDataSet;
    GridViewHolder.DataBind();
    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, December 9, 2011 4:43 PM
  • User3866881 posted

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

    Hello:)

    It should upgrate your whole tables' data contents when re-binding to it unless your real tables haven't been changed。And if your MachineDataSet is a DataSet instead of a DataTable,you should use DataTable instance instead of DataSet to make a DataBinding。

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

    This is another problem—DataSource control and manually data binding can be ONLY one existing,you cannot bind both of them to the Data presentation control。

    Best reguards!

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Saturday, December 10, 2011 9:50 PM

All replies

  • User1228272849 posted

    simply rebind grid view again after updating and deleting.


    GridViewHolder
    .DataSource = MachineDataSet;
    GridViewHolder.DataBind();
    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, December 9, 2011 4:43 PM
  • User3866881 posted

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

    Hello:)

    It should upgrate your whole tables' data contents when re-binding to it unless your real tables haven't been changed。And if your MachineDataSet is a DataSet instead of a DataTable,you should use DataTable instance instead of DataSet to make a DataBinding。

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

    This is another problem—DataSource control and manually data binding can be ONLY one existing,you cannot bind both of them to the Data presentation control。

    Best reguards!

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Saturday, December 10, 2011 9:50 PM