2011-12-08 54 views
0

我有一個gridview運行rowcommand來更新數據庫中的值。一切正常,但運行更新後,gridview不會刷新表的最新值。我已經看到了很多解決方案來解決這個問題,他們大多隻是簡單地說,「只是重新綁定gridview」使用datasourceid刷新gridview獲取更新後的最新值

我試過運行gridview的綁定,但是這並沒有什麼能夠刷新gridview。

我還試圖重新分配數據源到第一創造一個像這樣的表時使用的ObjectDataSource控件:

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

然而,我最終得到一個錯誤說:這兩個數據源和DataSourceID的聲明和刪除一個定義。

如果有人能告訴我我的gridview出錯了,我將不勝感激。

下面你會發現我的gridview和rowcommand的代碼隱藏。

Gridview:注意:我刪除了所有不必要的上下文以便於閱讀,如果您需要更多信息,我會拋出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> 

代碼隱藏:

/// <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"); 
    } 

數據源代碼:

<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> 

任何幫助或建議,不勝感激。

感謝

+1

「但是,我最終得到一個錯誤:數據源和datasourceid都被聲明並刪除了一個定義。」從我記得你必須使用其中一個或另一個。在你的代碼後面,你可能能夠逃脫只是做一個.Databind()調用 – user1231231412

+0

@jonc我試着做一個.databind()調用以及不拋出錯誤,但它不會刷新後的gridview更新。 – James213

+0

你可以發佈你的DataSource定義代碼嗎?對於MachineDataSet – user1231231412

回答

2

我看你有沒有這在你的代碼:

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

當它應該是:

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

不過,我想有這樣的功能,並調用它page_load事件,也在row_command上。但你不應該有它在GridView上的定義:

DataSourceID="MachineDataSet"> 

只有當你在網頁DataSource控件中的DataSourceID應該被使用。

編輯:請記住,如果你從後面的代碼綁定你的gridview,你將不得不在代碼中做一些調整,以便能夠在Gridview中進行分頁/排序。如果你需要幫助,你只需要做一個簡單的搜索,你會發現它肯定。 祝你好運!

+0

所以我應該只使用datasourceid,如果我有這樣的一個頁面: – James213

+0

你應該只使用DataSourceID來指定控制的ID「代表數據源從哪個數據綁定控件檢索其數據。「 http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.databoundcontrol.datasourceid.aspx –

+0

所以是啊@ James213,你的數據源控件(ObjectDataSource MachineDataSet)看起來不錯,但你應該使用DataSourceID或DataSource不是兩者。 –