Solved

DataGridView Add Rows [Null Value]

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

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

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

Suggested Solutions

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…
Introduction Although it is an old technology, serial ports are still being used by many hardware manufacturers. If you develop applications in C#, Microsoft .NET framework has SerialPort class to communicate with the serial ports.  I needed to…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…
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…

803 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