I am working on an old .Net 2.0 WinForms project and need to set some cells to read only.
I have a DataTable that I am reading and setting as the DataSource and the field types are being set correctly
Generate DataTable and columns
public DataTable FilterData(DataTable datatable, string dataType)
{
try
{
if (dataType == "MailPreferences")
{
var dt = new DataTable();
dt.Columns.Add("SEQ_ID", typeof(int)); // SEQ_ID
dt.Columns.Add("MAIL_PREFERENCE_ID", typeof(string)); // MAIL_PREFERENCE_ID
dt.Columns.Add("Mail Preference Description", typeof(string)); // MAIL_PREFERENCE_DESC
dt.Columns.Add("Post", typeof(bool)); // POST
dt.Columns.Add("SMS", typeof(bool)); // SMS
dt.Columns.Add("Email", typeof(bool)); // EMAIL
dt.Columns.Add("Telephone", typeof(bool)); // TELEPHONE
foreach (DataRow row in datatable.Rows)
{
dt.Rows.Add(row["SEQ_ID"].ToString(),
row["MAIL_PREFERENCE_ID"].ToString(),
row["MAIL_PREFERENCE_DESC"].ToString(),
Convert.ToBoolean(row["POST"]),
Convert.ToBoolean(row["SMS"]),
Convert.ToBoolean(row["EMAIL"]),
Convert.ToBoolean(row["TELEPHONE"]));
}
return dt;
}
}
catch (Exception ex)
{
// catch and deal with my exception here
}
return null;
}
The above method is being called here and this is where I am having the issue of disabling cells.
private void PopulateMailPreferencesGV()
{
var dt = FilterData(_cAddPersonWizard.GetMailPreferneces(), "MailPreferences");
dgvMailPreferences.DataSource = dt;
dgvMailPreferences.Columns["Mail Preference Description"].Width = 250;
dgvMailPreferences.Columns["Post"].Width = 50;
dgvMailPreferences.Columns["SMS"].Width = 50;
dgvMailPreferences.Columns["Email"].Width = 50;
dgvMailPreferences.Columns["Telephone"].Width = 75;
dgvMailPreferences.Columns["SEQ_ID"].Visible = false;
dgvMailPreferences.Columns["MAIL_PREFERENCE_ID"].Visible = false;
// not setting the datagridview cell to readonly
foreach (DataGridViewRow row in dgvMailPreferences.Rows)
{
foreach (DataGridViewCell cell in row.Cells)
{
if (cell.GetType() == typeof(DataGridViewCheckBoxCell))
{
if(((DataGridViewCheckBoxCell)row.Cells[cell.ColumnIndex]).Selected == false)
{
((DataGridViewCheckBoxCell)row.Cells[cell.ColumnIndex]).ReadOnly = true;
}
}
}
}
}
When stepping through and looking at the Watch window I can see that the read only properties are being set, however when coming to work with the DataGridView the cells are still active.
I would be grateful if someone could point me in the direction of where this code is wrong or if I need to do something else?
Thanks for your help.
--- Edit 31/05/2017
The image above shows the grid that I want to work with, the options that are selected are selected by default.
The options that are not selected are to be disabled because these forms of delivery are not possible for the mail type
