C# 中的DataGridView控件如何设置成具有复选框功能?

C# 中的DataGridView控件如何设置成具有复选框功能?,第1张

runat="server" name="chids" />

Font-Underline="False" HorizontalAlign="Center" />

DataFormatString='{0:d}'>

--%>

ImageAlign="AbsMiddle">


//全选
function selectAll(SourceID, ControlID) {
var sourceBox = document.getElementById(SourceID)
var theBox = document.all(ControlID)

elm = theBox.getElementsByTagName('Input')
for (i = 0i < elm.lengthi++) {
if (elm[i].type == "checkbox") {
elm[i].checked = sourceBox.checked
}
}
}
//选择删除检测
function CheckNULL(ControlID) {
var isNULL = false
var theBox = document.all(ControlID)
elm = theBox.getElementsByTagName('Input')

for (var i = 0i < elm.lengthi++) {
var e = elm[i]
if (e.type == "checkbox" && e.checked == true && e.id != "chkAllInBox") {
isNULL = true
}
}
if (isNULL == false) {
alert('请选择删除项!')
return false
}
else {
return confirm('确定要删除信息?')
}
}

GridView1

<HeaderTemplate>

<input id="chkAll1" runat="server" type="checkbox" name="chkAll1" onclick="CheckAll(this,'GridView1')" />全选

</HeaderTemplate>

<ItemTemplate>

<asp:CheckBox runat="server" ID="cbx1" />

</ItemTemplate>

function CheckAll(oCheckbox,strGvlist)

{

var GridView1 = document.getElementById(strGvlist)

for(i = 0i <GridView1.rows.lengthi++)

{

GridView1.rows[i].cells[0].getElementsByTagName("INPUT")[0].checked = oCheckbox.checked

}

}

winform datagridview表头怎么增加复选框,点击表头的复选框,下面行的复选全选或全未选

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 WindowsFormsApplication1

{

publicpartialclass Form1 : Form

{

private DataGridView DataGridView1 =new DataGridView()

private CheckBox CheckBox1 =new CheckBox()

public Form1()

{

InitializeComponent()

}

privatevoid Form1_Load(object sender, EventArgs e)

{

CheckBox1.CheckedChanged += CheckBox1_CheckedChanged

DataGridView1.CellPainting += DataGridView1_CellPainting

this.DataGridView1.AllowUserToResizeRows =false

this.DataGridView1.AllowUserToResizeColumns =false

this.DataGridView1.Dock = DockStyle.Fill

this.DataGridView1.Columns.Add(new DataGridViewCheckBoxColumn())

this.DataGridView1.Columns.Add("Column2", "Column2")

for (int i =1i <=3i++)

{

this.DataGridView1.Rows.Add(0, "Row"+ i.ToString() +" Column2")

}

this.CheckBox1.Visible =false

this.CheckBox1.Text ="CheckBox"

this.Controls.Add(DataGridView1)

this.Controls.Add(CheckBox1)

}

privatevoid CheckBox1_CheckedChanged(object send, System.EventArgs e)

{

for (int i =0i <=this.DataGridView1.RowCount -1i++)

{

this.DataGridView1.Rows.SharedRow(i).SetValues(CheckBox1.Checked)

}

}

privatevoid DataGridView1_CellPainting(object sender, System.Windows.Forms.DataGridViewCellPaintingEventArgs e)

{

if (e.RowIndex ==-1&e.ColumnIndex ==0)

{

Point p =this.DataGridView1.GetCellDisplayRectangle(e.ColumnIndex, e.RowIndex, true).Location

p.Offset(this.DataGridView1.Left, this.DataGridView1.Top)

this.CheckBox1.Location = p

this.CheckBox1.Size =this.DataGridView1.GetCellDisplayRectangle(e.ColumnIndex, e.RowIndex, false).Size

this.CheckBox1.Visible =true

this.CheckBox1.BringToFront()

}

}

}

}


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/bake/11821397.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-18
下一篇 2023-05-18

发表评论

登录后才能评论

评论列表(0条)

保存