DataGridViewCheckboxCell在加载数据后不会更新

本文关键字:更新 数据 加载 DataGridViewCheckboxCell | 更新日期: 2023-09-27 18:03:48

在尝试了一些解决方案之后,我还是无法完成。我正在阅读访问权限。mdb文件,我正在填充datagridview。然后添加checkboxcolumn。完成后,您可以选择一些复选框,它们将保存在设置中。但是当我再次启动程序时,复选框的值被设置,但它们没有被绘制。我试过dataGridView.Refresh(), dataGridView.EndEdit()等等。我的错误在哪里,我错过了什么?

public partial class Form1 : Form {
    private List<int> listCheckedColumn = new List<int>();
    private List<string> listNewNames = new List<string>();
    public Form1() {
        InitializeComponent();
        //Properties.Settings.Default.Reset(); //fürs debugging
        if (!Properties.Settings.Default["pathOpenings"].Equals("leer") && !Properties.Settings.Default["pathProfiles"].Equals("leer")) {
            loadOutputData();
            //update RESULT Tabelle
        }
        if (!Properties.Settings.Default["naSysID"].Equals("leer")) {
            updateListCheckedColumnFromSettings();
            updateCheckboxes();
            //update die RESULT Tabelle
        }
        if (!Properties.Settings.Default["newNames"].Equals("leer")) {
            //fülle die new name Liste
            //fülle die zellen mit infos
            //update die RESULT Tabelle
        }
    }
    private void updateCheckboxes() {
        foreach (int value in listCheckedColumn) {
            int rowIndex = getRowIndexWithValueX(value);
            if (Convert.ToInt32(dataGridView3.Rows[rowIndex].Cells["SystemID"].Value) == value) {
                DataGridViewCheckBoxCell checkbox = (DataGridViewCheckBoxCell)dataGridView3.Rows[rowIndex].Cells["Nicht beachten"];
                checkbox.Value = checkbox.TrueValue;
                MessageBox.Show("Row:" + checkbox.RowIndex + " Column:" + checkbox.ColumnIndex);
            }
        }
    }
    ...
    ...
    /// <summary>
    /// 
    /// </summary>
    internal void loadOutputData() {
        var connOpenings = new OleDbConnection("Provider=Microsoft.Jet.OleDb.4.0;Data Source=" + Properties.Settings.Default["pathOpenings"] + ";");
        var connProfiles = new OleDbConnection("Provider=Microsoft.Jet.OleDb.4.0;Data Source=" + Properties.Settings.Default["pathProfiles"] + ";");
        ...
        //
        // System aka System Names Table
        //
        var sysNames = new DataTable();
        var adapterSysNames = new OleDbDataAdapter("SELECT SystemID, SystemName FROM Systems;", connProfiles);
        adapterSysNames.Fill(sysNames);
        dataGridView3.DataSource = sysNames;
        dataGridView3.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill;
        dataGridView3.Sort(dataGridView3.Columns[0], ListSortDirection.Ascending);
        DataGridViewCheckBoxColumn checkedColumn = new DataGridViewCheckBoxColumn();
        checkedColumn.Name = "Nicht beachten";
        checkedColumn.FalseValue = false;
        checkedColumn.TrueValue = true;
        dataGridView3.Columns.Add(checkedColumn);
        dataGridView3.CellValueChanged += new DataGridViewCellEventHandler(dataGridView3_CellValueChanged);
        dataGridView3.CurrentCellDirtyStateChanged += new System.EventHandler(dataGridView3_CurrentCellDirtyStateChanged);
    }
    ...
    private void dataGridView3_CellValueChanged(object sender, DataGridViewCellEventArgs e) {
        if (e.ColumnIndex == dataGridView3.Columns["Nicht beachten"].Index) {
            int cellValue = Convert.ToInt32(dataGridView3.Rows[e.RowIndex].Cells["SystemID"].Value);
            DataGridViewCheckBoxCell checkbox = (DataGridViewCheckBoxCell)dataGridView3.Rows[e.RowIndex].Cells["Nicht beachten"];
            if (checkbox.Value == checkbox.TrueValue) {
                if (!isInList(listCheckedColumn, cellValue)) {
                    listCheckedColumn.Add(cellValue);
                    listCheckedColumn.Sort();
                    Properties.Settings.Default["naSysID"] = makeStringFromIntList();
                    Properties.Settings.Default.Save();
                }
            } else {
                if (isInList(listCheckedColumn, cellValue)) {
                    listCheckedColumn.Remove(cellValue);
                    listCheckedColumn.Sort();
                    Properties.Settings.Default["naSysID"] = makeStringFromIntList();
                    Properties.Settings.Default.Save();
                }
            }
        }
        foreach (DataGridViewRow row in dataGridView3.Rows) {
            DataGridViewCheckBoxCell chk = (DataGridViewCheckBoxCell)row.Cells["Nicht beachten"];
            if (chk.Value == chk.TrueValue) {
                MessageBox.Show("Checked- Row: " + chk.RowIndex + " Column: " + chk.ColumnIndex);
            }
        }
    }
    ...
    private void dataGridView3_CurrentCellDirtyStateChanged(object sender, EventArgs e) {
        if (dataGridView3.IsCurrentCellDirty && dataGridView3.CurrentCell.ColumnIndex == dataGridView3.Columns["Nicht beachten"].Index) {
            dataGridView3.CommitEdit(DataGridViewDataErrorContexts.Commit);
        }
    }
   ...
   ...
}

DataGridViewCheckboxCell在加载数据后不会更新

您尝试过dataGridView.Update()吗?