Windows Develop Bookmark and Share   
 index > Windows Forms Data Controls and Databinding > DataGridView looses its CellStyle
 

DataGridView looses its CellStyle

Hello all,
I have a tabControl, contains2 tabpages. Both tabpages contains DatagridView which is binded to Collection.
In that one column of both datagridViewcontains DataGridViewcheckBoxcell and DatagridViewTextBoxCell.


for example following is my class and collection
 class Data

        {

          public bool Bool { get; set; }

          public  string Name { get; set; }

        }



        class Datasource : BindingList<Data>

        {

 

        }

I added two columns at design time both are as DatagridViewTextBoxColumn
And in FormLoad i bind it to DatagridViewlike,
 private void Form2_Load(object sender, EventArgs e)

        {

            Datasource ds = new Datasource();

            for (int i = 0; i < 10; i++)

                ds.Add(new Data()

                {

                    Bool = false,

                    Name = i.ToString()

                }

                );



            dataGridView1.DataSource = ds;

            for (int i = 0; i < 10; i++)

            {<br/>

                if ((i % 2) == 0)

                {<br/>                     /*And I changed some cell to DatagriViewCheckBoxcell*/

                    DataGridViewCheckBoxCell dgvcb=new DataGridViewCheckBoxCell(false);                    

                    dataGridView1.Rows[i].Cells["Column1"] = dgvcb;

                }

            }

        }

When Form is displayed, It shows data correctly.


Now I want to reshuffle tabpages, like remove tabPage and insert it any other location.


 private void button1_Click(object sender, EventArgs e)

        {

            tabControl1.SuspendLayout();          

            tabControl1.TabPages.Remove(tabPage1);

            tabControl1.TabPages.Insert(1, tabPage1);           

            tabControl1.ResumeLayout();

        }

The DataGridView oftabpage which I removed and Inserted, loose all its dataGridViewCheckBoxCell, and displayed them as DatagridViewTextBoxcell.
Why it is happening ?
  • Edited byNareshG Thursday, August 06, 2009 2:41 PMTitle changed
  •  
NareshG  Thursday, August 06, 2009 2:38 PM

Hi NareshG,

Based on my understanding, the root cause of the issue is that the data of the DataGridView is rebound and the cells of the DataGridView is recreated. Since the type of the column is DataGridViewTextBoxColumn, the new created cells would be of type DataGridViewTextBoxCell. So that we can only see the DataGridViewTextBoxCells.

These are my solutions:

1. We can reassign the cells just after we remove and insert the tab page as follows:

this.tabControl1.SuspendLayout();

this.tabControl1.TabPages.Remove(tabPage1);

this.tabControl1.TabPages.Insert(1, tabPage1);

tabControl1.ResumeLayout();

for (int i = 0; i < 5; i++)

{

if (i % 2 == 0)

this.dataGridView1.Rows[i].Cells[0] = new DataGridViewCheckBoxCell(false);

}

2. We can handle the DataBindingComplete event of the DataGridView and reassign the cells as follows:

void dataGridView1_DataBindingComplete(object sender, DataGridViewBindingCompleteEventArgs e)

{

for (int i = 0; i < 5; i++)

{

if (i % 2 == 0)

this.dataGridView1.Rows[i].Cells[0] = new DataGridViewCheckBoxCell(false);

}

}

Let me know if this helps.
Aland Li

Please mark the replies as answers if they help and unmark if they don't. This can be beneficial to other community members reading the thread.
Aland Li  Monday, August 10, 2009 3:40 AM

Hi Aland,
Thanks for reply.
Not only it change to DataGridViewTextBoxCell, but also change the backColor, If I set by DataGridViewRow.DefaultCellstyle.BackColor.

your solutions are no good for me, but it may helps others.


First solution,
In my scenarion it is not useful asTabControl and TabPage dont have access to DataGridView, and DataGridView is created in userControl. I know that I canexpose method, but it will the last option I will do.

Second Solution,
It fires both time, when bind to DataGridView and when I remove-insert DataGridView from tabpages. But problem is it fires two times. And DatagridViewbindingCompleteEventArgs is alos not helpful, as both time ListTypeChanged is Reset.

But Aland, we should not do allthese things again and again, there should be some way so that we can tell DataGridViewnot to rebound.

Thanks again.

NareshG  Tuesday, August 11, 2009 5:32 AM

Hi NareshGï¼?/span>

There is a way to prevent the binding event firing. This is a code snippet:

BindingSource bindSrc = this.dataGridView1.DataSource as BindingSource;

//Suspend the binding from source to DataGridView

bindSrc.RaiseListChangedEvents = false;

//Do something here, binding event would not fire here.

//Resume the binding from source to DataGridView

bindSrc.RaiseListChangedEvents = true;

//Reset the binding to synchronize the data.

bindSrc.ResetBindings(false);

Let me know if this helps.
Aland Li


Please mark the replies as answers if they help and unmark if they don't. This can be beneficial to other community members reading the thread.
Aland Li  Wednesday, August 12, 2009 1:55 AM

Hi Aland,
I tried, but it is not working.
NareshG  Wednesday, August 12, 2009 8:47 AM

You can use google to search for other answers

Custom Search

More Threads

• can't set the value of comboBox.selectedValue
• short help...Sample/ Data enrty screen
• Crystal Reports Help
• Difficulty in databinding.
• Setting Row Height based on cell content
• how to Change the editing control on the fly ?
• UserPreferenceChangedEventHandler
• Collapsible and expansible GridView
• application design with unreliable/periodic connection to sql server
• DataGrid Rows and Columns