DataGridViewCheckBoxColumn的状态改变

ch5201314zt 2009-05-12 10:53:09
我在DataGridView中添加一个DataGridViewCheckBoxColumn列,

想用程序的方法改变datagridviewcheckboxcell的选中状态(打勾或取消打勾)。搞了好久,希望大家能帮忙。

     列表名是readperIodCheckBox

     for (int i = 0; i < bs_cust_info.Rows.Count-1; i++)
{
string a = bs_cust_info.Rows[i].Cells["readperIod"].Value.ToString();
if (a.Equals("1"))
{

        //在这里改变状态,但是不行
bs_cust_info.Rows[i].Cells["readperIodCheckBox"].Value = true;
}
else
{
bs_cust_info.Rows[i].Cells["readperIodCheckBox"].Value = false;
}

}



这样子也不行,帮帮忙吧!

...全文
2868 40 打赏 收藏 转发到动态 举报
写回复
用AI写文章
40 条回复
切换为时间正序
请发表友善的回复…
发表回复
  • 打赏
  • 举报
回复
我也遇到了和楼主同样问题,今天早上正常调试通过了,后来添加了些代码就不能正常初始化checkBox了:
private void SYS_WorkOrder_Load(object sender, EventArgs e)
{
   dataGridView1.DataSource = dt;
   dataGridView1.Columns["ID"].Visible = true;
   dataGridView1.Columns["ModifyFlag"].Visible = true;
   dataGridView1.Columns["EmployeeName"].HeaderText = "姓名";
   DataGridViewCheckBoxColumn dgvCheckBox = new DataGridViewCheckBoxColumn();
   dgvCheckBox.HeaderText = "选择";
   dgvCheckBox.HeaderCell.Style.Alignment=DataGridViewContentAlignment.MiddleCenter;
   dataGridView1.Columns.Insert(1, dgvCheckBox);

   for (int i = 0; i < dataGridView1.Rows.Count; i++)
   {
  dataGridView1.Rows[i].Cells[1].Value = true;
  string flag = dataGridView1.Rows[i].Cells["ModifyFlag"].Value.ToString();
  if (flag=="2")
  {
  dataGridView1.Rows[i].Cells[1].Value = true;
  dataGridView1.Rows[i].DefaultCellStyle.BackColor = Color.Red;
  }
  if(flag =="1")
 dataGridView1.Rows[i].Cells[1].Value = true;
   }
}
  • 打赏
  • 举报
回复
各位大哥,看准楼主的题目内容,是datagridview不是datagrid,是winform不是webform。自己没试过的话就不要乱讲。
fangbeijing 2011-03-28
  • 打赏
  • 举报
回复
解决没啊?说说方法呗、、、、、
大进 2009-12-29
  • 打赏
  • 举报
回复
//单击单元格发生的事件
private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
for (int i = 0; i < dataGridView1.Rows.Count - 1; i++)
{
if (dataGridView1.Rows[i].Cells["test"].Selected)
{
dataGridView1.Rows[i].Cells["test"].Value = true;
}
else
{
dataGridView1.Rows[i].Cells["test"].Value = false;
}
}
}

我试了可这样可以
k_wind 2009-09-27
  • 打赏
  • 举报
回复
把选中代码放在Form_Load里就可以变成选中了状态了..
andesen 2009-05-15
  • 打赏
  • 举报
回复
现在解决了吗?,我也遇到了同样的问题,值改变但状态不变
wxm3630478 2009-05-12
  • 打赏
  • 举报
回复

for (int i = 0; i < dataGridView1.Rows.Count; i++)
{
if (dataGridView1.Rows[i].Cells["Column1"].Value == null)
{
dataGridView1.Rows[i].Cells["Column1"].Value = true;
}
if ((bool)dataGridView1.Rows[i].Cells["Column1"].Value)
{
dataGridView1.Rows[i].Cells["Column1"].Value = false;
}
else
{
dataGridView1.Rows[i].Cells["Column1"].Value = true;
}
}
wxm3630478 2009-05-12
  • 打赏
  • 举报
回复

for (int i = 0; i < dataGridView1.Rows.Count; i++)
{
if (dataGridView1.Rows[i].Cells["Column1"].Value == null) //是==
{
continue;
}
if ((bool)dataGridView1.Rows[i].Cells["Column1"].Value)
{
dataGridView1.Rows[i].Cells["Column1"].Value = false;
}
else
{
dataGridView1.Rows[i].Cells["Column1"].Value = false;
}
}

wxm3630478 2009-05-12
  • 打赏
  • 举报
回复
楼主是要实现 全选和反选功能吗.........


for (int i = 0; i < dataGridView1.Rows.Count; i++)
{
if (dataGridView1.Rows[i].Cells["Column1"].Value != null)
{
continue;
}
if ((bool)dataGridView1.Rows[i].Cells["Column1"].Value)
{
dataGridView1.Rows[i].Cells["Column1"].Value = false;
}
else
{
dataGridView1.Rows[i].Cells["Column1"].Value = false;
}
}

/*不知道行不行,我做过....忘记了....好像是这个样*/
pricks 2009-05-12
  • 打赏
  • 举报
回复
上面的各位:
没必要非要使用微软提供的checkboxColumn吧。
完全可以自己将checkbox嵌入在datagridview里面呀。
wuqianghappy 2009-05-12
  • 打赏
  • 举报
回复
.net 72 般技巧!上面有很多实例,很实用找一下吧!
outou 2009-05-12
  • 打赏
  • 举报
回复
上面代码所表示的值,既是状态,设定值即为设定表示状态。
ch5201314zt 2009-05-12
  • 打赏
  • 举报
回复
是改变状态啊,不是改变值
outou 2009-05-12
  • 打赏
  • 举报
回复
试试下面的代码如何。来自http://bingning.net/VB/SOURCE/datagridview/datagridviewcheckboxcolumn.html

//CurrentCellDirtyStateChanged事件处理器
private void DataGridView1_CurrentCellDirtyStateChanged(
object sender, EventArgs e)
{
if (DataGridView1.CurrentCellAddress.X == 0 &&
DataGridView1.IsCurrentCellDirty)
{
// Commit
DataGridView1.CommitEdit(DataGridViewDataErrorContexts.Commit);
}
}

//CellValueChanged事件处理器
private void DataGridView1_CellValueChanged(
object sender, DataGridViewCellEventArgs e)
{
//检测是否是CheckBox列
if (e.ColumnIndex == 0 &&
DataGridView1.Columns[e.ColumnIndex].ValueType == typeof(bool))
{
MessageBox.Show(
string.Format("第{0}行CheckBox值改变为{1}。",
e.RowIndex,
DataGridView1[e.ColumnIndex, e.RowIndex].Value));
}
}

ch5201314zt 2009-05-12
  • 打赏
  • 举报
回复
这是什么啊?貌似是状态改变么?
wartim 2009-05-12
  • 打赏
  • 举报
回复
关键这个条件成立不成立啊



readperIodCheckBoxColumn.TrueValue =1;
readperIodCheckBoxColumn.FalseValue =0;
readperIodCheckBoxColumn.IndeterminateValue =0;

。。。

bs_cust_info.EndEdit();
foreach (DataGridViewRow DVGR in bs_cust_info.Rows)
DVGR.Cells["readperIodCheckBox"].Value = (bs_cust_info.Rows[i].Cells["readperIod"].Value.ToString()=="1" ? 1 : 0);
bs_cust_info.EndEdit();
surlew 2009-05-12
  • 打赏
  • 举报
回复
DataGridViewCheckBoxColumn newColumn = (DataGridViewCheckBoxColumn)bs_cust_info.Rows[i].Cells["readperIodCheckBox"];
newColumn.TrueValue =1;
newColumn.FalseValue =0;
newColumn.IndeterminateValue =0;
ch5201314zt 2009-05-12
  • 打赏
  • 举报
回复
直接绑定的一个checkBox列啊
对象都能取到
是DataGridViewCheckBoxCell类型的对象啊
ch5201314zt 2009-05-12
  • 打赏
  • 举报
回复
大侠们,帮帮偶吧!
完全不知道则么回事啊
hb9191 2009-05-12
  • 打赏
  • 举报
回复
你的 readperIod 这格中如没有控件,就应该
bs_cust_info.Rows[i].Cells["readperIod"].Text 不是Value
加载更多回复(19)

110,586

社区成员

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

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

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