2011-11-25 49 views
1

我在VS2005上使用C#ASP.NET。ASP.NET在GridView中配置更新按鈕

我有一個GridView表,但它不具備選擇啓用編輯當我右擊智能標籤

AutoGenerateEditButton="True"

編輯按鈕已經成功地出現在我的GridView這樣的:

因此我手動用下面的代碼添加編輯按鈕 enter image description here

當我點擊編輯按鈕,頁面刷新並且該行現在可以編輯: enter image description here

然而,當我按下了更新按鈕,我被帶到了錯誤:

Updating is not supported by data source 'SqlDataSource1' unless UpdateCommand is specified.

enter image description here http://i.stack.imgur.com/W97K0.png

我不知道我如何能輸入或配置UpdateCommand因爲我沒有看到Update按鈕的任何後臺代碼。

需要經驗豐富的幫助。先謝謝你。


編輯:在SqlDataSource1添加的插入查詢,但是我仍然遇到了同樣的錯誤,當我按下按鈕Update

+0

正如你問你的評論中的例子,請看看.. – AnandMohanAwasthi

回答

3

您需要重新配置SqlDataSource1控制,雖然你可以用SELECT一起添加支持INSERTDELETEUPDATE

看看this教程。

+0

謝謝,我將配置現在。我的INSERT語句將是?我不知道如何將INSERT語句與特定的可編輯行綁定在一起。謝謝 – gymcode

+0

@RUiHAO - 看看已編輯的文章(已添加鏈接)。 – adatapost

0

例如,嘗試了這一點...

  1. 首先創建處理更新記錄的方法。

    private void UpdateOrAddNewRecord(string parametervalue1, string parametervalue2) 
        { 
         using (openconn()) 
         { 
          string sqlStatement = string.Empty; 
          sqlStatement = "Update TableName set Name1 [email protected] where [email protected]"; 
    
    
          try 
          { 
          // SqlCommand cmd = new SqlCommand("storedprocedureName", con); 
           //cmd.CommandType = CommandType.StoredProcedure; 
    
           SqlCommand cmd = new SqlCommand(sqlStatement, con); 
           cmd.Parameters.AddWithValue("Name2", parametervalue2); 
           cmd.Parameters.AddWithValue("@Name1",parametervalue1); 
    
           cmd.CommandType = CommandType.Text; 
           cmd.ExecuteNonQuery(); 
    
          } 
    
    
          catch (System.Data.SqlClient.SqlException ex) 
          { 
    
           string msg = "Insert/Update Error:"; 
    
           msg += ex.Message; 
    
           throw new Exception(msg); 
    
    
          } 
    
          finally 
          { 
    
           closeconn(); 
    
          } 
         } 
    
  2. 現在創建行更新方法..

    protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e) 
        { 
         string ParameterValue1 = ((TextBox)GridView1.Rows[e.RowIndex].Cells[0].Controls[0]).Text; 
    
         string ParameterValue2 = ((TextBox)GridView1.Rows[e.RowIndex].Cells[1].Controls[0]).Text; //Name 
    
         UpdateOrAddNewRecord(ParameterValue1, ParameterValue2); // call update method 
    
         GridView1.EditIndex = -1; 
         BindGridView(); 
         Label2.Visible = true; 
         Label2.Text = "Row Updated"; 
        } 
    
  3. 創建行取消事件..

    保護無效GridView1_RowCancelingEdit(對象發件人,GridViewCancelEditEventArgs E) { GridView1.EditIndex = -1; // swicth回到默認模式 BindGridView(); }

  4. 創建行編輯...

    protected void GridView1_RowEditing(object sender,GridViewEditEventArgs e) GridView1.EditIndex = e.NewEditIndex; BindGridView(); }

還有很多其他的出路,以不同的方式做同樣的活動。這是最基本的方法。不管怎麼說,如果你覺得有用,請把它標記爲其他人你的答案讓我知道...

0
In your code I think you have not handled the event for "Update". 


Have a look at the below example hope it might help you, 
check for the "UpdateCommand". 
Also write a Update event in C# to update. 



<asp:DetailsView ID="ManageProducts" runat="server" AllowPaging="True" 
    AutoGenerateRows="False" DataKeyNames="ProductID" 
    DataSourceID="ManageProductsDataSource" EnableViewState="False"> 
    <Fields> 
     <asp:BoundField DataField="ProductID" HeaderText="ProductID" 
      InsertVisible="False" ReadOnly="True" SortExpression="ProductID" /> 
     <asp:BoundField DataField="ProductName" HeaderText="ProductName" 
      SortExpression="ProductName" /> 
     <asp:BoundField DataField="UnitPrice" HeaderText="UnitPrice" 
      SortExpression="UnitPrice" /> 
     <asp:CheckBoxField DataField="Discontinued" HeaderText="Discontinued" 
      SortExpression="Discontinued" /> 
    </Fields> 
</asp:DetailsView> 
<asp:SqlDataSource ID="ManageProductsDataSource" runat="server" 
    ConnectionString="<%$ ConnectionStrings:NORTHWNDConnectionString %>" 
    DeleteCommand= 
     "DELETE FROM [Products] WHERE [ProductID] = @ProductID" 
    InsertCommand= 
     "INSERT INTO [Products] ([ProductName], [UnitPrice], [Discontinued]) 
     VALUES (@ProductName, @UnitPrice, @Discontinued)" 
    SelectCommand= 
     "SELECT [ProductID], [ProductName], [UnitPrice], [Discontinued] 
     FROM [Products]" 
    UpdateCommand= 
     "UPDATE [Products] SET [ProductName] = @ProductName, 
     [UnitPrice] = @UnitPrice, [Discontinued] = @Discontinued 
     WHERE [ProductID] = @ProductID"> 
    <DeleteParameters> 
     <asp:Parameter Name="ProductID" Type="Int32" /> 
    </DeleteParameters> 
    <UpdateParameters> 
     <asp:Parameter Name="ProductName" Type="String" /> 
     <asp:Parameter Name="UnitPrice" Type="Decimal" /> 
     <asp:Parameter Name="Discontinued" Type="Boolean" /> 
     <asp:Parameter Name="ProductID" Type="Int32" /> 
    </UpdateParameters> 
    <InsertParameters> 
     <asp:Parameter Name="ProductName" Type="String" /> 
     <asp:Parameter Name="UnitPrice" Type="Decimal" /> 
     <asp:Parameter Name="Discontinued" Type="Boolean" /> 
    </InsertParameters> 
</asp:SqlDataSource> 
1

而configurting的SqlDataSource當您配置select語句爲GridView,有一個選項爲「先進」。點擊然後點擊'生成更新,插入nad刪除語句'