以下是个DataGirdView
RoleName   Flag     checkbox
系统管理员    1      (复选框) 
财务          0      (复选框)请问:如果我要根据Flag列来判断复选框是否打勾,要怎么实现?
checkbox是动态生成的:
DataGridViewCheckBoxColumn Column = new DataGridViewCheckBoxColumn();
            this.dgvwRole.Columns.Add(Column)

解决方案 »

  1.   

    今天刚有人问过
    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 + "行,选中"); 
            } 
        } 
    }
      

  2.   


    请问:如果我要根据Flag列来判断复选框是否打勾,要怎么实现? 
      

  3.   

    foreach (DataGridViewRow row in dataGridView1.Rows) 
        {
           //根据你的情况选择其中的一种  
            if (row.Cells[1].Value.Equals(1))
            { 
               row.Cells[2].Value = true;
            } 
           
           if (row.Cells[1].Value.Equals(0))
            { 
               row.Cells[2].Value = true;
            }
        } 
      

  4.   

    DataGirdView绑定了一个表的.. Checkbox列是我自己加上去的。。
    我要根据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 列...
      

  5.   

      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;
                    }            }
    }
    仅供参考!
      

  6.   


    报错啊。。if (row.Cells[1].Value.Equals(1)) 
    未将对象引用设置到对象的实例。
      

  7.   


    不知道这样可以不???        
    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;
                }
            }
      

  8.   

    楼上的报错是因为还有一行是新增行,是Null的····
      

  9.   

    我这个的话要将DataGridView的ALLOWUSERTOADDROWS修改为FALSE
    或者判断下是不是空哦!
      

  10.   

    楼上的报错是因为还有一行是新增行,是Null的····??
    没有新增行啊。 就2行。。 不过第3行没有数据,但它的第3列有个Checkbox,难道是这个问题?
    谢谢大家这么热心帮助我哦,但还没解决问题。。
      

  11.   

    晕,,你的ALLOWUSERTOADDROWS默认是ture,也就是默认会有一行新增行的,那行就是Null,所以Guyschaos说的就是这个道理,如果这个为FALSE就不需要判断,如果为true就要判断一下··
      

  12.   

    将DataGridView的ALLOWUSERTOADDROWS修改为FALSE,这样就不会有第三行了!
    你是不是就想要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;
                    }            }        }
      

  13.   

    因为如果值为NULL的话再ToString()是会出错的!
    大家其实都已经帮你解决问题了!关键你自己都不调试的!
      

  14.   

    还是不行哦。private void HOHO_Load(object sender, EventArgs e)
            {
                          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了。。
      

  15.   

    晕···有些地方是要自己修改一下的···
    iRows.Cells[1]比如这里的1,,在你那里不一定是1啦····代码不能套,知道方法就好了!
      

  16.   

    没有解决哇,
    RoleName  Flag    checkbox 
    系统管理员    1      (复选框) 
    财务          0      (复选框) 系统管理员 的checkBox 应该打勾了才对啊。。您的代码没。。
      

  17.   

    iRows.Cells[2].Value = iRows.Cells[1].Value.ToString() == "1" ?true :false;
    //iRows.Cells[2]为你的checkbox的那一列,用名字也行
    //iRows.Cells[1]为你作为判断的那以列,比如你要用Flag这个列,你也可以改为iRows.Cells["Flag"](还是再提醒一下Flag为列名)···
    这样应该可以了吧!!!
      

  18.   

    if (iRows.Cells[1].Value != null)
                    iRows.Cells[2].Value = iRows.Cells[1].Value.ToString() == "1" ?true :false;
    这什么意思啊?
    不明白。 Cells[X]我改了啊。
      

  19.   

    这个是我最初写的测试用的
    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不要写在按钮事件里,不然会出现很多列!
    就这么多了!我的代码虽然不怎么好,但我自己用的时候完全可以!
      

  20.   

    如果这行的第二列不为null
      就:这行的第三列的Value 等于(如果这行的第二列的Value的字符串值 等于 1 就:返回True;否则就:返回False)
    第一次用中文编程,,哈哈!!!
    我改用易语言算了··哈哈!!!
      

  21.   

    在DataGridView的CellFormatting事件里设置复选框的状态:private void dataGridView1_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
            {
                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;
                    }
                }
            }