gpt4 book ai didi

c# - WinForms - DataGridViewCell 未设置为只读

转载 作者:可可西里 更新时间:2023-11-01 09:11:31 27 4
gpt4 key购买 nike

我正在处理一个旧的 .Net 2.0 WinForms 项目,需要将一些单元格设置为只读。

我有一个正在读取和设置为数据源的数据表,并且正确设置了字段类型

生成数据表和列

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;
}

上面的方法在这里被调用,这就是我遇到禁用单元格问题的地方。

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;
}
}
}
}

}

当逐步浏览并查看监 window 口时,我可以看到正在设置只读属性,但是当开始使用 DataGridView 时,单元格仍然处于事件状态。

如果有人能指出此代码错误的方向或者我是否需要做其他事情,我将不胜感激?

感谢您的帮助。

--- 编辑 2017 年 5 月 31 日

enter image description here

上图显示了我要使用的网格,默认情况下选中的选项。

未选中的选项将被禁用,因为邮件类型无法使用这些投递方式

最佳答案

我在一个小示例项目中检查过这个,这对我有用:

// Loop through all the rows of your grid
foreach (DataGridViewRow row in this.dgvMailPreferences.Rows)
{
// Loop through all the cells of the row
foreach (DataGridViewCell cell in row.Cells)
{
// Check if the cell type is CheckBoxCell
// If not, check the next cell
if (!(cell is DataGridViewCheckBoxCell)) continue;

// Set the specific cell to read only, if the cell is not checked
cell.ReadOnly = !Convert.ToBoolean(cell.Value);
}
}

此外,您可以添加一个事件来跟踪单元格更改以激活对单击单元格的只读:

private void dgvMailPreferences_CellValueChanged(object sender, DataGridViewCellEventArgs e)
{
if (!(e.RowIndex >= 0 && e.ColumnIndex >= 0)) return;

DataGridViewCell cell = ((DataGridView)sender).Rows[e.RowIndex].Cells[e.ColumnIndex];

if (!(cell is DataGridViewCheckBoxCell)) return;

cell.ReadOnly = !System.Convert.ToBoolean(cell.Value);
}

(在完成更改并保留单元格后触发。)

您可以使用 Ivan Stoev 中的 fiddle 构建示例项目在 Dotnetfiddle

using System;
using System.Collections.Generic;
using System.Data;
using System.Windows.Forms;

namespace Samples
{
static class Program
{
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
var form = new Form();
var dgv = new DataGridView { Dock = DockStyle.Fill, Parent = form };
form.Load += (sender, e) =>
{
var dt = GetData();
dgv.DataSource = dt;
dgv.Columns["Mail Preference Description"].Width = 250;
dgv.Columns["Post"].Width = 50;
dgv.Columns["SMS"].Width = 50;
dgv.Columns["Email"].Width = 50;
dgv.Columns["Telephone"].Width = 75;
dgv.Columns["SEQ_ID"].Visible = false;
dgv.Columns["MAIL_PREFERENCE_ID"].Visible = false;
foreach (DataGridViewRow row in dgv.Rows)
{
foreach (DataGridViewCell cell in row.Cells)
{
if (cell.Value is bool && (bool)cell.Value == false)
cell.ReadOnly = true;
}
}
};
Application.Run(form);
}

static DataTable GetData()
{
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

dt.Rows.Add(1, "1", "Membership", true, true, true, true);
dt.Rows.Add(2, "2", "Monthly Newsletter", false, false, true, false);
dt.Rows.Add(3, "3", "Mothhly Technical Briefing", false, false, true, false);
dt.Rows.Add(4, "4", "Magazine", false, false, true, false);
dt.Rows.Add(5, "5", "Branch Mailings", false, true, true, false);
dt.Rows.Add(6, "6", "Events", true, true, true, true);
dt.Rows.Add(7, "7", "Qualifications", true, true, true, true);
dt.Rows.Add(8, "8", "Training", true, true, true, true);
dt.Rows.Add(9, "9", "Recruitment", true, true, true, true);
dt.Rows.Add(10, "A", "General", true, true, true, true);

return dt;
}
}
}

关于c# - WinForms - DataGridViewCell 未设置为只读,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43975562/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com