zoukankan      html  css  js  c++  java
  • 在dataGridView中实现批量删除

    dataGridView设计如下图:


    代码如下:

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Data.SqlClient;
    using System.Drawing;
    using System.Text;
    using System.Windows.Forms;

    namespace test
    {
        
    public partial class Form1 : Form
        {
            
    public Form1()
            {
                InitializeComponent();
            }
            LinkDB linkdb 
    = new LinkDB();
            
    string strSelectAll = "select * from tb1";
            
    string strTable = "tb1";
            DataSet ds 
    = new DataSet();
            
    private void Form1_Load(object sender, EventArgs e)
            {
                dataGridView1.AutoGenerateColumns 
    = false;
                dataGridView1.AllowUserToAddRows 
    = false;
                dataGridView1.AlternatingRowsDefaultCellStyle.BackColor 
    = Color.Azure;
                ds 
    = linkdb.QueryDB(strSelectAll, strTable);
                dataGridView1.DataSource 
    = ds;
                dataGridView1.DataMember 
    = strTable;
            }

            
    bool blIsSelectAll = false;
            
    private void btnSelectAll_Click(object sender, EventArgs e)
            {
                
    //写法1
                
    //if (dataGridView1.Rows.Count > 0)
                
    //{
                
    //    for (int i = 0; i < dataGridView1.Rows.Count; i++)
                
    //    {
                
    //        dataGridView1[0, i].Value = true;
                
    //    }
                
    //}
                
    //blIsSelectAll = true;
                
    //return;

                
    //写法2
                foreach (DataGridViewRow dr in dataGridView1.Rows)
                {
                    ((DataGridViewCheckBoxCell)dr.Cells[
    0]).Value = true;
                }
                blIsSelectAll 
    = true;
                
    return;
            }

            
    private void btnCancelAll_Click(object sender, EventArgs e)
            {
                
    foreach (DataGridViewRow dr in dataGridView1.Rows)
                {
                    ((DataGridViewCheckBoxCell)dr.Cells[
    0]).Value = false;
                }
                blIsSelectAll 
    = false;
                
    return;
            }

            
    private void btnDelete_Click(object sender, EventArgs e)
            {
                
    try
                {
                    
    if (dataGridView1.Rows.Count > 0)
                    {
                        
    if (blIsSelectAll) //全选:进行批量删除
                        {
                            
    for (int rowIndex = 0; rowIndex < ds.Tables[strTable].Rows.Count; rowIndex++)
                            {
                                ds.Tables[strTable].Rows[rowIndex].Delete();
    //逻辑性删除(从数据集ds中删除)
                                
    //物理性删除(从DB中删除)
                            }
                        }
                        
    else  //删除所选择的行
                        {
                            List
    <DataGridViewRow> tmpList = new List<DataGridViewRow>();
                            
    for (int j = 0; j < dataGridView1.Rows.Count; j++)
                            {
                                
    if (Convert.ToBoolean(dataGridView1[0, j].EditedFormattedValue.ToString()))
                                {
                                    tmpList.Add(dataGridView1.Rows[j]);
                                }
                            }
                            
    for (int i = 0; i < tmpList.Count; i++)
                            {
                                dataGridView1.Rows.Remove(tmpList[i]);
                            }
                            tmpList 
    = null;
                        }
                    }
                }
                
    catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
            }

            
    private void dataGridView1_CellValueChanged(object sender, DataGridViewCellEventArgs e)
            {
                blIsSelectAll 
    = false;
            }

            
    private void dataGridView1_RowPostPaint(object sender, DataGridViewRowPostPaintEventArgs e)
            {
                SolidBrush b 
    = new SolidBrush(Color.Black);
                e.Graphics.DrawString(Convert.ToString(e.RowIndex 
    + 1), e.InheritedRowStyle.Font, b, e.RowBounds.X + 15, e.RowBounds.Y + 1);
            }   
        }
    }
  • 相关阅读:
    oracle性能监控
    MySQL Explain详解
    oracle中merge into用法解析
    Mysql常见函数
    Quartz -第一篇-入门
    搭建zookeeper集群
    linux 安装mysql
    mysql无法远程访问
    ActiveMQ 持久化
    Nyoj 城市平乱(图论)
  • 原文地址:https://www.cnblogs.com/perfect/p/715726.html
Copyright © 2011-2022 走看看