有个datagridview第一列是checkbox,但是没有能得到绑定数据,想在绑定数据后,再一行行判断是否应该选中,但是怎么选中??下面这方法试了,不行??求教高人???????
string sql = "SELECT * FROM 课程";
            SqlMethod.bindDataGridView(dataGridView1 ,sql,"课程",ConnectionCom .getConnection());
            int sum = dataGridView1.Rows.Count;
            StringCom.showInformation(sum.ToString());
            for (int i = 0; i < sum;i++ )
            {
               //string courceid= dataGridView1.Rows[i].Cells[1].Value.ToString();
               ////StringCom.showInformation(courceid);
               ////StringCom.showInformation(temp.ToString());
               //if (ConnectionCom.IsHaveSelectCource(stuNumber, Convert .ToInt32(courceid)))
               //{                   DataGridViewCheckBoxCell checkcell = (DataGridViewCheckBoxCell)dataGridView1.Rows[i].Cells[0];
                   checkcell.Value = true;
               //}

解决方案 »

  1.   

     for (int i = 0; i < this.dataGridView1.Rows.Count; i++)
                    {                   
                        if (this.dataGridView1.Rows[i].Cells[0].EditedFormattedValue.ToString().Trim() == "True")
                        {
                        }
                    }
    试试这个行吧!
      

  2.   

    选择列在第几列这里Cells[n] n就是数字几,注意下标是从零开始的!
      

  3.   

    上面的兄弟,主要是主怎么手动设置没有绑定数据的这个checkbox怎么让其打上钩?处于选中状态???????
      

  4.   

    EditedFormattedValue只是个只读属性,????
      

  5.   

    this.dataGridView1.EndEdit();
    试试
      

  6.   

    好了,还是我这方法不错,开始不是有一个没有数据的么??我生成个column放进去不就得了,后来如果选中了,看看数据中有没有,没有就插入,而没选中,也看看数据中有没有,有就删除!!!感谢大家支持!!!!!!!
    namespace 学籍管理系统
    {
        public partial class SelectCource : Form
        {
            private string stuNumber;
            public SelectCource(string stuNum)
            {
                InitializeComponent();
                stuNumber = stuNum;
                GP.Text = stuNum+"的选课信息";
                bindDataGridView();
            }
            protected void bindDataGridView()
            { 
                string sql = "SELECT * FROM 课程";
                DataTable dt = SqlMethod.ReturnDataTable(ConnectionCom .getConnection (),sql,"课程");
                DataColumn dc = new DataColumn("select",System.Type.GetType("System.Boolean"));
                dt.Columns.Add(dc);
                dc.SetOrdinal(1);
                int sum = dataGridView1.Rows.Count;
                //StringCom.showInformation(sum.ToString());
                for (int i = 0; i < dt.Rows.Count;i++ )
                {
                   string courceid= dt.Rows[i]["课程编号"] .ToString();
                   //StringCom.showInformation(courceid);
                   //StringCom.showInformation(temp.ToString());
                   if (ConnectionCom.IsHaveSelectCource(stuNumber, Convert.ToInt32(courceid)))
                   {
                       dt.Rows[i]["select"] = true;
                   }
                   else
                   {
                       dt.Rows[i]["select"] = false;
                   }
                }
                dataGridView1.DataSource = dt;
            }        private void SelectCource_Load(object sender, EventArgs e)
            {        }        private void button1_Click(object sender, EventArgs e)
            {
                try
                {
                    int sum = dataGridView1.Rows.Count;
                    for (int i = 0; i < sum; i++)
                    {
                        //StringCom.showInformation(dataGridView1.Rows[i].Cells[0].Value.ToString());
                        int courseid = Convert.ToInt32(dataGridView1.Rows[i].Cells[0].Value.ToString());
                        //StringCom.showInformation(this.dataGridView1.Rows[i].Cells[1].EditedFormattedValue.ToString().Trim());
                        if (this.dataGridView1.Rows[i].Cells[1].EditedFormattedValue.ToString().Trim() == "True")
                        {
                            if (!ConnectionCom.IsHaveSelectCource(stuNumber, courseid))
                            {
                                string sql = " insert into 选课(学号,课程编号) values(" + StringCom.QuoteStr(stuNumber) + "," + courseid + ")";
                                SqlMethod.InsertUpdateDeleteToDataBase(ConnectionCom.getConnection(), sql);
                            }
                        }
                        else
                        {
                            if (ConnectionCom.IsHaveSelectCource(stuNumber, courseid))
                            {
                                string sql = " delete from  选课 where 学号=" + StringCom.QuoteStr(stuNumber) + " and 课程编号=" + courseid;
                                SqlMethod.InsertUpdateDeleteToDataBase(ConnectionCom.getConnection(), sql);
                            }
                        }
                    }
                    StringCom.showInformation("保存成功!");
                    this.Dispose();
                }
                catch (Exception err)
                {
                    StringCom.showInformation(err.Message);
                }
            }
        }
    }