以下是个DataGirdView
RoleName Flag checkbox
系统管理员 1 (复选框)
财务 0 (复选框)请问:如果我要根据Flag列来判断复选框是否打勾,要怎么实现?
checkbox是动态生成的:
DataGridViewCheckBoxColumn Column = new DataGridViewCheckBoxColumn();
this.dgvwRole.Columns.Add(Column)
RoleName Flag checkbox
系统管理员 1 (复选框)
财务 0 (复选框)请问:如果我要根据Flag列来判断复选框是否打勾,要怎么实现?
checkbox是动态生成的:
DataGridViewCheckBoxColumn Column = new DataGridViewCheckBoxColumn();
this.dgvwRole.Columns.Add(Column)
private void button1_Click(object sender, EventArgs e)
{
foreach (DataGridViewRow row in dataGridView1.Rows)
{
//这里0是你复选框所在列的索引
if (row.Cells[2].Value != null && row.Cells[2].Value.ToString() == "True")
{
MessageBox.Show(row.Index + "行,选中");
}
}
}
请问:如果我要根据Flag列来判断复选框是否打勾,要怎么实现?
{
//根据你的情况选择其中的一种
if (row.Cells[1].Value.Equals(1))
{
row.Cells[2].Value = true;
}
if (row.Cells[1].Value.Equals(0))
{
row.Cells[2].Value = true;
}
}
我要根据Flag 来判断绑定是通过这个查询语句的:select a.RoleCode as RoleCode,r.RoleName as RoleName,1 as Flag from AUserRole a,ARole r where a.Rolecode=r.Rolecode and usercode='" + code + "' union all select RoleCode,RoleName,0 as Flag from ARole where RoleCode not in (select RoleCode from AUserRole where usercode='" + code + "')";数据库并没有Flag 列...
{
DataTable dt = new DataTable();
dt.Columns.Add("RoleName");
dt.Columns.Add("flag");
DataRow dr = dt.NewRow();
dr.ItemArray = new string[] { "ss", "1" };
dt.Rows.Add(dr);
DataRow dr1 = dt.NewRow();
dr1.ItemArray = new string[] { "ss", "1" };
dt.Rows.Add(dr1);
DataRow dr2 = dt.NewRow();
dr2.ItemArray = new string[] { "ss", "0" };
dt.Rows.Add(dr2);
this.dataGridView1.DataSource = dt;
DataGridViewCheckBoxColumn ss = new DataGridViewCheckBoxColumn();
ss.Name = "checkbox";
this.dataGridView1.Columns.Add(ss);
} private void button1_Click(object sender, EventArgs e)
{
for(int i=0;i<this.dataGridView1.Rows.Count;i++)
{
if (this.dataGridView1["flag", i].Value.ToString()== "1")
{
this.dataGridView1["checkbox", i].Value = true;
} }
}
仅供参考!
报错啊。。if (row.Cells[1].Value.Equals(1))
未将对象引用设置到对象的实例。
不知道这样可以不???
private void button1_Click(object sender, EventArgs e)
{
DataGridViewCheckBoxColumn Column = new DataGridViewCheckBoxColumn();
this.dataGridView1.Columns.Add(Column); foreach (DataGridViewRow iRows in this.dataGridView1.Rows)
{
if(iRows.Cells[1].Value!=null)
iRows.Cells[2].Value = iRows.Cells[1].Value.ToString()=="1"?true:false;
}
}
或者判断下是不是空哦!
没有新增行啊。 就2行。。 不过第3行没有数据,但它的第3列有个Checkbox,难道是这个问题?
谢谢大家这么热心帮助我哦,但还没解决问题。。
你是不是就想要FLAG是1的在CHECKBOX中打勾呢?
我那个我都测试过了!
private void button1_Click(object sender, EventArgs e)
{
DataGridViewCheckBoxColumn ss = new DataGridViewCheckBoxColumn();
ss.Name = "checkbox";
this.dataGridView1.Columns.Add(ss);
for(int i=0;i<this.dataGridView1.Rows.Count;i++)
{
if (this.dataGridView1["flag", i].Value.ToString()== "1")
{
this.dataGridView1["checkbox", i].Value = true;
} } }
大家其实都已经帮你解决问题了!关键你自己都不调试的!
{
this.dgvwRole.AllowUserToAddRows = false;
DataGridViewCheckBoxColumn Column = new DataGridViewCheckBoxColumn();
this.dgvwRole.Columns.Add(Column);
foreach (DataGridViewRow iRows in this.dgvwRole.Rows)
{
if (iRows.Cells[1].Value != null)
iRows.Cells[2].Value = iRows.Cells[1].Value.ToString() == "1" ?true :false;
}}运行后,结果是:
RoleName Flag checkbox
系统管理员 0 (复选框)
财务 0 (复选框) Flag都变成0了。。
iRows.Cells[1]比如这里的1,,在你那里不一定是1啦····代码不能套,知道方法就好了!
RoleName Flag checkbox
系统管理员 1 (复选框)
财务 0 (复选框) 系统管理员 的checkBox 应该打勾了才对啊。。您的代码没。。
//iRows.Cells[2]为你的checkbox的那一列,用名字也行
//iRows.Cells[1]为你作为判断的那以列,比如你要用Flag这个列,你也可以改为iRows.Cells["Flag"](还是再提醒一下Flag为列名)···
这样应该可以了吧!!!
iRows.Cells[2].Value = iRows.Cells[1].Value.ToString() == "1" ?true :false;
这什么意思啊?
不明白。 Cells[X]我改了啊。
private void Form1_Load(object sender, EventArgs e)
{
DataTable dt = new DataTable();
dt.Columns.Add("RoleName");
dt.Columns.Add("flag");
DataRow dr = dt.NewRow();
dr.ItemArray = new string[] { "ss", "1" };
dt.Rows.Add(dr);
DataRow dr1 = dt.NewRow();
dr1.ItemArray = new string[] { "ss", "1" };
dt.Rows.Add(dr1);
DataRow dr2 = dt.NewRow();
dr2.ItemArray = new string[] { "ss", "0" };
dt.Rows.Add(dr2);
this.dataGridView1.DataSource = dt;
DataGridViewCheckBoxColumn ss = new DataGridViewCheckBoxColumn();
ss.Name = "checkbox";
this.dataGridView1.Columns.Add(ss);
} private void button1_Click(object sender, EventArgs e)
{
for(int i=0;i <this.dataGridView1.Rows.Count;i++)
{
if (this.dataGridView1["flag", i].Value.ToString()== "1")
{
this.dataGridView1["checkbox", i].Value = true;
} }
}
动态生成的CHECKBOX不要写在按钮事件里,不然会出现很多列!
就这么多了!我的代码虽然不怎么好,但我自己用的时候完全可以!
就:这行的第三列的Value 等于(如果这行的第二列的Value的字符串值 等于 1 就:返回True;否则就:返回False)
第一次用中文编程,,哈哈!!!
我改用易语言算了··哈哈!!!
{
if (e.ColumnIndex == 2)
{
if (dataGridView1[1, e.RowIndex].Value!=null && dataGridView1[1, e.RowIndex].Value.ToString() == "1")
{
e.Value = 1;
}
else
{
e.Value = 0;
}
}
}