我在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;
}
} 这样子也不行,帮帮忙吧!
{
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;
}
} 这样子也不行,帮帮忙吧!
newColumn.TrueValue =1;
newColumn.FalseValue =0;
newColumn.IndeterminateValue =0;
{
string a = bs_cust_info.Rows[i].Cells["readperIod"].Value.ToString();
CheckBox _CheckBox =(CheckBox)bs_cust_info.Rows[i].FindControl("CheckBox1");
if (a.Equals("1"))
{
_CheckBox.Checked = true;
}
else
{
_CheckBox.Checked = false;
} }
bs_cust_info.Rows[i].Cells["readperIod"].Text 不是Value
对象都能取到
是DataGridViewCheckBoxCell类型的对象啊
newColumn.TrueValue =1;
newColumn.FalseValue =0;
newColumn.IndeterminateValue =0;
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();
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));
}
}
没必要非要使用微软提供的checkboxColumn吧。
完全可以自己将checkbox嵌入在datagridview里面呀。
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;
}
}/*不知道行不行,我做过....忘记了....好像是这个样*/
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;
}
}
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;
}
}
{
if (e.ColumnIndex > 1 && e.ColumnIndex < 5)
{
bool t = (bool)dataGridView1[e.ColumnIndex, e.RowIndex].Value;
t = !t;
dataGridView1[e.ColumnIndex, e.RowIndex].Value = t;
dataGridView1.CurrentCell = dataGridView1[0, e.RowIndex];
dataGridView1.CurrentCell = dataGridView1[e.ColumnIndex, e.RowIndex];
}
}
参考
for (int i = 0; i < dataGridView1.Rows.Count-1; i++)
{
if (dataGridView1.Rows[i].Cells["readperIod"].Value.ToString() == "1")
{
dataGridView1.Rows[i].Cells["readperIodCheckBox"].Value = true;
}
else
{
dataGridView1.Rows[i].Cells["readperIodCheckBox"].Value = false;
}
}/*我试过了 我的是可以的.........*/
但是状态还是未选中状态!
非常的郁闷
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;
}
}
}我试了可这样可以
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;
}
}