Home > Blockchain >  How to make specific rows editable for which checkbox is marked and other rows should be read only i
How to make specific rows editable for which checkbox is marked and other rows should be read only i

Time:06-07

foreach (DataGridViewRow dgvrow in dataGridView1.SelectedRows)
{
    if (Convert.ToBoolean(dgvrow.Cells["chkbox"].Value) == true)
    {

        LOB = dgvrow.Cells["lOBDataGridViewTextBoxColumn"].Value.ToString();
        POL_NUM = dgvrow.Cells["polNumDataGridViewTextBoxColumn"].Value.ToString();
        POL_Effdate = Convert.ToDateTime(dgvrow.Cells["polEffDtDataGridViewTextBoxColumn"].Value);
        POL_Expdate = Convert.ToDateTime(dgvrow.Cells["polExpDtDataGridViewTextBoxColumn"].Value);
        tbl_pol_coll_agmt_dtls_id = Convert.ToInt32(dgvrow.Cells["TBL_POL_COLL_AGMT_DTLS_ID"].Value);


        if (MessageBox.Show("Would like to update the click yes!!",
                        "Input Policy", MessageBoxButtons.YesNo) ==
                        System.Windows.Forms.DialogResult.Yes)
        {
            collServcall.UpdateInputPolicyData(tbl_pol_coll_agmt_dtls_id, LOB, POL_NUM, POL_Effdate, POL_Expdate);
        }
    }        
}

Here I have mentioned whether the checkbox value is true or not but need to make only those cells editable and rest rows or cells should be readonly

CodePudding user response:

How to make specific rows editable

You should handle CellBeginEdit event of your data grid view and cancel the edit if a condition for not editing is met. I will give a demo code which you can change and adapt into your solution.

Take a look at following example. In this example rows with even index number are not allowed to be edited.

private void DataGridView1_CellBeginEdit(object sender, 
    DataGridViewCellCancelEventArgs e)
{
    if (e.RowIndex % 2 == 0)
    {
        e.Cancel = true;
    }
}

So, you should do the same.

How to make specific rows editable for which checkbox is marked and other rows should be read only

Assuming if you have a data grid view check box column named checkBoxColumn then you should do something like below. (Demo code)

private void DataGridView1_CellBeginEdit(
    object sender, DataGridViewCellCancelEventArgs e)
{
    // For this demo we will make exception for checkBoxColumn itself.
    // So it is always editable.
    if (e.ColumnIndex != checkBoxColumn.Index)
    {
        object rawValue = dataGridView1[checkBoxColumn.Index, e.RowIndex].Value;
        bool value = rawValue is bool ? (bool)rawValue : false;
        // But if checkBoxColumn is checked then editing not allowed
        if (!value)
        {
            e.Cancel = true;
        }
    }
}
  • Related