Solved

DataGridView Add Rows [Null Value]

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
XML & .net 5 41
Need a complete list of ASP.NET page load events 3 29
ASP.NET 5 Templates 2 68
free scanner TWAIN can be operated with a Web application 9 41
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!
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.
Both in life and business – not all partnerships are created equal. As the demand for cloud services increases, so do the number of self-proclaimed cloud partners. Asking the right questions up front in the partnership, will enable both parties …

863 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

21 Experts available now in Live!

Get 1:1 Help Now