Solved

DataGridView Add Rows [Null Value]

Posted on 2011-02-22
4
450 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
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

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Article by: Najam
Having new technologies does not mean they will completely replace old components.  Recently I had to create WCF that will be called by VB6 component.  Here I will describe what steps one should follow while doing so, please feel free to post any qu…
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!
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

708 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now