?
Solved

DataGridView Add Rows [Null Value]

Posted on 2011-02-22
4
Medium Priority
?
491 Views
Last Modified: 2012-05-11
Experts,

I`m using VS 2008 C# 3.5.

How do I add rows to a DataGridView for something like this.

    foreach (double T in TimeList)
                {
                    List<double?> TElem = new List<double?>();

                    for (int k = 0; k < CList.Count; k++)
                    {
                        if (CList[k].Time == T && CList[k].Ax == Enum.A)
                        {
                            TElem.Add(CList[k].Pos);
                        }
                        else
                        {
                            TElem.Add(null);
                        }
                        if (CList[k].Time == T && CList[k].Ax == Enum.B)
                        {
                            TElem.Add(CList[k].Pos);
                        }
                        else
                        {
                            TElem.Add(null);
                        }
                        if (CList[k].Time == T && CList[k].Ax == Enum.C)
                        {
                            TElem.Add(CList[k].Pos);
                        }
                        else
                        {
                            TElem.Add(null);
                        }
                        if (CList[k].Time == T && CList[k].Ax == Enum.D)
                        {
                            TElem.Add(CList[k].Pos);
                        }
                        else
                        {
                            TElem.Add(null);
                        }
                        if (CList[k].Time == T && CList[k].Ax == Enum.E)
                        {
                            TElem.Add(CList[k].Pos);
                        }
                        else
                        {
                            TElem.Add(null);
                        }
                   }

                    MTabUCntrl.GridView.Rows.AddRange(TElem.ToArray()); //Error Here
                   
                }

Basically I`ll have a dataset like

12.56    null    56.67   null   null             - Row 1
12.56    78.7   56.67   null   46.6           - Row 2
12.56    null     56.67   null   null            - Row 3

Each row is a List. I`m open to change this if there is a better way to do this.
I want to add these to a DatGridView.
 
0
Comment
Question by:San24
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
4 Comments
 
LVL 9

Accepted Solution

by:
rawinnlnx9 earned 668 total points
ID: 34953708
0
 

Author Comment

by:San24
ID: 34953729
I`m sorry...I should have mentioned it. I`m working on Windows Forms.
0
 
LVL 13

Assisted Solution

by:Naman Goel
Naman Goel earned 664 total points
ID: 34953959
hey I am not seeing any error on similar operation

 dataGridView1.Rows.Add( "1", null,"2", null );

This is not giving any exception..


also I had used NullValue property for displaying NULL.

            System.Windows.Forms.DataGridViewCellStyle dataGridViewCellStyle1 = new System.Windows.Forms.DataGridViewCellStyle();
            dataGridViewCellStyle1.Alignment = System.Windows.Forms.DataGridViewContentAlignment.MiddleLeft;
            dataGridViewCellStyle1.BackColor = System.Drawing.SystemColors.Window;
            dataGridViewCellStyle1.Font = new System.Drawing.Font("Microsoft Sans Serif", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            dataGridViewCellStyle1.ForeColor = System.Drawing.SystemColors.ControlText;
            dataGridViewCellStyle1.NullValue = "NULL";
            dataGridViewCellStyle1.SelectionBackColor = System.Drawing.SystemColors.Highlight;
            dataGridViewCellStyle1.SelectionForeColor = System.Drawing.SystemColors.HighlightText;
            dataGridViewCellStyle1.WrapMode = System.Windows.Forms.DataGridViewTriState.False;
            this.dataGridView1.DefaultCellStyle = dataGridViewCellStyle1;
0
 
LVL 11

Assisted Solution

by:Sudhakar Pulivarthi
Sudhakar Pulivarthi earned 668 total points
ID: 34979026
Hi,

U can add the datatable in the dataset directly to DataGridView by assigning the DataSource property of the grid. This is simple and straight forward.
Ex:
datagridview.DataSource = dataset.Tables[0]; // the first table is the source table
Check this example:
http://msdn.microsoft.com/en-us/library/fbk67b6z.aspx
0

Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …
In response to a need for security and privacy, and to continue fostering an environment members can turn to for support, solutions, and education, Experts Exchange has created anonymous question capabilities. This new feature is available to our Pr…

718 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question