在checkbox.check上从数据库向datagridview添加一行

时间:2012-03-02 12:54:50

标签: c# ado.net datagridview checkbox

我希望选中复选框在datagridview 中添加一行。如果我选中另一个复选框,则应添加另一行。如果我取消选中任何复选框,则应从数据网格中删除相关行,将已检查的1保留在datagrid中。请帮助我通过插入自己的列和行尝试这个,但我想从数据库中的行。请帮助并提前感谢您,问候。 我有一个像这样的代码:

SqlConnection con7 = new SqlConnection("Data Source = .;Initial Catalog = SAHS;integrated security = true");
            con7.Open();
            SqlCommand cmd3 = new SqlCommand("select Status from vw_stdtfeedetail where Std=" + comboBox2.SelectedItem + " and Div ='" + comboBox3.SelectedItem + "' and name='" + comboBox4.SelectedItem + "' and mnthname ='August'", con7);
            SqlDataReader dr = cmd3.ExecuteReader();
            BindingSource bs = new BindingSource();
            bs.DataSource = dr;
            dataGridView3.DataSource = bs;
            this.monthFeeTableAdapter.Fill(this.sAHSDataSet4.MonthFee);
            int testrow = dataGridView3.Rows.Count;
            for (int i = 0; i < testrow; i++)
            {
                if (dataGridView3.Rows[i].Cells[0].Value == DBNull.Value)
                {
                    SqlConnection con4 = new SqlConnection("Data Source = .;Initial Catalog = SAHS;integrated security = true");
                    con4.Open();
                    SqlCommand cmd4 = new SqlCommand("Select mnthname , Description, Amount from monthfee where mnthname='August'", con4);
                    SqlDataReader dr1 = cmd4.ExecuteReader();
                    BindingSource bs1 = new BindingSource();
                    bs1.DataSource = dr1;
                    dataGridView1.DataSource = bs1;
                    this.monthFeeTableAdapter.Fill(this.sAHSDataSet4.MonthFee);
                    con4.Close();
                    break;
                    /*SqlDataReader dr1 = cmd4.ExecuteReader();
                    BindingSource bs3 = new BindingSource();
                    bs3.DataSource = dr1;
                    dataGridView1.DataSource = bs3;
                    this.monthFeeTableAdapter.Fill(this.sAHSDataSet4.MonthFee);*/
                }

1 个答案:

答案 0 :(得分:0)

为复选框设置AutoPostback = True。然后在复选框的已检查更改事件中,使用新的where子句重新加载gridview。