2013-05-18 72 views
0

我想通過按下另一種形式的按鈕從一個表單中的datagridview中刪除記錄。但我得到一個nullreferenceexception was unhandled錯誤。我是c#的新手,所以如果有人能寫我正確的代碼,我會非常感激。通過在gridview上按鈕單擊選擇來刪除sql表中的記錄

這是我到目前爲止。

private void button1_Click(object sender, EventArgs e) 
{ 
    SqlConnection con = new SqlConnection(@" Data Source=HOME-D2CADC8D4F\SQL;Initial Catalog=motociclete;Integrated Security=True"); 

    SqlCommand cmd = new SqlCommand(); 
    for (int i = 0; i < dataGridView1.Rows.Count; i++) 
    { 
     DataGridViewRow dr = dataGridView1.Rows[i]; 
     if (dr.Selected == true) 
     { 
      dataGridView1.Rows.RemoveAt(i); 
      try 
      { 
       con.Open(); 
       cmd.CommandText = "Delete from motociclete where codm=" + i + ""; 
       cmd.ExecuteNonQuery(); 
       con.Close(); 
      } 
      catch (Exception ex) 
      { 
       MessageBox.Show(ex.ToString()); 
      } 
     } 
    } 
    this.Close(); 
} 
+0

你在哪一行得到異常?我也注意到你在這裏指的是多種形式。有關這方面的更多信息可能會對我們有所幫助。 –

+0

我在這一行得到異常:for(int i = 0; i stefan9976

+0

當你得到一個異常時,你會得到一個行號,它告訴你它到底發生了什麼。如果您在該行上設置斷點,然後運行代碼以找出導致異常的事件,這會很有幫助。如果您在問題中(而不是在評論中,而是在問題本身中)爲我們確定了該行,則通過對該行進行評論或以其他方式標記該行,以便我們不必猜測,這也非常有幫助。畢竟,你知道*究竟是哪一行*,因爲例外給你提供了這些信息。請與我們分享。 –

回答

0

在遍歷他們你是刪除行,並導致NullPointerException因爲當你刪除一行,計數變化,但循環仍在繼續,只要初始計數。

一個這樣做的方法是創建行的臨時名單,將它們刪除後:

List<DataGridViewRow> rowstodelete = new List<DataGridViewRow>(); 

for (int i = 0; i < dataGridView1.Rows.Count; i++) 
{ 
    DataGridViewRow dr = dataGridView1.Rows[i]; 
    if (dr.Selected) 
    { 
     rowstodelete.Add(dr); 
     try 
     { 
       con.Open(); 
       cmd.CommandText = "Delete from motociclete where codm=" + i + ""; 
       cmd.ExecuteNonQuery(); 
       con.Close(); 
     } 
     catch (Exception ex) 
     { 
       MessageBox.Show(ex.ToString()); 
     } 
    } 
} 

foreach (DataGridViewRow row in rowstodelete) 
{ 
    dataGridView1.Rows.Remove(row); 
} 
+0

我仍然在這條線上得到相同的錯誤︰for(int i = 0; i stefan9976

+0

當你concatenate時,自動強制爲一個字符串它的刪除語句? – Tim

+0

我不知道是不是。 – stefan9976

1

只是顛倒循環的詩句。

for (int i = dataGridView1.Rows.Count - 1; i >= 0 ; i--) 

這樣你的循環不受變動數行

同樣的。這是我不會打開/關閉在每個命令執行的連接的情況下,如果你以這種方式使用參數的命令執行可能是更好的性能

using(SqlConnection con = new SqlConnection(@" Data Source=HOME-D2CADC8D4F\SQL;Initial Catalog=motociclete;Integrated Security=True")) 
using(SqlCommand cmd = new SqlCommand("Delete from motociclete where [email protected]", con)) 
{ 
    con.Open(); 
    cmd.Parameters.AddWithValue("@id", 0); 
    for (int i = dataGridView1.Rows.Count-1; i >= 0; i++) 
    { 
     DataGridViewRow dr = dataGridView1.Rows[i]; 
     if (dr.Selected == true) 
     { 
      dataGridView1.Rows.RemoveAt(i); 
      cmd.Parameters["@id"].Value = i; 
      cmd.ExecuteNonQuery(); 
     } 
    } 
} 
+0

我仍然得到錯誤。 – stefan9976

+0

您的代碼未設置與命令的連接。 – Steve

+0

在for [for(i = dateGridView1.Rows.Count-1)行上獲得NullReferenceException的唯一可能性是由於行或datagridview爲空。 – Steve

0
private void button1_Click(object sender, EventArgs e) 
{ int row =-1; 

     SqlConnection con = new SqlConnection(@" Data Source=HOME-D2CADC8D4F\SQL;Initial Catalog=motociclete;Integrated Security=True"); 

    SqlCommand cmd = new SqlCommand(); 
    row = new_tab_Object.CurrentCell.RowIndex; 

      if (row!= (-1)) 
      { 

       new_tab_Object.Rows.RemoveAt(row);     
       row = -1; 
      try 
      { 
       con.Open(); 
       cmd.CommandText = "Delete from motociclete where codm=" + row + ""; 
       cmd.ExecuteNonQuery(); 
       con.Close(); 
      } 
      catch (Exception ex) 
      { 
       MessageBox.Show(ex.ToString()); 
      } 
     } 

    this.Close(); 
    } 

你這...

+0

我在row和new_tab_Object.CurrentCell .RowIndex – stefan9976

+0

用你的GridView對象替換new_tab_Object對於ex DataGridView dr = new DataGridView(); – sanjeev

+0

我仍然有if(row)錯誤,它說「不能隱式地將類型'int'轉換爲'bool'」。你也幫我過嗎? – stefan9976

0

爲什麼你通過所有行中循環,使用dataGridView1.SelectedRows:

for (int i = dataGridView1.SelectedRows.Count - 1; i >= 0; i--) 
    dataGridView1.Rows.Remove(dataGridView1.SelectedRows[i]); 

而且通過使用一個DataTable和BindingSource的綁定您的數據。

0

我看到您正在嘗試訪問另一個窗體的網格。確保你正確地得到你的表單的引用(使用表格的引用),並使用它引用表格。以達到您可能必須公開您的網格對象。

在你父窗體公開的屬性網格

public GridView Grid 
{ 
    return dataGridView1; 
} 

..和當您啓動新的形式(比如ChildForm)做這樣的

Form child = new ChildForm(this); 
child.Show(); 

請更改你的孩子表單使用構造函數參數。

private Form m_ParentForm; 
public ChildForm(Form child) 
{ 
    m_ParentForm = child; 
} 

..和你的循環應該是這樣

for (int i = 0; i < m_ParentForm.Grid.Rows.Count; i++) 

希望這有助於。

+0

我可能是從一種形式到另一種形式的連接。我必須寫表單parentForm = refObject;在第一種形式?什麼是refObject? – stefan9976

+0

@ stefan9976我編輯了我早先的答案。 –

相關問題