datagridview中实现checkbox

qingbt 2010-12-17 11:25:06
我想实现datagridview的每行前显示checkbox,值为数据表"Y"/"N"值,列名checked。搜了很多网站没有详细的,大侠们帮帮忙。我的程序不是web的。
...全文
298 15 打赏 收藏 转发到动态 举报
写回复
用AI写文章
15 条回复
切换为时间正序
请发表友善的回复…
发表回复
mr_china 2010-12-17
  • 打赏
  • 举报
回复
checkCell.Value 的值可以设置成 Y 或 N ,选择完之后写个方法,将值为 Y 的行都删选出来就可以了
mr_china 2010-12-17
  • 打赏
  • 举报
回复
 private void DataGridView1_CellMouseClick(object sender, DataGridViewCellMouseEventArgs e)
{
if ((e.RowIndex >= 0)&&(e.ColumnIndex==0))//表格第一列
{
DataGridViewCheckBoxCell checkCell = (DataGridViewCheckBoxCell)DataGridView1.Rows[e.RowIndex].Cells["jd_check"];
Boolean flag = Convert.ToBoolean(checkCell.Value);//判断是否已选
if (flag)
{
checkCell.Value = false;
}
else
{
checkCell.Value = true;
}
}
}
红帽小小 2010-12-17
  • 打赏
  • 举报
回复
select case checked when 'Y' then 'true' when 'N' then 'false' end chekced from table

忘了加别名
luo19841124 2010-12-17
  • 打赏
  • 举报
回复
相当不错的哦
红帽小小 2010-12-17
  • 打赏
  • 举报
回复
datagridview最前面加一个DataGridViewCheckBoxColumn类型的列,绑定字段checked
sql语句就写
select case checked when 'Y' then 'true' when 'N' then 'false' end from table
tercelstar 2010-12-17
  • 打赏
  • 举报
回复
<asp:TemplateField HeaderText="checked">
<ItemTemplate>
<asp:CheckBox ID="c1" runat="server" Text='<%#Eval("列名") %>'/>
</ItemTemplate>
</asp:TemplateField>
wuyq11 2010-12-17
  • 打赏
  • 举报
回复
foreach (DataGridViewRow dr in this.dataGridView1.Rows)
{
DataGridViewCheckBoxCell cbx = (DataGridViewCheckBoxCell)dr.Cells[0];
if (!(bool)cbx.FormattedValue)
{

}
}
遍历
dgr.Cells["a"].Value
Karual 2010-12-17
  • 打赏
  • 举报
回复
datagridview里面的列属性可以设置,然后将它和数据表中的列绑定。
qingbt 2010-12-17
  • 打赏
  • 举报
回复
解决了,谢谢大家了。
DataGridViewCheckBoxColumn newColumn = new DataGridViewCheckBoxColumn();
newColumn.HeaderText = "选择";
dataGridView1.Columns.Insert(1, newColumn);
for (int i = 0; i < dataGridView1.Rows.Count; i++) {
if (dataGridView1.Rows[i].Cells["checked"].Value.Equals("Y"))
dataGridView1.Rows[i].Cells[1].Value = true;
else
dataGridView1.Rows[i].Cells[1].Value = false;
}
power6d 2010-12-17
  • 打赏
  • 举报
回复
补充一下,把Column1的类型设置成DataGridViewCheckBoxColumn后,再设置一下Column1里的FalseValue和TrueValue的值。
power6d 2010-12-17
  • 打赏
  • 举报
回复
先设置好DataGridView控件,比如把Column1的类型设置成CheckBox


DataSet SqlDS = new DataSet();
SqlDataAdapter SqlDA = new SqlDataAdapter("Select Null As [Check],字段1,字段2 From 表名", 连接句柄);
SqlDA.Fill(SqlDS);

DataView tblcomponents = SqlDS.Tables[0].DefaultView;

dataGridView1.AutoGenerateColumns = false;
dataGridView1.DataSource = tblcomponents;

Column1.DataPropertyName = "Check";
Column2.DataPropertyName = "字段1";
Column3.DataPropertyName = "字段2";
power6d 2010-12-17
  • 打赏
  • 举报
回复
先设置好DataGridView控件,比如把Column1的类型设置成CheckBox


DataSet SqlDS = new DataSet();
SqlDataAdapter SqlDA = new SqlDataAdapter("Select Null As [Check],字段1,字段2 From 表名", 连接句柄);
SqlDA.Fill(SqlDS);

DataView tblcomponents SqlDS.Tables[0].DefaultView;

dataGridView1.AutoGenerateColumns = false;
dataGridView1.DataSource = tblcomponents;

Column1.DataPropertyName = "Check";
Column2.DataPropertyName = "字段1";
Column3.DataPropertyName = "字段2";
qingbt 2010-12-17
  • 打赏
  • 举报
回复
别沉啊,在线等呢,不行加分啦
qingbt 2010-12-17
  • 打赏
  • 举报
回复
问4楼:怎么绑定列,我的数据库是mysql,根本加不了数据源啊
qingbt 2010-12-17
  • 打赏
  • 举报
回复
是这样吗,结果是一个编辑框,不是checkbox啊:
DataTable table=DBUtil.GetInstance().Query("Select id,checked from test").Tables[0];
if(table!=null){
dataGridView1.DataSource = table;
for (int i = 0; i < dataGridView1.Rows.Count;i++ ) {
DataGridViewCheckBoxCell checkCell = (DataGridViewCheckBoxCell)
dataGridView1.Rows[i].Cells["checked"];
if ((char)dataGridView1.Rows[i].Cells["checked"].Value == 'Y')
checkCell.Value = true;
else
checkCell.Value = false;
}
}

110,535

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

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