如何在datagridview中启用/禁用按钮

问题描述:

嗨大家好





我想知道如何在我的datagridview启用/禁用按钮。在我的datagridview中,它有2列,包含2个组合框,2列包含按钮。 Gridview中的2个组合框和表单加载时的第三个列按钮被启用,第四个列按钮被禁用。用户从两个组合框中选择项目后,第三列按钮将其保存到数据库,并将禁用两个组合框,并启用第四列按钮。第三个按钮也将被删除。如果用户按下第四个按钮,它将启用datagridview内的三个控件。我不知道如何做到这一点。但我所拥有的是如何在datagridview中显示它们。下面是我的代码:



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");


              }
            
            
            
            
            
            }}

试试这些链接 -

如何:禁用按钮列中的按钮 [ ^ ]

http://www.dotnetspider.com/forum/227412-How-disable-button-datagridview.aspx [ ^ ]
Try these links -
How to: Disable Buttons in a Button Column [^]
http://www.dotnetspider.com/forum/227412-How-disable-button-datagridview.aspx[^]