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);
            }   
        }
    }
  • 相关阅读:
    LightOJ1002 分类: 比赛 最短路 2015-08-08 15:57 15人阅读 评论(0) 收藏
    DZY Loves Chemistry 分类: CF 比赛 图论 2015-08-08 15:51 3人阅读 评论(0) 收藏
    周赛-DZY Loves Chessboard 分类: 比赛 搜索 2015-08-08 15:48 4人阅读 评论(0) 收藏
    周赛-Equidistant String 分类: 比赛 2015-08-08 15:44 6人阅读 评论(0) 收藏
    周赛-Toy Cars 分类: 比赛 2015-08-08 15:41 5人阅读 评论(0) 收藏
    A Knight's Journey 分类: POJ 搜索 2015-08-08 07:32 2人阅读 评论(0) 收藏
    哈希-4 Values whose Sum is 0 分类: POJ 哈希 2015-08-07 09:51 3人阅读 评论(0) 收藏
    哈希-Gold Balanced Lineup 分类: POJ 哈希 2015-08-07 09:04 2人阅读 评论(0) 收藏
    哈希-Snowflake Snow Snowflakes 分类: POJ 哈希 2015-08-06 20:53 2人阅读 评论(0) 收藏
    欧拉通路-Play on Words 分类: POJ 图论 2015-08-06 19:13 4人阅读 评论(0) 收藏
  • 原文地址:https://www.cnblogs.com/perfect/p/715726.html
Copyright © 2011-2022 走看看