datagridview refresh problem

Was hoping someone could help with a refresh problem. I'm running a function to update the colours for the rows on the DataGridView in a tabbed pane based on some value from the dataSource of the DataGridView.

However the DataGridView will not refresh the first time round. If I move to another tabbed pane and come back then the DataGridView is properly refreshed.

What I think the problem might be is that the dataGridView has a few eventhandlers when a user changes the values. So what I did was to call the EndEdit before I refreshed, which didn't work. I've even refreshed the parent of the DataGridView which didn't work as well.

Can anyone offer some suggestions as to what might be wrong Thanks.


Answer this question

datagridview refresh problem

  • sptma

    sorry for the late reply (was away on a business trip) but thanks for the replies.

    I've checked using breakpoints and it does enter the paint event when the 2nd tab is clicked, still very weird why the colours don't change though. I need to have the paint event as a separate function because the colours will change according to how well an entry is known. (It's a studylist program).

    What I'm going to try now is to explictly call the paint event through a different event handler, maybe for some strange reason it doesn't like the tabPage2_Click event.Weird, but worth a shot right

  • somatic

    Can you give details on what you mean by "on the second time around"

    The problem detailed in the original post was that the bit of code to cahnge the colours was in the click event. That meant that it wouldn't run until the user clicked inside the control. Is this what you mean by "the second time around "

    If so then you do have the same problem. You need to move the code to somewhere other than the click event.



  • _Ed Noepel

    My guess is that doing it in the pain event is serious overkill (though quite good just to make sure that the code works).

    Unless the tableset changes its colors a lot, then you really only need to do this once, after the DataGridView is populated. After that, the colors are static.



  • Jeffrey D. Baker

    can u send the code
  • Jobr77

    thanks for the reply. Here's the relevant code:


    private: System::Void tabPage2_Click(System::Object^ sender, System::EventArgs^ e) {
    dataGridView1->DataSource = currentStudyList;

    dataGridView1->Columns["Colour"]->Visible = false;
    dataGridView1->Columns["English"]->SortMode = DataGridViewColumnSortMode::NotSortable;
    dataGridView1->Columns["Hiragana"]->SortMode = DataGridViewColumnSortMode::NotSortable;
    dataGridView1->Columns["Kanji"]->SortMode = DataGridViewColumnSortMode::NotSortable;

    // update the colours in the dataGrid
    updateColours(dataGridView1, currentStudyList);
    }


    private: System::Void updateColours(DataGridView^ currView, DataTable^ currTable)
    {
    String^ colourToChange = "0";

    // increment the dataGridView changing the colours
    for(int i=0; i< currTable->Rows->Count; i++)
    {
    colourToChange = currTable->Rows[ i]["Colour"]->ToString();

    if(System::String::Equals(colourToChange, "0")){
    currView->Rows[ i]->DefaultCellStyle->BackColor = System::Drawing::Color::AliceBlue;
    }
    else if(System::String::Equals(colourToChange, "1")){
    currView->Rows[ i]->DefaultCellStyle->BackColor = System::Drawing::Color::Aquamarine;
    }
    else if(System::String::Equals(colourToChange, "2")){
    currView->Rows[ i]->DefaultCellStyle->BackColor = System::Drawing::Color::Blue;

    }
    }

    // redraw the dataGridView
    currView->EndEdit();
    currView->Focus(); // added just incase
    currView->Refresh();
    currView->Parent->Refresh();
    }

  • Saht

    Hi,

    I have the same problem, after I fill tableadapter with a dataset and view the DataGridView, (DataGridVeiw is linked to the tableAdapter) I only see the updated records on the second time around. Your thread did not seem to state an answer, could you let me know how you resolved it.

    Thanks



  • xhh

    hi cross check your code, Jero Grav is absolutely right, tabPage2 click event is not firing when you click on the tab, instead of that , i tried on the paint event for tabPage2, its working there, but i don't know how good is this approach to get the work done.
  • CharlestonSW.com

    My guess is that the Click event on TabPage2 isn't getting triggered initially so its only when you click onto another tab and then back onto TabPage2 (which *will* raise the Click event) that the code runs.

    Easy way to test if this is the case. Put a breakpoint in your updateColours() method and see if it gets hit before you click on a different tab. If it doesn't, then the code isn't being run.



  • datagridview refresh problem