小妹分数不多,谢谢各位好心人帮助了。代码如下:private void button1_Click(object sender, EventArgs e)
{
try
{ for (int n = 0; n < datagridview1.RowCount; n++)
{
if (datagridview1[0, n].Value != null && datagridview1[1, n].Value != null && datagridview1[2, n].Value != null && datagridview1[3, n].Value!=null)//datagridview1有四列,判断是否为空
{
try
{
SQL s = new SQL();//SQL是自己定义的一个数据库操作类,这里是为了连接数据库这一句应该没问题,我在别的地方大量使用
string tempTime = tempStr;//tempStr是public类型,从datagridview1_Click里面传递过来的,记录鼠标点击了哪一行 string sqlStr = "delete from my_table where storeTime= '" + tempTime.ToString() + "'";//storeTime是数据库表中的一列,就是记录文件保存时刻的,是varchar(50)类型
//以下是SQL类里面的操作,下面这几句都没有问题,在程序中别的地方大量使用
SqlConnection conn = new SqlConnection(s.ConnectionString);
conn.Open();
SqlCommand command = new SqlCommand(sqlStr, conn);
command.ExecuteNonQuery();
command.Dispose();
conn.Close();
conn.Dispose();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
else
continue;
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
private void datagridview1_Click(object sender, EventArgs e)
{
tempStr = this.datagridview1.SelectedCells[2].Value.ToString();//要取的是第3列
}
{
try
{ for (int n = 0; n < datagridview1.RowCount; n++)
{
if (datagridview1[0, n].Value != null && datagridview1[1, n].Value != null && datagridview1[2, n].Value != null && datagridview1[3, n].Value!=null)//datagridview1有四列,判断是否为空
{
try
{
SQL s = new SQL();//SQL是自己定义的一个数据库操作类,这里是为了连接数据库这一句应该没问题,我在别的地方大量使用
string tempTime = tempStr;//tempStr是public类型,从datagridview1_Click里面传递过来的,记录鼠标点击了哪一行 string sqlStr = "delete from my_table where storeTime= '" + tempTime.ToString() + "'";//storeTime是数据库表中的一列,就是记录文件保存时刻的,是varchar(50)类型
//以下是SQL类里面的操作,下面这几句都没有问题,在程序中别的地方大量使用
SqlConnection conn = new SqlConnection(s.ConnectionString);
conn.Open();
SqlCommand command = new SqlCommand(sqlStr, conn);
command.ExecuteNonQuery();
command.Dispose();
conn.Close();
conn.Dispose();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
else
continue;
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
private void datagridview1_Click(object sender, EventArgs e)
{
tempStr = this.datagridview1.SelectedCells[2].Value.ToString();//要取的是第3列
}
用
!DbNull.Value.Equals(dataGridView1[0, n].Value)
你断点调试,一步步F10走,很容易发现错误
{
tempStr = (this.datagridview1.SelectedCells[2].Value==null?" ":this.datagridview1.SelectedCells[2].Value.ToString());//要取的是第3列
}
string tempTime = tempStr;//tempStr是public类型,从datagridview1_Click里面传递过来的,记录鼠标点击了哪一行