• 大小: 58KB
    文件类型: .zip
    金币: 1
    下载: 0 次
    发布日期: 2021-06-10
  • 语言: C#
  • 标签:

资源简介

C#winform DataGridView checkbox复选框 实现单选效果,系统默认的是界面中多选,下午没事,做个单选的效果,同时复习下datagridview的常用知识,包含dantGridVIew的常用操作内容,注意事项,代码有点乱,学习。

资源截图

代码片段和文件信息

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

namespace WindowsFormsApplication2
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        string conStr = “Data Source=PC-20130515FUTQ;Initial Catalog=test;Integrated Security=True“;
        private void Form1_Load(object sender EventArgs e)
        {
            using (SqlConnection con = new SqlConnection())
            {
                try
                {

                    con.ConnectionString = conStr;
                    con.Open();
                    SqlCommand cmd = new SqlCommand();
                    cmd.Connection = con;
                    cmd.CommandText = “select * from Person“;

                    DataTable dt = new System.Data.DataTable();
                    SqlDataReader dr = cmd.ExecuteReader();
                    dt.Load(dr);
                    con.Close();
                    cmd.Dispose();
                    dataGridView1.DataSource = dt;
                }
                catch
                {
                }
            }
        }

        //单击单元格(无论单击的是单元格为内容还是单元格为列头)。
        private void dataGridView1_CellClick(object sender DataGridViewCellEventArgs e)
        {
            //判断是否单击了列头  如果单击了列头e.RowIndex的值为-1;
            if (e.RowIndex >= 0)
            {
                DataGridViewCheckBoxCell cell = null;
                if (e.ColumnIndex == 0)
                {
                    cell= dataGridView1.Rows[e.RowIndex].Cells[0] as DataGridViewCheckBoxCell;
                    
                }
                if (cell != null)
                {
                  //  MessageBox.Show(“chen“);
                }
                //else if (column is DataGridViewColumn)
                //{
                //    grdPerson.Rows[e.RowIndex].Cells[0].Value = true;
                //    // ((DataGridViewColumn)column).Selected = true;
                //}
                DataGridViewColumn column = dataGridView1.Columns[e.ColumnIndex];
                if (column is DataGridViewCheckBoxColumn)
                {
                   
                }

            }
        }
        //MultiSelect设置为fasle,用户一次可以多选(按住ctrl键进行选择,不然只为一条)  
       // 始终在SelectedRows集合中只有一行
        //如果没有按住ctrl键进行点击选择无论MultiSelect设置为fasle或true始终选择一行
        private void button1_Click(object sender EventArgs e)
        {
            //选择的行数
            MessageBox.Show(dataGridView1.SelectedRows.Count.ToString());
            //选择的行
            MessageBox.Show(dataGridView1.SelectedRows[0].Cells[0].RowIndex.ToString());
        }

        //MultiSelect设置为true遍历chexkBox选中的行
        private void button2_Click(object sender EventArgs e)

 属性            大小     日期    时间   名称
----------- ---------  ---------- -----  ----
     目录           0  2013-09-14 21:02  DataGridView摸索\
     目录           0  2013-09-14 21:02  DataGridView摸索\WindowsFormsApplication2\
     文件         187  2013-09-14 21:01  DataGridView摸索\WindowsFormsApplication2\App.config
     目录           0  2013-09-14 21:01  DataGridView摸索\WindowsFormsApplication2\bin\
     目录           0  2013-09-14 21:07  DataGridView摸索\WindowsFormsApplication2\bin\Debug\
     文件       12800  2013-09-14 22:49  DataGridView摸索\WindowsFormsApplication2\bin\Debug\WindowsFormsApplication2.exe
     文件         187  2013-09-14 21:01  DataGridView摸索\WindowsFormsApplication2\bin\Debug\WindowsFormsApplication2.exe.config
     文件       28160  2013-09-14 22:49  DataGridView摸索\WindowsFormsApplication2\bin\Debug\WindowsFormsApplication2.pdb
     文件       22984  2013-09-14 22:50  DataGridView摸索\WindowsFormsApplication2\bin\Debug\WindowsFormsApplication2.vshost.exe
     文件         187  2013-09-14 21:01  DataGridView摸索\WindowsFormsApplication2\bin\Debug\WindowsFormsApplication2.vshost.exe.config
     文件         490  2012-06-06 02:06  DataGridView摸索\WindowsFormsApplication2\bin\Debug\WindowsFormsApplication2.vshost.exe.manifest
     文件        5904  2013-09-14 22:50  DataGridView摸索\WindowsFormsApplication2\Form1.cs
     文件        5663  2013-09-14 22:42  DataGridView摸索\WindowsFormsApplication2\Form1.Designer.cs
     文件        6553  2013-09-14 22:04  DataGridView摸索\WindowsFormsApplication2\Form1.resx
     目录           0  2013-09-14 21:01  DataGridView摸索\WindowsFormsApplication2\obj\
     目录           0  2013-09-14 22:49  DataGridView摸索\WindowsFormsApplication2\obj\Debug\
     文件         851  2013-09-14 21:02  DataGridView摸索\WindowsFormsApplication2\obj\Debug\DesignTimeResolveAssemblyReferences.cache
     文件        7044  2013-09-14 21:02  DataGridView摸索\WindowsFormsApplication2\obj\Debug\DesignTimeResolveAssemblyReferencesInput.cache
     文件           0  2013-09-14 21:02  DataGridView摸索\WindowsFormsApplication2\obj\Debug\TemporaryGeneratedFile_036C0B5B-1481-4323-8D20-8F5ADCB23D92.cs
     文件           0  2013-09-14 21:02  DataGridView摸索\WindowsFormsApplication2\obj\Debug\TemporaryGeneratedFile_5937a670-0e60-4077-877b-f7221da3dda1.cs
     文件           0  2013-09-14 21:02  DataGridView摸索\WindowsFormsApplication2\obj\Debug\TemporaryGeneratedFile_E7A71F73-0F8D-4B9B-B56E-8E70B10BC5D3.cs
     目录           0  2013-09-14 21:02  DataGridView摸索\WindowsFormsApplication2\obj\Debug\TempPE\
     文件        1194  2013-09-14 22:50  DataGridView摸索\WindowsFormsApplication2\obj\Debug\WindowsFormsApplication2.csproj.FileListAbsolute.txt
     文件         975  2013-09-14 22:04  DataGridView摸索\WindowsFormsApplication2\obj\Debug\WindowsFormsApplication2.csproj.GenerateResource.Cache
     文件        2143  2013-09-14 22:35  DataGridView摸索\WindowsFormsApplication2\obj\Debug\WindowsFormsApplication2.csprojResolveAssemblyReference.cache
     文件       12800  2013-09-14 22:49  DataGridView摸索\WindowsFormsApplication2\obj\Debug\WindowsFormsApplication2.exe
     文件         180  2013-09-14 22:04  DataGridView摸索\WindowsFormsApplication2\obj\Debug\WindowsFormsApplication2.Form1.resources
     文件       28160  2013-09-14 22:49  DataGridView摸索\WindowsFormsApplication2\obj\Debug\WindowsFormsApplication2.pdb
     文件         180  2013-09-14 21:07  DataGridView摸索\WindowsFormsApplication2\obj\Debug\WindowsFormsApplication2.Properties.Resources.resources
     文件         536  2013-09-14 21:02  DataGridView摸索\WindowsFormsApplication2\Program.cs
     目录           0  2013-09-14 21:01  DataGridView摸索\WindowsFormsApplication2\Properties\
............此处省略8个文件信息

评论

共有 条评论

相关资源