zoukankan      html  css  js  c++  java
  • C# datagridview 删除行(转 学会、放弃博客)

    原文引入:http://zhangyanyansy.blog.163.com/blog/static/13530509720106171252978/

    datagridview 删除行  

    2010-07-17 13:25:29|  分类: C#|字号 订阅

     
     

     private void bindingNavigatorDeleteItem_Click(object sender, EventArgs e)
            {
                #region Shift多行删除
                if (this.dgvAdmin.SelectedRows.Count > 0)
                {
                    if (MessageBox.Show("确定要该管理员的信息吗?", "操作提示", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
                    {
                        Del();
                    }                
                }
                else
                {
                    MessageBox.Show("请选择要删除的信息!", "操作提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    return;
                }
                #endregion
            }

    public bool DelUser(int adminId)
            {
                bool result = false;
                try
                {
                    string sql = "delete from Admin where AdminId=" + adminId;
                    SqlCommand command = new SqlCommand(sql, DBHelper.connection);
                    DBHelper.connection.Open();
                    command.ExecuteNonQuery();
                    result = true;
                }
                catch (Exception ex)
                {
                    result = false;
                    Console.Write(ex.Message);                
                }
                finally
                {
                    DBHelper.connection.Close();
                }
                return result;           
            }


            /// <summary>
            /// 删除的方法
            /// </summary>
            public void Del()
            {
                int count = this.dgvAdmin.SelectedRows.Count;
                if (dgvAdmin.Rows.Count > 0)
                {
                    for (int i = count; i >= 1; i--)
                    {
                        int adminId = Convert.ToInt32(dgvAdmin.SelectedRows[i - 1].Cells["AdminId"].Value.ToString());
                        if (DelUser(adminId))
                        {
                            this.dgvAdmin.Rows.RemoveAt(dgvAdmin.SelectedRows[i - 1].Index);
                        }
                        else
                        {
                            MessageBox.Show("删除失败!", "操作提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
                            return;
                        }
                        
                    }
                    MessageBox.Show("删除成功!", "操作提示", MessageBoxButtons.OK, MessageBoxIcon.Error);  
                }
                else
                {
                    dgvAdmin.Rows.Clear();
                }

            }

  • 相关阅读:
    【C#】:浅谈反射机制 【转】
    ArcGIS Server 10中的切图/缓存机制深入【转】
    ArcGIS Server的切图原理深入【转】
    【OpenGL】用OpenGL shader实现将YUV(YUV420,YV12)转RGB-(直接调用GPU实现,纯硬件方式,效率高)
    MFC 带Ribbonbar的窗口 实现全屏和取消全屏
    C#.net开发 List与DataTable相互转换 【转】
    Net编程 详解DataTable用法【转】
    三维空间两直线/线段最短距离、线段计算算法 【转】
    OSG立体模式下动态修改相机远近裁剪面的实现
    用curl去探测接口是否正常返回结果,若没有正常返回则触发报警
  • 原文地址:https://www.cnblogs.com/meimao5211/p/3334204.html
Copyright © 2011-2022 走看看