datagridview 怎么在头一列加个全选?c#
发布网友
发布时间:2022-05-16 17:33
我来回答
共4个回答
热心网友
时间:2023-08-15 08:34
实现方式: 给DataGirdView动态添加CheckBox列
代码:
StringBuilder sql = new StringBuilder();
sql.Append("select * from aa");
DataSet ds = db.GetDataSet(sql.ToString());
if(ds==null)
{
dataGridView3.DataSource = "";
return;
}
//添加列
DataGridViewCheckBoxColumn newcol = new DataGridViewCheckBoxColumn();
newcol.HeaderText = "选择";
dataGridView3.Columns.Insert(0, newcol);
//添加完列再绑定数据源
dataGridView3.DataSource = ds.Tables[0];
触发checkbox的CheckedChanged事件
/// <summary>
/// 全选
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void checkBox1_CheckedChanged(object sender, EventArgs e)
{
if (checkBox1.Checked)
for (int i = 0; i < dataGridView3.Rows.Count; i++)
dataGridView3.Rows[i].Cells[0].Value = true;
else
for (int i = 0; i < dataGridView3.Rows.Count; i++)
dataGridView3.Rows[i].Cells[0].Value = false;
}
//添加其他类型的列(注意显示文字的设置)
第一: newedit.DefaultCellStyle.NullValue = "修改";
第二:newedit .UseColumnTextForButtonValue=true;
newedit .Text="修改";
示例:
dataGridView3.DataSource = ds.Tables[0];
DataGridViewButtonColumn newedit = new DataGridViewButtonColumn();
newedit.HeaderText = "编辑";
newedit.DefaultCellStyle.NullValue = "修改"; //列的显示文字
dataGridView3.Columns.Insert(9, newedit);//列的位置
热心网友
时间:2023-08-15 08:35
//允许datagridview一次选择多行,这个写load的事件里dgv.MultiSelect = true; 拖一个checkbox,将下面代码写在其CheckedChange事件中:if (ckbAll.Checked == true)
{
for (int i = 0; i < dgvMain.Rows.Count; i++)
{
for (int j = 0; j < dgvMain.Columns.Count; j++)
{
dgvMain[j, i].Selected = true;
}
}
}
else
{
dgvMain.ClearSelection();
}
热心网友
时间:2023-08-15 08:35
<asp:GridView ID="GridView1" runat="server">
<Columns>
<asp:TemplateField>
<HeaderTemplate> <asp:CheckBox runat="server" ID="cbxall" Text="全选" /></HeaderTemplate>
<ItemTemplate>
<asp:CheckBox runat="server" ID="cbxsingle" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView> 用模板列emplateField 如上 不用BoundField
热心网友
时间:2023-08-15 08:36
恐怕这个功能不能直接完成,建议你到CSDN里看看一个重写DataGridView的完整示例代码:给你一个做好的内容:using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
namespace DataGridViewEx
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
/// <summary>
/// 加载数据
/// </summary>
private void btnLoadData_Click(object sender, EventArgs e)
{
DataTable dtTemp = new DataTable();
using (SqlConnection con = new SqlConnection("server=.;uid=sa;pwd=sa;database=testdb;"))
{
SqlDataAdapter sa = new SqlDataAdapter("select * from userinfo", con);
sa.Fill(dtTemp);
}
this.dataGridView1.DataSource = dtTemp;
}
/// <summary>
/// 在checkbox列显示列头checkbox
/// </summary>
/// <param name="sender"> </param>
/// <param name="e"> </param>
private void button1_Click(object sender, EventArgs e)
{
datagridviewCheckboxHeaderCell ch = new datagridviewCheckboxHeaderCell();
ch.OnCheckBoxClicked += new datagridviewcheckboxHeaderEventHander(ch_OnCheckBoxClicked);//关联单击事件
//第三列为DataGridViewCheckBoxColumn
DataGridViewCheckBoxColumn checkboxCol = this.dataGridView1.Columns[2] as DataGridViewCheckBoxColumn;
checkboxCol.HeaderCell = ch;
checkboxCol.HeaderCell.Value = string.Empty;//消除列头checkbox旁出现的文字
}
/// <summary>
/// 单击事件
/// </summary>
private void ch_OnCheckBoxClicked(object sender, datagridviewCheckboxHeaderEventArgs e)
{
foreach (DataGridViewRow dgvRow in this.dataGridView1.Rows)
{
if (e.CheckedState)
{
dgvRow.Cells[2].Value = true;
}
else
{
dgvRow.Cells[2].Value = false;
}
}
}
}
//定义触发单击事件的委托
public delegate void datagridviewcheckboxHeaderEventHander(object sender, datagridviewCheckboxHeaderEventArgs e);
//定义包含列头checkbox选择状态的参数类
class datagridviewCheckboxHeaderEventArgs : EventArgs
{
private bool checkedState = false;
public bool CheckedState
{
get { return checkedState; }
set { checkedState = value; }
}
}
//定义继承于DataGridViewColumnHeaderCell的类,用于绘制checkbox,定义checkbox鼠标单击事件
class datagridviewCheckboxHeaderCell : DataGridViewColumnHeaderCell
{
Point checkBoxLocation;
Size checkBoxSize;
bool _checked = false;
Point _cellLocation = new Point();
System.Windows.Forms.VisualStyles.CheckBoxState _cbState =
System.Windows.Forms.VisualStyles.CheckBoxState.UncheckedNormal;
public event datagridviewcheckboxHeaderEventHander OnCheckBoxClicked;
//绘制列头checkbox
protected override void Paint(System.Drawing.Graphics graphics,
System.Drawing.Rectangle clipBounds,
System.Drawing.Rectangle cellBounds,
int rowIndex,
DataGridViewElementStates dataGridViewElementState,
object value,
object formattedValue,
string errorText,
DataGridViewCellStyle cellStyle,
DataGridViewAdvancedBorderStyle advancedBorderStyle,
DataGridViewPaintParts paintParts)
{
base.Paint(graphics, clipBounds, cellBounds, rowIndex,
dataGridViewElementState, value,
formattedValue, errorText, cellStyle,
advancedBorderStyle, paintParts);
Point p = new Point();
Size s = CheckBoxRenderer.GetGlyphSize(graphics,
System.Windows.Forms.VisualStyles.CheckBoxState.UncheckedNormal);
p.X = cellBounds.Location.X +
(cellBounds.Width / 2) - (s.Width / 2) - 1;//列头checkbox的X坐标
p.Y = cellBounds.Location.Y +
(cellBounds.Height / 2) - (s.Height / 2);//列头checkbox的Y坐标
_cellLocation = cellBounds.Location;
checkBoxLocation = p;
checkBoxSize = s;
if (_checked)
_cbState = System.Windows.Forms.VisualStyles.
CheckBoxState.CheckedNormal;
else
_cbState = System.Windows.Forms.VisualStyles.
CheckBoxState.UncheckedNormal;
CheckBoxRenderer.DrawCheckBox
(graphics, checkBoxLocation, _cbState);
}
/// <summary>
/// 点击列头checkbox单击事件
/// </summary>
protected override void OnMouseClick(DataGridViewCellMouseEventArgs e)
{
Point p = new Point(e.X + _cellLocation.X, e.Y + _cellLocation.Y);
if (p.X >= checkBoxLocation.X && p.X <=
checkBoxLocation.X + checkBoxSize.Width
&& p.Y >= checkBoxLocation.Y && p.Y <=
checkBoxLocation.Y + checkBoxSize.Height)
{
_checked = !_checked;
//获取列头checkbox的选择状态
datagridviewCheckboxHeaderEventArgs ex = new datagridviewCheckboxHeaderEventArgs();
ex.CheckedState = _checked;
object sender = new object();//此处不代表选择的列头checkbox,只是作为参数传递。应该列头checkbox是绘制出来的,无法获得它的实例
if (OnCheckBoxClicked != null)
{
OnCheckBoxClicked(sender, ex);//触发单击事件
this.DataGridView.InvalidateCell(this);
}
}
base.OnMouseClick(e);
}
}
}