2011-06-16 69 views
1

當前我正在使用哪種標準方式在GridView控件中執行編輯。但是這允許我一次只執行一個操作。在我的應用程序中,當一個字段的值變化時,當用戶點擊「更新」時。應該更新行並且還應該觸發一個刪除查詢。在編輯操作同時發生時,在ASP.NET中的GridView控件中執行刪除操作

我嘗試使用手動編輯

<asp:GridView ID="GridView1" runat="server" AllowPaging="True" 
       AllowSorting="True" AutoGenerateColumns="False" DataKeyNames="ID" 
       DataSourceID="sdsample1" Visible="False" OnRowEditing="GridView1_OnRowEditing" OnRowUpdated="GridView1_OnRowUpdated"> 

- 想獲得在backnd當前編輯行的GridView中CheckBoxField字段值和8列

<asp:CheckBoxField DataField="Goal_Type" HeaderText="Goal_Type" 
         SortExpression="Goal_Type" /> 


protected void GridView1_OnRowEditing(object sender, GridViewEditEventArgs e) 
{ 

((CheckBox)GridView1.Rows[GridView1.SelectedIndex].FindControl("Goal_Type")); 
CheckBox chk2 = ((CheckBox)GridView1.Rows[GridView1.EditIndex].Cells[7].Controls[0]); 
goal_type =Convert.ToString(chk2.Checked); 
if(goal_type.Equals("False")) 
Goal_flag.Value ="0"; 
else 
Goal_flag.Value = "1"; 
} 

然後,一旦我得到checkbox字段的值設置了一些標誌變量,我在下面的函數中執行刪除操作

protected void GridView1_OnRowUpdated(object sender, GridViewUpdatedEventArgs e) 
{ 

CheckBox chk2 = ((CheckBox)GridView1.Rows[e.AffectedRows].Cells[7].Controls[0]); 
Boolean goal_type = chk2.Checked; 
if (goal_type == true && Goal_flag.Value.Equals("0")) 
{ 

string connectionString = 
WebConfigurationManager.ConnectionStrings["DataCollectionConnectionString"].ConnectionString; 
SqlConnection con = new SqlConnection(connectionString); 
SqlCommand cmd; 
int ID = Convert.ToInt32(((TextBox)GridView1.Rows[e.AffectedRows].FindControl("ID")).Text); 
  

cmd =new SqlCommand("Delete from XXX WHERE (ID = " + ID + ") ", con); 
  

cmd.CommandType = 

CommandType.Text; 
con.Open(); 

  

cmd.ExecuteNonQuery(); 

con.Close(); 

} 

我在「GridView1_OnRowEditing」函數本身出現此錯誤 索引超出範圍。必須是非負數且小於集合的大小。 參數名稱:索引

請讓我知道如果我的方法在GridView中同時操作是正確的。

回答

0

將RowEditing事件用於多個更新。在本issue提到:

protected void ASPxGridView1_RowUpdating(object sender, DevExpress.Web.Data.ASPxDataUpdatingEventArgs e) { 
    datasource.UpdateCommand = "UPDATE ....."; 
    datasource.Update(...); 

    datasource.UpdateCommand = "UPDATE ....."; 
    datasource.Update(...); 

    datasource.UpdateCommand = "UPDATE ....."; 
    datasource.Update(...); 

    e.Cancel = true; 
    ASPxGridView1.CancelEdit(); 
} 

爲了找到控制我建議在這個問題中提到的方法: http://www.devexpress.com/Support/Center/p/Q91970.aspx

protected void grd_ReportChartSeries_RowUpdating(object sender, DevExpress.Web.Data.ASPxDataUpdatingEventArgs e) { 
    ASPxGridView gridView = sender as ASPxGridView; 
    ASPxComboBox combo = gridView.FindEditRowCellTemplateControl(gridView.Columns["Data2"] as GridViewDataColumn, "comboData2") as ASPxComboBox; 
    object data2 = combo.Value; 

    combo = gridView.FindEditRowCellTemplateControl(gridView.Columns["ID"] as GridViewDataColumn, "comboID") as ASPxComboBox; 
    object ID = combo.Value; 

    ASPxTextBox textBox = gridView.FindEditRowCellTemplateControl(gridView.Columns["Data1"] as GridViewDataColumn, "txtData1") as ASPxTextBox; 
    object data1 = textBox.Value; 

    ds = Session["DataSet"] as DataSet; 
    DataTable dataTable = ds.Tables[0]; 
    DataRow row = dataTable.Rows.Find(e.Keys[0]); 
    row["ID"] = ID; 
    row["Data1"] = data1; 
    row["Data2"] = data2; 
    gridView.CancelEdit(); 
    e.Cancel = true; 

} 

或者你可以做更新,並通過一個存儲過程調用刪除。

相關問題