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);
            }   
        }
    }
  • 相关阅读:
    Smali基本语法
    图片智能缩小
    How to install ia32-libs in Ubuntu 14.04 LTS (Trusty Tahr)
    [操作系统][Ubuntu 14.04] 安装Flash 安装QQ2013
    eclipse在Ubuntu 13.04下的安装过程及问题小记
    Android系统手机端抓包方法
    Android 开源框架ActionBarSherlock 和 ViewPager 仿网易新闻客户端
    试用Android Annotations
    Android Annotations 介绍
    盘点国内Android移动广告平台的现状
  • 原文地址:https://www.cnblogs.com/perfect/p/715726.html
Copyright © 2011-2022 走看看