c ++如何防止在加载库存上调用函数Grid CellValueChanged

时间:2014-05-16 19:02:01

标签: winforms datagridview c++-cli

当我调用LoadItems()函数时,也会触发下面的事件。

void InventoryList::LoadItems()
{
    string s = "select * from pos_db.items;";
    String^ cmdString = gcnew String(s.c_str());

    String^ conString = L"datasource=localhost;port=3306;username=root;password=root";
    MySqlConnection^ conDataBase = gcnew MySqlConnection(conString);
    MySqlCommand^ cmdDataBase = gcnew MySqlCommand(cmdString, conDataBase);
    MySqlDataReader^ myReader;

    try
    {
        conDataBase->Open();
        myReader = cmdDataBase->ExecuteReader();
        while(myReader->Read())
        {
            inventoryGrid->Rows->Add();
            DataGridViewRow^ row = inventoryGrid->Rows[inventoryGrid->RowCount - 1];
            row->Cells["alu"]->Value = myReader->GetString(0);
            row->Cells["upc"]->Value = myReader->GetString(1);
            row->Cells["vendor"]->Value = myReader->GetString(2);
            row->Cells["sku"]->Value = myReader->GetString(3);
            row->Cells["description"]->Value = myReader->GetString(4);
            row->Cells["size"]->Value = myReader->GetString(5);
            row->Cells["color"]->Value = myReader->GetString(6);
            row->Cells["price"]->Value = myReader->GetString(7);
            row->Cells["cost"]->Value = myReader->GetString(8);
            row->Cells["quantity"]->Value = myReader->GetString(9);
        }
        conDataBase->Close();
    }
    catch (Exception^ ex)
    {
        MessageBox::Show(ex->Message);
    }
}

private: System::Void inventoryGrid_CellValueChanged(System::Object^ sender, System::Windows::Forms::DataGridViewCellEventArgs^  e)
{
    if (inventoryGrid->Visible)
    {
        UpdateItem(e->RowIndex, inventoryGrid->Columns[e->ColumnIndex]->Name, (String^)inventoryGrid->Rows[e->RowIndex]->Cells[e->ColumnIndex]->Value);
    }
}

inventoryGrid-> Visible保持UpdateItem()函数在最初加载时不被调用,但是当我搜索一个项时,它再次调用LoadItems()函数,再次触发该事件。麻烦的是,在搜索过程中调用它时,DataGridView是可见的,并且调用了UpdateItem()函数。如何在不必快速查看DataGridView闪烁的情况下防止这种情况发生?

1 个答案:

答案 0 :(得分:0)

您尚未显示搜索代码,因此我不确定为什么搜索需要您再次调用LoadItems。此外,您还没有向UpdateItem显示您的代码,因此我们不知道那里发生了什么。

那就是说,我会考虑拨打SuspendLayout& ResumeLayout。暂停,然后对网格进行所有修改,然后调用resume。