DataGridViewCheckboxCell在加载数据后不会更新

时间:2016-08-23 15:06:47

标签: c# datagridview datagridviewcheckboxcell

在尝试了一些解决方案后,我无法完成它。 我正在读取access.mdb文件,我正在填充 datagridview。 然后我添加了checkboxcolumn。完成后,您可以选择一些复选框,它们将保存在设置中。但是当我再次启动prog时,复选框的值已设置但不会绘制它们。我尝试了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);
        }
    }

   ...

   ...
}

1 个答案:

答案 0 :(得分:1)

你试过dataGridView.Update()吗?也许这很有效。