删除后,datagridview应该刷新,但它不能在Windows窗体中工作 [英] after delete datagridview should be refresh but itsnot working in windows form

查看:78
本文介绍了删除后,datagridview应该刷新,但它不能在Windows窗体中工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  private   void  Delete_Click(对象发​​件人,EventArgs e)
{
for int i = 0 ; i < dataGridView2.SelectedRows.Count; i ++)
{
string sql = 从[删除] SAPProduction]。[ProductionData] + 其中id =' + dataGridView2.CurrentRow .Cells [ id]。Value.ToString()+ ';
// Jobcard =您的愿望行名片ID

SqlCommand cmd = new SqlCommand(sql,objConn1);
objConn1.Open();
cmd.ExecuteNonQuery();
objConn1.Close();
MessageBox.Show( 记录已成功删除...);
dataGridView2.Refresh();

}
}

解决方案

目前,您调用dataGridView2.Refresh();在for循环中。这意味着在删除第一个选定的行后,数据网格将被刷新,选择将丢失,因此其他所选行不会被删除。

只需将其放在稍后位置,例如

  private  < span class =code-keyword> void  Delete_Click( object  sender,EventArgs e)
{
objConn1.Open() ;
for int i = 0 ; i < dataGridView2.SelectedRows.Count; i ++)
{
string sql = 从[SAPProduction]中删除。[ProductionData]其中id =' + dataGridView2。 CurrentRow.Cells [ id]。值+ ';
SqlCommand cmd = new SqlCommand(sql,objConn1);
cmd.ExecuteNonQuery();
}
objConn1.Close();
MessageBox.Show( 记录已成功删除...);
dataGridView2.Refresh();
}



除了这个改变之外,你还应该使用参数化查询。






  private   void  Delete_Click( object  sender,EventArgs e)
{
for int i = 0 ; i < dataGridView2.SelectedRows.Count; i ++)
{
string sql = 从[SAPProduction]中删除。[ProductionData] + 其中id =' + dataGridView2.CurrentRow.Cells [ id]。 .ToString()+ ';
// Jobcard =您的愿望行名片ID

SqlCommand cmd = new SqlCommand(sql,objConn1);
objConn1.Open();
cmd.ExecuteNonQuery();
objConn1.Close();
MessageBox.Show( 记录已成功删除...);
// dataGridView2.Refresh();
// 调用将数据绑定到datagridview2的函数或从datagridview2中删除当前行

}
}





http://social.msdn.microsoft.com/forums/en-US/winformsdatacontrols/thread/3970aea7-de83-4c53-8034-83aaa9f7a9a1 [ ^ ]


datagridview.Refresh()从数据源重绘网格视图

可能是你个案中的DataSet );



它没有从数据库重新加载 DataSet



恕我直言,您可以在成功删除消息后简单地绑定您的代码。这会更简单。



  private   void  Delete_Click( object  sender,EventArgs e)
{
for int i = 0 ; i < dataGridView2.SelectedRows.Count; i ++)
{
string sql = 从[SAPProduction]中删除。[ProductionData] + 其中id =' + dataGridView2.CurrentRow.Cells [ id]。Value.ToString()+ ';
// Jobcard =您的愿望行名片ID

SqlCommand cmd = new SqlCommand(sql,objConn1);
objConn1.Open();
cmd.ExecuteNonQuery();
objConn1.Close();
MessageBox.Show( 记录已成功删除...);
// 而不是这个
dataGridView2.Refresh();
// 在这里填写网格
}
}


private void Delete_Click(object sender, EventArgs e)
       {
           for (int i = 0; i < dataGridView2.SelectedRows.Count; i++)
           {
               string sql = "Delete from  [SAPProduction].[ProductionData]  " + " Where id = '" + dataGridView2.CurrentRow.Cells["id"].Value.ToString() + "'  ";
               // Jobcard = your desire Rows Jobcard Id

               SqlCommand cmd = new SqlCommand(sql, objConn1);
               objConn1.Open();
               cmd.ExecuteNonQuery();
               objConn1.Close();
               MessageBox.Show("Record Deleted successfully...");
               dataGridView2.Refresh();

           }
       }

解决方案

Presently, you call dataGridView2.Refresh(); in the for loop. This means that after deleting the first of the selected rows, the datagrid gets refreshed, and the selection is lost, consequently the other selected rows won''t be deleted.
Just place that at a later position, something like

private void Delete_Click(object sender, EventArgs e)
   {
       objConn1.Open();
       for (int i = 0; i < dataGridView2.SelectedRows.Count; i++)
       {
           string sql = "Delete from  [SAPProduction].[ProductionData]  Where id = '" + dataGridView2.CurrentRow.Cells["id"].Value + "'";
           SqlCommand cmd = new SqlCommand(sql, objConn1);
           cmd.ExecuteNonQuery();
       }
       objConn1.Close();
       MessageBox.Show("Record Deleted successfully...");
       dataGridView2.Refresh();
   }


Apart from that change, you should also use a parameterized query.


Hi,

private void Delete_Click(object sender, EventArgs e)
       {
           for (int i = 0; i < dataGridView2.SelectedRows.Count; i++)
           {
               string sql = "Delete from  [SAPProduction].[ProductionData]  " + " Where id = '" + dataGridView2.CurrentRow.Cells["id"].Value.ToString() + "'  ";
               // Jobcard = your desire Rows Jobcard Id

               SqlCommand cmd = new SqlCommand(sql, objConn1);
               objConn1.Open();
               cmd.ExecuteNonQuery();
               objConn1.Close();
               MessageBox.Show("Record Deleted successfully...");
//               dataGridView2.Refresh();
// call the function which will bind the data to datagridview2 or remove the current row from datagridview2

           }
       }



http://social.msdn.microsoft.com/forums/en-US/winformsdatacontrols/thread/3970aea7-de83-4c53-8034-83aaa9f7a9a1[^]


datagridview.Refresh() redraws the grid-view from the data source
(presumably a DataSet in your case);

It doesn''t reload the DataSet from the database.

IMHO, You can simply bind your code after successful deletion message. That would be more simpler.

private void Delete_Click(object sender, EventArgs e)
       {
           for (int i = 0; i < dataGridView2.SelectedRows.Count; i++)
           {
               string sql = "Delete from  [SAPProduction].[ProductionData]  " + " Where id = '" + dataGridView2.CurrentRow.Cells["id"].Value.ToString() + "'  ";
               // Jobcard = your desire Rows Jobcard Id

               SqlCommand cmd = new SqlCommand(sql, objConn1);
               objConn1.Open();
               cmd.ExecuteNonQuery();
               objConn1.Close();
               MessageBox.Show("Record Deleted successfully...");
               //Instead of this
               dataGridView2.Refresh();
               // Fill your grid here
           }
       }


这篇关于删除后,datagridview应该刷新,但它不能在Windows窗体中工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆