datagridview 列头上加checkbox

qinmi 2008-05-22 02:04:38
请问如何在datagridview的列头上设置一个checkbox。

请指教!谢谢!
...全文
1558 12 打赏 收藏 转发到动态 举报
写回复
用AI写文章
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
坐看昀起 2010-02-25
  • 打赏
  • 举报
回复
只能通过重写datagridview的列头来实现了。查了下,可参考。 http://www.cnblogs.com/gossip/archive/2008/11/22/1338868.html

灰常有用!
  • 打赏
  • 举报
回复
        #region 检测功能测试--得到指定病人的过敏史

/// <summary>
/// 得到指定病人的过敏史
/// </summary>
/// <param name="patientAllergen"></param>
/// <param name="tbName_PatientAllergen"></param>
/// <returns></returns>
public DataSet GetPatientAllergenByPatientId(cPatientAllergen patientAllergen,string tbName_PatientAllergen)
{
SqlParameter[] prams = {
data.MakeInParam("@patientid",SqlDbType.Int,4,patientAllergen.PatientId),
};

return (data.RunProcReturn("SELECT CASE C_inp_no WHEN 1 THEN CAST('1' as bit) ELSE CAST('0' as bit) END AS C_inp_no "
+",c_patient_id,c_allergen_code,c_allergen_name,c_drug_source"
+ ",c_allergen_symptom FROM his_allergen_info WHERE c_patient_id = @patientid ORDER BY c_patient_id",
prams, tbName_PatientAllergen));
}
#endregion
syszero 2008-12-05
  • 打赏
  • 举报
回复
给你一个做好的内容: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);
}

}



}
qinmiqinmi 2008-06-30
  • 打赏
  • 举报
回复
楼主的问题太深奥了,给分吧
HimeTale 2008-06-01
  • 打赏
  • 举报
回复
要么换别的控件,要么自己写控件
推荐动态生成checkbox,放在行的前头
gyc 2008-05-31
  • 打赏
  • 举报
回复
Combox ? 还是方法?

如果你要在系统上那个显示,恐怕你需要自定义控件了
如果不是必须的, 自己添加一个CheckBox列, 然后把DataGridView 的样式设置一下, 把行头设成不显示
Jash_Qi 2008-05-30
  • 打赏
  • 举报
回复
提醒楼上的兄弟一句。DATAGRIDVIEW和DATAGRID都分不清的人就先别发言了。

有个办法可以实现..


Public Class HeaderComboxView
Inherits DataGridView
...

end class

继承DATAGRIDVIEW.手写一个可以动态添加COMBOX的方法。.

生成控件...

关键是你这需求太恶心了。
只能用这种缺德的办法
lfywy 2008-05-22
  • 打赏
  • 举报
回复

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if(e.Row.RowType == DataControlRowType.Header)
{
for(int i = 1;i<Zm_list.Count+1;i++)
{
e.Row.Cells[i].Text="<a herf='#'style='cursor:hand;' onclick='linkyl("+Zm_list[i-1]+")'>"+ZmName_list[i-1]+"</a>";//此处你可以添加你要的checkbox
}
}
}
//还有种方法,代码如下:
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" Width="100%" AllowPaging="True" OnPageIndexChanging="GridView1_PageIndexChanging" CssClass="Table_Bgcolor" BorderStyle="None" BorderWidth="0px" CellPadding="1" CellSpacing="1" OnRowDataBound="GridView1_RowDataBound">
<FooterStyle HorizontalAlign="Right" />
<Columns>
<asp:TemplateField HeaderText="序号">
<ItemStyle HorizontalAlign="Center" Width="5%" />
<HeaderStyle CssClass="Title_Font" />
</asp:TemplateField>
<asp:TemplateField>
<HeaderTemplate>
<asp:CheckBox ID="CheckBox2" runat="server" AutoPostBack="True" OnCheckedChanged="CheckBox2_CheckedChanged" />
</HeaderTemplate>
<ItemStyle HorizontalAlign="Center" Width="5%" />
<HeaderStyle CssClass="Title_Font" />
<ItemTemplate>
<asp:CheckBox ID="CheckBox1" runat="server" OnCheckedChanged="CheckBox1_CheckedChanged" />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="编辑">
<ItemStyle HorizontalAlign="Center" Width="5%" />
<HeaderStyle CssClass="Title_Font" HorizontalAlign="Center" />
<ItemTemplate>
<asp:ImageButton ID="ImgBtnEdit" runat="server" SkinID="Edit" ImageUrl="~/Images/edit.gif" ToolTip="编辑用户信息" OnClick="ImgBtnEdit_Click" />
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="USERNAME" HeaderText="姓名">
<ItemStyle HorizontalAlign="Center" Width="10%" />
<HeaderStyle CssClass="Title_Font" />
</asp:BoundField>
<asp:BoundField DataField="USERLOGINNAME" HeaderText="登录帐号 ">
<ItemStyle HorizontalAlign="Center" Width="10%" />
<HeaderStyle CssClass="Title_Font" />
</asp:BoundField>
<asp:BoundField DataField="COMID" HeaderText="单位">
<ItemStyle HorizontalAlign="Left" Width="10%" />
<HeaderStyle CssClass="Title_Font" />
</asp:BoundField>
<asp:BoundField DataField="TEL" HeaderText="联系电话">
<ItemStyle HorizontalAlign="Center" Width="15%" />
<HeaderStyle CssClass="Title_Font" />
</asp:BoundField>
<asp:BoundField DataField="EMAIL" HeaderText="电子邮件">
<ItemStyle HorizontalAlign="Center" Width="15%" />
<HeaderStyle CssClass="Title_Font" />
</asp:BoundField>
<asp:BoundField DataField="USERID" HeaderText="系统角色">
<ItemStyle HorizontalAlign="Center" Width="15%" />
<HeaderStyle CssClass="Title_Font" />
</asp:BoundField>
<asp:BoundField DataField="ISUSING" HeaderText="启用停用">
<ItemStyle HorizontalAlign="Center" Width="10%" />
<HeaderStyle CssClass="Title_Font" />
</asp:BoundField>
<asp:TemplateField>
<HeaderTemplate>
<asp:ImageButton ID="ImageButton1" runat="server" OnClick="ImageButton1_Click"
SkinID="Delete" ImageUrl="~/Images/delete.gif" ToolTip="点击删除所选项用户信息" OnClientClick="return confirm('确认要删除本页选中的吗?');" />
</HeaderTemplate>
<ItemStyle HorizontalAlign="Center" Width="10%" />
<HeaderStyle CssClass="Title_Font" HorizontalAlign="Center" />
<ItemTemplate>
<asp:ImageButton ID="ImageButton2" runat="server" OnClick="ImageButton1_Click1" SkinID="Delete" ImageUrl="~/Images/delete.gif" ToolTip="点击删除该项用户信息" OnClientClick="return confirm('确认要删除该项吗?');" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
<RowStyle CssClass="Table_Tr_Bgcolor" />
<PagerStyle CssClass="Table_Tr_Bgcolor" HorizontalAlign="Right" />
<PagerSettings Visible="False" />
</asp:GridView>

希望对你有帮助或启示作用!这个问题启示很简单!
hxb74 2008-05-22
  • 打赏
  • 举报
回复
想放个CHECKBOX做什么用。
allanli 2008-05-22
  • 打赏
  • 举报
回复
直接放一个CheckBox控件到列头上就可以了,用代码控制他的位置和表格数据的过滤关系
这个是最简单的办法(也许也是唯一办法了),想修改DataGridView支持这个功能,很难
qinmi 2008-05-22
  • 打赏
  • 举报
回复
难道真的没有高手吗。郁闷!
蛋蛋の忧伤 2008-05-22
  • 打赏
  • 举报
回复
兄弟,放弃吧,我这个问题都问了3礼拜了
没有人告诉

16,721

社区成员

发帖
与我相关
我的任务
社区描述
VB技术相关讨论,主要为经典vb,即VB6.0
社区管理员
  • VB.NET
  • 水哥阿乐
  • 无·法
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧