我正在编写一个Watch Window,它从 Serial Port 获取数据,并在 DataGridView 中显示变量info / value . 我有一个 Timer Event ,每隔500毫秒触发一次,并为每一行更新 Value 列 . 用户可以从ComboBox中选择Data Type ,这样他/她可以观察从变量的行值转换的实际值 .

当我单击Type列中的 ComboBox 时,将鼠标指针指向与所选 Type 不同的 Type 然后稍等一下(超过500ms), Timer Event 会使 Combobox 自动将选择带到SelectedIndex . 我需要将鼠标移动到另一个区域并返回到我想要选择的 Type 并在Timer事件自动刷新之前选择它 .

我怀疑的是,我只是在_12677中更改一个单元格(值),但我想它会刷新整行 . 所以,如果我没有在 Timer Event 触发之前点击我想要选择的类型,它会自动选择当前的SelectedItem . 如果我不想让它刷新,直到我点击那里并手动选择其中一个,我怎么能防止刷新ComboBox?

这是我的 DataTableDataGridView 绑定:

BindingSource SBind = new BindingSource();
DataTable WatchDataTable = new DataTable();

public WatchWindowForm()
{
    InitializeComponent();
    initializeDataTable();
    initializeConfigPanel();
}

private void initializeDataTable()
{
    /* Create the DataTable with the same column properties */
    WatchDataTable.Columns.Add(new DataColumn("varName"));
    WatchDataTable.Columns.Add(new DataColumn("varSize"));
    WatchDataTable.Columns.Add(new DataColumn("varValue"));
    WatchDataTable.Columns.Add(new DataColumn("varAddress"));
    WatchDataTable.Columns.Add(new DataColumn("varType"));
    WatchDataTable.Columns.Add(new DataColumn("varDelete"));

    /* Bind the DataTable to the DataGridview (varTable_dgv) */
    SBind.DataSource = WatchDataTable;
    varTable_dgv.DataSource = SBind;
}

这是我的 Timer Event

private void WatchTimerEvent(object sender, EventArgs e)
{   
    foreach(DataRow dr in WatchDataTable.Rows)
    {
        /* Search the database using the "Name" cell in the row 
        and find the corresponding local variable  */
        Var_t var = DataBase.LiveVariableDatabase.Find(x => x.name == dr["varName"].ToString());

        /* Get the new value of the variable 
        and write it to the "Value" cell in the DataGridView */
        dr["varValue"] = DataTypes.GetTypedVariable(var);
    }
}

Edit: 我意识到当我尝试读取Type ComboBox的当前值时,它会读取默认值 . 但是如果我尝试从 button click 读取当前值,它会正确读取当前值 . 我们可以假设从不同的线程访问DataTable之间存在差异吗?