简体   繁体   中英

how to get access to the checkbox in datagridview c#

I have a grid having two columns "name"(string) and "status"(check box),I want to save all grid data into database at once but i want string status = "P" if check box is checked other wise status = "A" for each row. How it is Possible. It an attendence project.

private void button1_Click(object sender, EventArgs e)
    {

        for (int i = 0; i < dataGridView1.Rows.Count; i++)
        {

            string name = dataGridView1.Rows[i].Cells[0].Value.ToString();
            string status = "";

            mscon.Open();
            mscmd = new OleDbCommand("insert into attendence (dat,nam,status)values(#" + dateTimePicker1.Text + "#,'" + name + "','" + status + "') ", mscon);
            mscmd.ExecuteNonQuery();
            mscmd.Dispose();
            mscon.Close();


        }

        MessageBox.Show("saved");


    }

This is what I do when I evaluate checkbox per row:

foreach (DataGridViewRow row in dg.Rows)
        {
            if (Convert.ToBoolean(row["checkColumnName"].ToString())
            {
                // do checked thing
            }
            else
            {
                // do unchecked thing
            }
        }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM