Solved

DataGridView Add Rows [Null Value]

Posted on 2011-02-22
4
470 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 167 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 166 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 167 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

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
ASP.NET MVC -Update model when click on <li> element 29 106
Save json data from URL using SSIS 1 77
.NET Enums [Flags] & Bitwise Design Question 6 53
c#, datatable, aspx 4 53
This article describes a simple method to resize a control at runtime.  It includes ready-to-use source code and a complete sample demonstration application.  We'll also talk about C# Extension Methods. Introduction In one of my applications…
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…
This video shows how to use Hyena, from SystemTools Software, to update 100 user accounts from an external text file. View in 1080p for best video quality.

710 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