2012-08-31 229 views
0

我有一個GridView它具有這樣的特性:數據庫行不會通過GridView控件更新更新

OnRowUpdating="GridViewRowUpdateEventHandler"

在GridView的,我有以下的控制:

<asp:TemplateField HeaderText="Progress" SortExpression="progress"> 
    <ItemTemplate> 
     <asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="true"> 
      <asp:ListItem Value="0">Incomplete</asp:ListItem> 
      <asp:ListItem Value="1">Complete</asp:ListItem> 
     </asp:DropDownList> 
    </ItemTemplate> 
</asp:TemplateField> 

GridViewRowUpdateEventHandler看起來是這樣的:

protected void GridViewRowUpdateEventHandler(object sender, GridViewUpdateEventArgs e) 
{ 
    SqlConnection connection; 
    SqlCommand command; 

    GridViewRow row = (GridViewRow)GridView1.Rows[e.RowIndex]; 

    DropDownList ddlPriority = (DropDownList)row.FindControl("DropDownList1"); 

    using (connection = new SqlConnection(ConfigurationManager.AppSettings["connString"])) 
    { 
     using (command = new SqlCommand(@"update table1 set priority = @priority where id = @id", connection)) 
     { 
      command.Parameters.Add("@priority", SqlDbType.Int, 1).Value = ddlPriority.SelectedValue; 
      command.Parameters.Add("@id", SqlDbType.Int, 1).Value = row.RowIndex; 

      connection.Open(); 
      command.ExecuteNonQuery(); 
      connection.Close(); 
     } 
    } 

    GridView1.DataBind(); 
} 

我沒有收到錯誤消息所有,並且數據庫中的相關行未被更新。有人知道爲什麼

難道是我看着錯誤的單元格Cells[5]?或者也許是因爲我在page_load中沒有任何東西?或者也許別的東西?

+0

如果您在查看錯誤的單元格,它會引發您指向下拉列表的異常 – Divi

+0

根據答案更新了更新代碼,但我仍然得到相同的結果,即沒有錯誤,也沒有數據庫更新。我想我必須首先在我的PC上進行VS2008調試。 – oshirowanen

回答

1

由於DropDiownList處於TemplateField和它的NamingContainer是GridViewRow,你應該使用row.FindControl獲得參考:

DropDownList ddlPriority = (DropDownList)row.FindControl("DropDownList1"); 

代替

DropDownList ddlPriority = (DropDownList)row.Cells[5].FindControl("DropDownList1"); 

但不是核心您的問題自row.Cells[5].FindControl也可能工作,當它在第六個單元格。否則,你會得到一個NullreferenceException

我假設你也上回發綁定GridView的,你應該檢查IsPostBack屬性:

protected void Page_Load() 
{ 
    if (!IsPostBack) 
    { 
     BindGrid(); 
    } 
} 

除了:

command.Parameters.Add("@id", SqlDbType.Int, 1).Value = row; 

不起作用,因爲row是GridViewRow。您的GridView應該也在DataBind之後,否則這些更改將不會被反映出來。

+0

根據答案更新了更新代碼,但我仍然得到相同的結果,即沒有錯誤,也沒有數據庫更新。我想我必須首先在我的PC上進行VS2008調試。此外,我沒有綁定回發,我根本沒有BindGrid()... – oshirowanen

+0

@oshirowanen:那麼你使用'SqlDataSource'或任何其他聲明性數據源控件?是的,使用調試器來查看會發生什麼。 –

+0

是的,我使用''控件。只要我有調試排序,我會回到這裏。 – oshirowanen

1

你在你的GridViewRowUpdateEventHandler

GridView.DataBind(); 

爲了末尾添加DataBind找到控制

var ddlPriority = (DropDownList)row.FindControl("DropDownList1"); 
+0

根據答案更新了更新代碼,但我仍然得到相同的結果,即沒有錯誤,也沒有數據庫更新。我想我必須首先在我的PC上進行VS2008調試。 – oshirowanen

+0

好吧,我在這裏如果你需要幫助,當你調試 –

1

我會假設你已經忘記重新綁定的GridView數據

gridview1.DataSource = YOUR_DATASOURCE; 
gridview1.DataBind(); 

而且你應該

DropDownList ddlPriority = (DropDownList) row.FindControl("DropDownList1"); 

編輯

發現一個錯誤

command.Parameters.Add("@id", SqlDbType.Int, 1).Value = row; // wrong! primaryKey value needed! 

這將無法正常工作,行式GridViewRow的訪問您的下拉列表。你應該分配你的primaryKey值!

無論您決定先做什麼,我都會添加一些Debug.WriteLine(..)語句來查看將發送到SQL Server的值。

+0

由於某種原因,我嘗試使用VS2008進行調試,它不起作用。我想它沒有正確配置。任何其他方式來檢查值?你的項目開始按鈕旁邊的 – oshirowanen

+0

應該是「Debug」,並且在你的web.config裏面,像給它試試。調試對於各種應用程序至關重要! –

+1

根據答案更新了更新代碼,但我仍然得到相同的結果,即沒有錯誤,也沒有數據庫更新。我想我必須首先在我的PC上進行VS2008調試。 – oshirowanen

1

我認爲問題在於您使用行的rowIndex作爲ID。所以在最後你的SQL命令會是這樣的

update table1 set priority = 'blah' where id = 0 //(where its the first row and so on) 

所以這是一個非常好的查詢,這將沒有錯誤或異常運行,但沒有ID = 0您綁定到錯誤的ID 。你需要綁定它的是表中的ID。

您可以通過運行SQL事件探查器來檢查此問題,並且您應該能夠找到正在發送到數據庫的查詢。