我的GridView中的列是动态生成的,因为要绑定的表不唯一
每次绑定的时候都要GridView.Columns.Clear();
然后绑定。
我需要在数据列的前面加一列复选框.cs文件中用CheckBoxField
但是只能把列标题加上,生成的GridView里面并没有复选框!!!按钮可以ButtonField bf = new ButtonField(); bf.ButtonType = ButtonType.Button;(这样按钮就加上了)
可是CheckBoxField没有像ButtonType这样的属性。不知道该怎么动态加上复选框列。
每次绑定的时候都要GridView.Columns.Clear();
然后绑定。
我需要在数据列的前面加一列复选框.cs文件中用CheckBoxField
但是只能把列标题加上,生成的GridView里面并没有复选框!!!按钮可以ButtonField bf = new ButtonField(); bf.ButtonType = ButtonType.Button;(这样按钮就加上了)
可是CheckBoxField没有像ButtonType这样的属性。不知道该怎么动态加上复选框列。
解决方案 »
- 关于C# ActiveX的开发客户端打印EXCEL功能
- 这段代码为什么不执行
- 请问怎样锁定我的电脑和开始菜单,不能点开,当然要有办法能让我自己打开..
- 大家看下这个压缩文件,有高手帮忙看看那能解开不?
- DataGridView中的Cell中可否加入自定义控件,并且实现拖动效果
- winform 开机启动 如何在启动程序时快速运行?
- ImageList使用问题。
- 请问怎样在c#里设置或改变屏幕背景
- 有没有什么便利的方法在程序运行中根据新设定的背景图片自动改变panel或窗体的大小?
- 如何判断键盘鼠标是否与电脑相连
- ScriptManager (ajax) 带来的打开新页的烦恼
- datagridview问题~~~!2
Dc.Name="test";
Dc.HeaderText="测试";
this.dataGridView1.Columns.Add(Dc);
<head id="Head1" runat="server">
<title>Gridview------Check Box</title>
<script language="javascript" type="text/javascript">
//先获取所有的Checkbox
var chkList = document.getElementsByName("CheckBox1");
window.onload = function()
{
//为所有checkbox添加onclick事件处理,以自动更新“已选择的项”
for(var i=0; i<chkList.length; i++)
{
chkList[i].onclick = chkClick;
}
}
//checkbox的onclick事件,用于更新“已选择的项”
function chkClick(){
var checkedList = "";
//获取所有被选中的项
for(var i=0; i<chkList.length; i++){
if(chkList[i].checked)
checkedList += chkList[i].value + ",";
}
//把选中项的列表显示到“已选择的项”中,substring在这里是为了去除最后一个逗号
document.getElementById("HiddenField1").value = checkedList.substring(0,checkedList.length-1);
}
function checkAll()
{
var chkall=document.getElementById("CheckBoxAll");
if(chkall.checked)
{
var checkedList = "";
for(var i=0;i<chkList.length;i++)
{
chkList[i].checked=true;
checkedList += chkList[i].value + ",";
}
document.getElementById("HiddenField1").value = checkedList.substring(0,checkedList.length-1);
}
else
{
for(var i=0;i<chkList.length;i++)
chkList[i].checked=false;
document.getElementById("HiddenField1").value="";
}
}
</script>
<link href="~/CSS/Gridview.css" rel="stylesheet" type="text/css" />
</head>
<body>
<form id="form1" runat="server">
<asp:HiddenField ID="HiddenField1" runat="server" />
<div id="container">
<asp:GridView ID="GridView1" BorderColor="Black" OnRowDataBound="GridView1_RowDataBound" AllowPaging="True" runat="server" AutoGenerateColumns="False" Font-Size="12px" Width="549px" OnPageIndexChanging="GridView1_PageIndexChanging">
<Columns>
<asp:TemplateField HeaderText="编号">
<ItemTemplate>
<input name="CheckBox1" type="checkbox" value="<%#Eval("ID") %>"/>
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="EmpID" HeaderText="账号" />
<asp:BoundField DataField="EmpRealName" HeaderText="姓名" />
<asp:BoundField DataField="EmpSex" HeaderText="性别" />
<asp:BoundField DataField="EmpAddress" HeaderText="住址" />
<asp:BoundField DataField="EmpZipCode" HeaderText="邮编" />
<asp:BoundField DataField="EmpBirthday" HeaderText="生日" DataFormatString="{0:yyyy-MM-dd}" HtmlEncode="False" />
<asp:BoundField DataField="EmpSalary" HeaderText="薪水" DataFormatString="{0:c}" HtmlEncode="False" />
</Columns>
<HeaderStyle BackColor="Azure" Font-Size="12px" HorizontalAlign="Center" />
<RowStyle HorizontalAlign="Center" />
<PagerStyle HorizontalAlign="Center" />
</asp:GridView>
<br />
全选:<input id="CheckBoxAll" type="checkbox" onclick="checkAll()" />
<asp:Button ID="Button1" runat="server" Height="20px" Text="删 除" OnClick="Button1_Click" Width="59px" />
</div>
</form>
</body>
</html>
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
bind();
}
}
/// <summary>
/// 绑定数据
/// </summary>
public void bind()
{
string sqlStr = "select * from Employee";
DataSet myds = Common.dataSet(sqlStr);
GridView1.DataSource = myds;
GridView1.DataKeyNames = new string[] { "ID" };
GridView1.DataBind();
}
/// <summary>
/// 在 GridView 控件中的某个行被绑定到一个数据记录时发生。此事件通常用于在某个行被绑定到数据时修改该行的内容。
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{ //遍历所有行设置边框样式
foreach (TableCell tc in e.Row.Cells)
{
tc.Attributes["style"] = "border-color:Black";
}
}
/// <summary>
/// 在单击页导航按钮时发生,但在 GridView 控件执行分页操作之前。此事件通常用于取消分页操作。
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
GridView1.PageIndex = e.NewPageIndex;
bind();
}
/// <summary>
/// 删除所选记录
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void Button1_Click(object sender, EventArgs e)
{
string type = HiddenField1.Value;
string [] keyValue=type.Split(',');
foreach(string keyName in keyValue)
{
Common.ExecuteSql("delete from Employee where ID=" + keyName + "");
}
HiddenField1.Value = "";
bind();
}
}
刚开始就没有CheckBox那列,我要在绑定数据时,把那列自己加上!!!我问的是怎么加!!!
<ItemTemplate>
<input id="CheckboxGroup" type="checkbox" value='<%#Eval("TransitJobID")%>' name="CheckboxGroup" />
</ItemTemplate>
</asp:TemplateField>
我的是网站式的,没有那个叫DataGridViewCheckBoxColumn 的
{
ListItem li = new ListItem();
li.DataField = dc.ColumnName;
li.HearderText = (dc.ColumnName的中文名);
this.GridView2.Columns.Add(li);
}this.GridView2.DataSource = dt.DefaultView;
this.GridView2.DataBind();
在列表前面有"复选框"那一列,我怎么加上?????
hlc.HeaderText = "修改";
hlc.Text = "修改";
hlc.DataNavigateUrlFormatString = "IntContent.aspx?cid={0}";
hlc.DataNavigateUrlField = "ConID";
DataGrid1.Columns.Add(hlc);
换成CheckBoxColumn 就可以了
好的,我试试CheckBoxColumns我之前用的:
CheckBoxField cbf = new CheckBoxField();
……
不能显示复选出框。
文章出处:http://www.diybl.com/course/4_webprogram/asp.net/asp_netshl/2008430/112156.html