我想实现datagridview的每行前显示checkbox,值为数据表"Y"/"N"值,列名checked。搜了很多网站没有详细的,大侠们帮帮忙。我的程序不是web的。

解决方案 »

  1.   

    datagridview里面的列属性可以设置,然后将它和数据表中的列绑定。
      

  2.   

    foreach (DataGridViewRow dr in this.dataGridView1.Rows)   
    {   
      DataGridViewCheckBoxCell cbx = (DataGridViewCheckBoxCell)dr.Cells[0];   
      if (!(bool)cbx.FormattedValue)   
      {   
        
      }   
    }   
    遍历
    dgr.Cells["a"].Value 
      

  3.   

                        <asp:TemplateField HeaderText="checked">
                            <ItemTemplate>
                                <asp:CheckBox ID="c1" runat="server" Text='<%#Eval("列名") %>'/>
                            </ItemTemplate>
                        </asp:TemplateField>
      

  4.   

    datagridview最前面加一个DataGridViewCheckBoxColumn类型的列,绑定字段checked
    sql语句就写
    select case checked when 'Y' then 'true' when 'N' then 'false' end from table
      

  5.   

    select case checked when 'Y' then 'true' when 'N' then 'false' end chekced from table
    忘了加别名
      

  6.   

     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;
                    }
                }
            }
      

  7.   

    checkCell.Value 的值可以设置成 Y 或 N ,选择完之后写个方法,将值为 Y 的行都删选出来就可以了
      

  8.   

    是这样吗,结果是一个编辑框,不是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;
       }
    }
      

  9.   

    问4楼:怎么绑定列,我的数据库是mysql,根本加不了数据源啊
      

  10.   

    先设置好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";
      

  11.   

    先设置好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";
      

  12.   

    补充一下,把Column1的类型设置成DataGridViewCheckBoxColumn后,再设置一下Column1里的FalseValue和TrueValue的值。
      

  13.   

    解决了,谢谢大家了。
                   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;
                 }