WinForm Linq初學
獲取容器類控件CheckBox全部選中值
--實例1 帶核取功能的DataGridView---
1.Winform=>加入>使用者控制項>ucDataGirdView.cs
2.Designer頁面代碼
`namespace GridTest
{
partial class ucDataGirdView
{
///
/// 設計工具所需的變數。
///
private System.ComponentModel.IContainer components = null;
/// <summary>
/// 清除任何使用中的資源。
/// </summary>
/// <param name="disposing">如果應該處置 Managed 資源則為 true,否則為 false。</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#region 元件設計工具產生的程式碼
/// <summary>
/// 此為設計工具支援所需的方法 - 請勿使用程式碼編輯器
/// 修改這個方法的內容。
/// </summary>
private void InitializeComponent()
{
this.tableLayoutPanel1 = new System.Windows.Forms.TableLayoutPanel();
this.dgvData = new System.Windows.Forms.DataGridView();
this.ckbSelectAll = new System.Windows.Forms.CheckBox();
this.btnDeleteSelections = new System.Windows.Forms.Button();
this.lblVal = new System.Windows.Forms.Label();
this.btnReverse = new System.Windows.Forms.Button();
this.blSelect = new System.Windows.Forms.DataGridViewCheckBoxColumn();
this.tableLayoutPanel1.SuspendLayout();
((System.ComponentModel.ISupportInitialize)(this.dgvData)).BeginInit();
this.SuspendLayout();
//
// tableLayoutPanel1
//
this.tableLayoutPanel1.ColumnCount = 4;
this.tableLayoutPanel1.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle());
this.tableLayoutPanel1.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle());
this.tableLayoutPanel1.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle());
this.tableLayoutPanel1.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle());
this.tableLayoutPanel1.Controls.Add(this.dgvData, 0, 1);
this.tableLayoutPanel1.Controls.Add(this.ckbSelectAll, 0, 0);
this.tableLayoutPanel1.Controls.Add(this.btnDeleteSelections, 2, 0);
this.tableLayoutPanel1.Controls.Add(this.lblVal, 3, 0);
this.tableLayoutPanel1.Controls.Add(this.btnReverse, 1, 0);
this.tableLayoutPanel1.Dock = System.Windows.Forms.DockStyle.Fill;
this.tableLayoutPanel1.Location = new System.Drawing.Point(0, 0);
this.tableLayoutPanel1.Name = "tableLayoutPanel1";
this.tableLayoutPanel1.RowCount = 2;
this.tableLayoutPanel1.RowStyles.Add(new System.Windows.Forms.RowStyle());
this.tableLayoutPanel1.RowStyles.Add(new System.Windows.Forms.RowStyle());
this.tableLayoutPanel1.Size = new System.Drawing.Size(485, 259);
this.tableLayoutPanel1.TabIndex = 0;
//
// dgvData
//
this.dgvData.AllowUserToAddRows = false;
this.dgvData.AllowUserToDeleteRows = false;
this.dgvData.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize;
this.dgvData.Columns.AddRange(new System.Windows.Forms.DataGridViewColumn[] {
this.blSelect});
this.tableLayoutPanel1.SetColumnSpan(this.dgvData, 4);
this.dgvData.Dock = System.Windows.Forms.DockStyle.Fill;
this.dgvData.Location = new System.Drawing.Point(3, 32);
this.dgvData.Name = "dgvData";
this.dgvData.RowTemplate.Height = 24;
this.dgvData.Size = new System.Drawing.Size(479, 232);
this.dgvData.TabIndex = 0;
this.dgvData.CellClick += new System.Windows.Forms.DataGridViewCellEventHandler(this.dgvData_CellClick);
//
// ckbSelectAll
//
this.ckbSelectAll.AutoSize = true;
this.ckbSelectAll.Dock = System.Windows.Forms.DockStyle.Fill;
this.ckbSelectAll.Location = new System.Drawing.Point(3, 3);
this.ckbSelectAll.Name = "ckbSelectAll";
this.ckbSelectAll.Size = new System.Drawing.Size(48, 23);
this.ckbSelectAll.TabIndex = 1;
this.ckbSelectAll.Text = "全选";
this.ckbSelectAll.UseVisualStyleBackColor = true;
this.ckbSelectAll.CheckedChanged += new System.EventHandler(this.ckbSelectAll_CheckStateChanged);
this.ckbSelectAll.CheckStateChanged += new System.EventHandler(this.ckbSelectAll_CheckStateChanged);
//
// btnDeleteSelections
//
this.btnDeleteSelections.Dock = System.Windows.Forms.DockStyle.Fill;
this.btnDeleteSelections.Location = new System.Drawing.Point(138, 3);
this.btnDeleteSelections.Name = "btnDeleteSelections";
this.btnDeleteSelections.Size = new System.Drawing.Size(75, 23);
this.btnDeleteSelections.TabIndex = 3;
this.btnDeleteSelections.Text = "删除选中行";
this.btnDeleteSelections.UseVisualStyleBackColor = true;
this.btnDeleteSelections.Click += new System.EventHandler(this.btnDeleteSelections_Click);
//
// lblVal
//
this.lblVal.AutoSize = true;
this.lblVal.Dock = System.Windows.Forms.DockStyle.Fill;
this.lblVal.Location = new System.Drawing.Point(219, 0);
this.lblVal.Name = "lblVal";
this.lblVal.Size = new System.Drawing.Size(263, 29);
this.lblVal.TabIndex = 4;
this.lblVal.Text = "0";
this.lblVal.TextAlign = System.Drawing.ContentAlignment.MiddleLeft;
//
// btnReverse
//
this.btnReverse.Location = new System.Drawing.Point(57, 3);
this.btnReverse.Name = "btnReverse";
this.btnReverse.Size = new System.Drawing.Size(75, 23);
this.btnReverse.TabIndex = 5;
this.btnReverse.Text = "反选";
this.btnReverse.UseVisualStyleBackColor = true;
this.btnReverse.Click += new System.EventHandler(this.btnReverse_Click);
//
// blSelect
//
this.blSelect.HeaderText = "选取";
this.blSelect.Name = "blSelect";
this.blSelect.ReadOnly = true;
//
// ucDataGirdView
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.Controls.Add(this.tableLayoutPanel1);
this.Name = "ucDataGirdView";
this.Size = new System.Drawing.Size(485, 259);
this.tableLayoutPanel1.ResumeLayout(false);
this.tableLayoutPanel1.PerformLayout();
((System.ComponentModel.ISupportInitialize)(this.dgvData)).EndInit();
this.ResumeLayout(false);
}
#endregion
private System.Windows.Forms.TableLayoutPanel tableLayoutPanel1;
private System.Windows.Forms.CheckBox ckbSelectAll;
public System.Windows.Forms.DataGridView dgvData;
private System.Windows.Forms.Button btnDeleteSelections;
private System.Windows.Forms.Label lblVal;
private System.Windows.Forms.Button btnReverse;
private System.Windows.Forms.DataGridViewCheckBoxColumn blSelect;
}
}
3.後台cs代碼
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace GridTest
{
public partial class ucDataGirdView : UserControl
{
public ucDataGirdView()
{
InitializeComponent();
}
#region 字段定义
///
/// 计算和值的列索引
///
private int qtyCellIndex = -1;
///
/// 计算和值的列索引
///
public int QtyCellIndex
{
get { return qtyCellIndex > dgvData.Columns.Count - 1 || qtyCellIndex <= 0 ? -1 : qtyCellIndex; }
set { qtyCellIndex = value > dgvData.Columns.Count - 1 || value <= 0 ? -1 : value; }
}
///
/// DataGridView的DataSource:DataTable
///
private DataTable dtSource = new DataTable();
///
/// DataGridView的DataSource:DataTable
///
public DataTable DataSource
{
get { dtSource = dgvData.DataSource as DataTable; return dtSource; }
set { dtSource = value; dgvData.DataSource = dtSource; }
}
///
/// 列值求和
///
private double sum = 0;
///
/// 列值求和
///
public double SumQty
{
get { sum = Convert.ToDouble(lblVal.Text); return sum; }
private set { sum = value; lblVal.Text = sum.ToString(); }
}
///
/// 显示和值标签
///
private bool showSumLabel = true;
///
/// 显示和值标签
///
public bool ShowSumLabel
{
get { showSumLabel = lblVal.Visible; return showSumLabel; }
set { showSumLabel = value; lblVal.Visible = showSumLabel; }
}
#endregion
#region 响应事件的方法实现
///
/// "全选/全不选/部分选中"的选取状态改变引发事件的方法
///
///
///
private void ckbSelectAll_CheckStateChanged(object sender, EventArgs e)
{
if (ckbSelectAll.CheckState == CheckState.Indeterminate)
{
return;
}
dgvData.Rows.Cast
CalVal();
}
///
/// 选择某列后,变更状态及计算值
///
///
///
public virtual void dgvData_CellClick(object sender, DataGridViewCellEventArgs e)
{
dynamic dgv = sender as DataGridView;
if (e.ColumnIndex == 0 && e.RowIndex >= 0)
{
DataGridViewRow dgvr = dgv.Rows[e.RowIndex];
dgvr.Cells[0].Value = !dgvr.RowChecked();
CalVal();
}
}
///
/// 反选
///
///
///
private void btnReverse_Click(object sender, EventArgs e)
{
dgvData.Rows.Cast
CalVal();
}
///
/// 删除选中的项目行
///
///
///
private void btnDeleteSelections_Click(object sender, EventArgs e)
{
dgvData.Rows.Cast
CalVal();
}
#endregion
#region 自定义一般方法
/// <summary>
/// 求某列和值
/// </summary>
public virtual void CalVal()
{
SumQty = dgvData.Rows.Cast<DataGridViewRow>().Where(r => r.RowChecked()).Sum(r => Convert.ToDouble(r.Cells[qtyCellIndex].Value));
UpdateStatus();
}
/// <summary>
/// 设定CheckBox的三种状态:全选、全不选、部分选择
/// </summary>
private void UpdateStatus()
{
int ChkCount = dgvData.Rows.Cast<DataGridViewRow>().Where(r => r.RowChecked()).ToList().Count;
if (ChkCount == 0)
{
ckbSelectAll.CheckState = CheckState.Unchecked;
}
else if (ChkCount == dgvData.Rows.Count)
{
ckbSelectAll.CheckState = CheckState.Checked;
}
else
{
ckbSelectAll.CheckState = CheckState.Indeterminate;
}
}
#endregion
}
}
`
4.Test頁面
--Designer頁面---
namespace GridTest
{
partial class Form18
{
///
/// Required designer variable.
///
private System.ComponentModel.IContainer components = null;
/// <summary>
/// Clean up any resources being used.
/// </summary>
/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.ucDataGirdView1 = new GridTest.ucDataGirdView();
this.btnBind = new System.Windows.Forms.Button();
this.SuspendLayout();
//
// ucDataGirdView1
//
this.ucDataGirdView1.DataSource = null;
this.ucDataGirdView1.Location = new System.Drawing.Point(25, 12);
this.ucDataGirdView1.Name = "ucDataGirdView1";
this.ucDataGirdView1.QtyCellIndex = -1;
this.ucDataGirdView1.ShowSumLabel = true;
this.ucDataGirdView1.Size = new System.Drawing.Size(485, 259);
this.ucDataGirdView1.TabIndex = 0;
//
// btnBind
//
this.btnBind.Location = new System.Drawing.Point(534, 57);
this.btnBind.Name = "btnBind";
this.btnBind.Size = new System.Drawing.Size(75, 23);
this.btnBind.TabIndex = 1;
this.btnBind.Text = "绑定数据";
this.btnBind.UseVisualStyleBackColor = true;
this.btnBind.Click += new System.EventHandler(this.button1_Click);
//
// Form18
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(646, 399);
this.Controls.Add(this.btnBind);
this.Controls.Add(this.ucDataGirdView1);
this.Name = "Form18";
this.Text = "Form18";
this.ResumeLayout(false);
}
#endregion
private ucDataGirdView ucDataGirdView1;
private System.Windows.Forms.Button btnBind;
}
}
-----cs頁面--------
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace GridTest
{
public partial class Form18 : Form
{
public Form18()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
DataTable dtSource = new DataTable();
dtSource.Columns.Add("Col2", typeof(string));
dtSource.Columns.Add("Col3", typeof(string));
dtSource.Columns.Add("Col4", typeof(int));
dtSource.Rows.Add("Row1_Col2", "Row1_Col3", 2);
dtSource.Rows.Add("Row2_Col2", "Row2_Col3", 2);
dtSource.Rows.Add("Row3_Col2", "Row3_Col3", 3);
dtSource.Rows.Add("Row4_Col2", "Row4_Col3", 6);
dtSource.Rows.Add("Row5_Col2", "Row5_Col3", 8);
ucDataGirdView1.DataSource = dtSource;
ucDataGirdView1.QtyCellIndex = 3;
}
}
}
【推荐】博客园的心动:当一群程序员决定开源共建一个真诚相亲平台
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】Flutter适配HarmonyOS 5知识地图,实战解析+高频避坑指南
【推荐】开源 Linux 服务器运维管理面板 1Panel V2 版本正式发布
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步