Click here to Skip to main content
15,949,741 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi Guys


I would like to know on how am I able to make the button on my datagridview enable/disable. In my datagridview, it has 2 columns with 2 combobox and 2 columns that contains buttons. The 2 combobox inside the gridview and the third column button first at form load is enabled and the fourth column button is disabled. After the user selected items from the two combobox, the third column button will save it to the database and will make the two combobox disabled and the fourth column button will be enabled. the third button will also be disbled. if the user press the fourth button, it will enabled the three controls inside the datagridview. I do not know on how to make this. But what I have is on how to display them inside the datagridview. below is my code:

private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e) //Insert button inside datagridview
        {
            

            if (e.ColumnIndex == 3)
            {

             
                var senderGrid = (DataGridView)sender;
            

                if (senderGrid.Columns[e.ColumnIndex] is DataGridViewButtonColumn &&
                    e.RowIndex >= 0)
                {
                    //TODO - Button Clicked - Execute Code Here
                    //string x = dataGridView1.Rows[e.RowIndex].Cells[4].Value.ToString();
                    conn = new SqlConnection(connec.GetServer());
                    conn.Open();
                        cmd = new SqlCommand("Insert into tblSubj (Subject, TimeIN, TimeOUT,GradeLevel) Values('" + dataGridView1.Rows[e.RowIndex].Cells[0].Value + "','" + dataGridView1.Rows[e.RowIndex].Cells[1].Value + "','" + dataGridView1.Rows[e.RowIndex].Cells[2].Value + "','"+getinfo.rowid+"')", conn);
                        m_dr = cmd.ExecuteReader();
                        m_dr.Close();
                        conn.Close();
                        MessageBox.Show("Results Saved!");
                    

                }
            }
            else if (e.ColumnIndex == 4)
            {
            var senderGrid = (DataGridView)sender;

                if (senderGrid.Columns[e.ColumnIndex] is DataGridViewButtonColumn &&
                    e.RowIndex >= 0)
                {
                    //TODO - Button Clicked - Execute Code Here
                    //string x = dataGridView1.Rows[e.RowIndex].Cells[4].Value.ToString();
                    conn = new SqlConnection(connec.GetServer());
                    conn.Open();
                    cmd = new SqlCommand("update  tblSubj set Subject='" + dataGridView1.Rows[e.RowIndex].Cells[0].Value + "' ,TimeIN='" +dataGridView1.Rows[e.RowIndex].Cells[1].Value + "' ,TimeOUT='"+ dataGridView1.Rows[e.RowIndex].Cells[2].Value + "' Where  GradeLevel='" + getinfo.rowid + "'", conn);
                    m_dr = cmd.ExecuteReader();

                    conn = new SqlConnection(connec.GetServer());
                    conn.Open();
                    SqlCommand cmd1 = new SqlCommand("update  tblTime set Temp='" + "Taken" + "' Where  GradeLevel='" + getinfo.rowid + "' And TimeIN = '" + dataGridView1.Rows[e.RowIndex].Cells[1].Value + "' And TimeOUT = '" + dataGridView1.Rows[e.RowIndex].Cells[2].Value + "'", conn);
                    SqlDataReader m_dr1 = cmd1.ExecuteReader();
                    conn.Close();
                    MessageBox.Show("Updated");


              }
            
            
            
            
            
            }}
Posted

1 solution

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900