2013-05-18 70 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我编辑了我早先的答案。 –

相关问题