Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Windows Forms / WinForm Controls / July 2007

Tip: Looking for answers? Try searching our database.

DataGridView with a TabPage Lossing Formating Detail

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
CraigDevel - 24 Jul 2007 18:18 GMT
Hello All,

I am have a strange problem.  I have an application which creates a set of
TabPages with DataGridViews in it.  The data is binding fine but I am losing
all of the formating I have added to the DataGridView.  Below is a snipit of
the code I am using to generate the tabpages.  Also if you create the tab
pages and datagrid views at design time the formating shows up. If anyone
knows how to get the formating to show-up I would appreciate it.

.NET Version: 2.0
OS: XP

CODE--
private void PopulateTabArea(DataSet valid, Dictionary<string, CellState[,]>
validationinfo, DataTable raw)
       {
           //Clear the tab pages.
           tbValidation.TabPages.Clear();
           //add the raw data to a tab page.
           TabPage tpraw = new TabPage("Raw Data");
           tbValidation.TabPages.Add(tpraw);
           tpraw.Parent = tbValidation;
           DataGridView dgvraw = new DataGridView();
           dgvraw.Dock = DockStyle.Fill;
           dgvraw.EditMode = DataGridViewEditMode.EditProgrammatically;
           dgvraw.AllowUserToAddRows = false;
           dgvraw.AllowUserToDeleteRows = false;
           dgvraw.AllowUserToOrderColumns = false;
           tpraw.Controls.Add(dgvraw);
           dgvraw.Parent = tpraw;
           dgvraw.AutoGenerateColumns = true;
           dgvraw.DataSource = raw;
           //dgvraw.Update();
           dgvraw.Refresh();
           tpraw.Refresh();
           tbValidation.Refresh();
           //create a tab and data grid for each data table in the valid
dataset.
           foreach(DataTable dt in valid.Tables)
           {
               CellState[,] valtable = validationinfo[dt.TableName];
               TabPage tp = new TabPage("Processed Data for " +
dt.TableName);
               tbValidation.TabPages.Add(tp);
               tp.Parent = tbValidation;
               DataGridView dgv = new DataGridView();
               dgv.Dock = DockStyle.Fill;
               tp.Controls.Add(dgv);
               dgv.Parent = tp;
               dgv.AutoGenerateColumns = true;
               dgv.DataSource = dt;
               dgv.Update();
               dgv.Refresh();
               tp.Refresh();
               tbValidation.Refresh();
               //dgv.BackgroundColor = Color.Violet;
               for (int i = 0; i < valtable.GetUpperBound(0) + 1; i++)
               {
                   //column
                   for (int j = 0; j < valtable.GetUpperBound(1) + 1; j++)
                   {
                       //row
                       CellState cs = valtable[i, j];

                       switch (cs)
                       {
                           case CellState.Invalid:
                               //set cell color to red.
                               dgv.Rows[j].Cells[i].Style.BackColor =
Color.Red;
                               break;
                           case CellState.Modified:
                               //set cell color to yellow green
                               dgv.Rows[j].Cells[i].Style.BackColor =
Color.YellowGreen;
                               break;
                           case CellState.Valid:
                               //set the color to green.
                               dgv.Rows[j].Cells[i].Style.BackColor =
Color.Green;
                               break;
                           case CellState.Initial:
                               //set the color to white.
                               dgv.Rows[j].Cells[i].Style.BackColor =
Color.White;
                               break;
                           case CellState.Unknown:
                               //set the color to yellow.
                               dgv.Rows[j].Cells[i].Style.BackColor =
Color.Yellow;
                               break;
                           default:
                               break;
                       }
                   }
               }
               dgv.Refresh();
               tp.Refresh();
               tbValidation.Refresh();
           }
       }

Signature

Thanks,
Craig.

ClayB - 24 Jul 2007 20:37 GMT
Here is an observation that you can verify.

If you drop a DataGridView on a form and bind it to a DataTable, and
add code like this in Form.Load,

              this.dataGridView1.Rows[0].Cells[2].Style.BackColor =
Color.Red;

you will see a red cell. BUT, if you click a column header to sort the
grid, the red cell goes away. So, it seems these style settings can be
transient and are reset when a ListChanged.Reset event is raised
(which happens when you sort a DataTable).

A better solution might be to use the CellFormatting event and provide
the formatting information at that point. If you have this event
handler,

      void dataGridView1_CellFormatting(object sender,
DataGridViewCellFormattingEventArgs e)
       {
           if (e.RowIndex == 0 && e.ColumnIndex == 2)
               e.CellStyle.BackColor = Color.Red;
       }

the red cell remains even if you sort things.

================
Clay Burch
Syncfusion, Inc.
CraigDevel - 24 Jul 2007 21:32 GMT
> Here is an observation that you can verify.
>
[quoted text clipped - 25 lines]
> Clay Burch
> Syncfusion, Inc.

That worked thank you very much.

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.