0

I am aware that there are already a few threads on this already created and closed (like 2-dimensional Integer array to DataGridView)

My problem is that since I have only been working with console applications, I am not aware of what I need to do in order to apply the code.

Up till now I have drag and dropped a new dataGridView and chose program.cs (where my main is) as a source. Now when I apply the code from the aforementioned link in program.cs, visualstudio is saying that dataGridView1 "does not exist in the current context". When I try to declare it beforehand, I get that the type/namespace can't be found. Any ideas?

Community
  • 1
  • 1
Gabriel Stellini
  • 426
  • 4
  • 13
  • The tag [tag:winform] has two great tutorials linked at the end of the [tag-wiki](http://stackoverflow.com/tags/winforms/info) – rene Sep 26 '15 at 16:12
  • Do you really need to work with console? I'm afraid, you can't use DataGridView in this case. You can use WinForms project, or write some other code. Something like foreach(var line in myArray) { foreach(int elem in line) { string s = String.Format("{0}\t",elem); Console.Write(s); } } – Oxoron Sep 26 '15 at 18:21
  • Have you resolved your problem? – TaW Oct 30 '15 at 11:38
  • @TaW I ended up using graphics objects and picture boxes and moving onto windows forms but it was very interesting reading what you wrote. Thanks a bunch :D PS: new question here: http://stackoverflow.com/questions/33454884/seeking-a-particular-value-when-using-binarywriter – Gabriel Stellini Oct 31 '15 at 18:12

1 Answers1

2
  • With regard to the errors you got: All you need to do is add the namespace: using System.Windows.Forms; and also a reference to it! This works just fine for Console applications. Of course it will never show up but other than that you can make use of its abilities..

  • But the real question is: What do you want to achieve and why do you stick to a console application?

This is not to say that you may not have a good reason! For example it is sometimes necessary to run a service application without a display screen. This could still create output from DataGridViews or from Chart controls..

But it always helps to understand the situation fully when we answer questions here..

Here is an example that creates and fills a DataGridView DGV and then saves an image of the data to a png file.

enter image description here

For this to work you also need to add System.Drawing. As for Windows.Forms you need to add both the using clause and the reference:

enter image description here

(I have only a German VS version here; instead of the 'Aktuell' (ie 'Current') group you should search the references in the 'Framework'! The result is the same - I chose the other one becasue it is not so big on the screenshot..)

Once the references are in place..

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;        // <--- add namespace AND reference!!
using System.Drawing;             // <--- add namespace AND reference!!

..this simple console application will compile and run:

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            DataGridView DGV = new DataGridView();
            List<string> test = new List<string>() 
            { "Anna", "Bertha", "Carol", "Doreen", "Erica", "Fran", "Gisa" };
            DGV.Columns.Add("No", "Number");
            DGV.Columns.Add("Name", "Name");
            DGV.Columns.Add("Age", "Age");
            DGV.Columns["Name"].DefaultCellStyle.Font = 
                                             new Font(DGV.Font, FontStyle.Bold);
            for (int i = 0; i < test.Count; i++) DGV.Rows.Add(new[]
                { (i + 1)+ "", test[i], i + 21 +""}); // cheap string array
            DGV.ScrollBars = ScrollBars.None;
            DGV.AllowUserToAddRows = false;
            DGV.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill;
            DGV.RowHeadersVisible = false;
            var width = DGV.Columns.GetColumnsWidth(DataGridViewElementStates.None);
            DGV.ClientSize = new Size(width,  
                         DGV.ColumnHeadersHeight + DGV.RowCount * (DGV.Rows[0].Height) );
            Bitmap bmp = new Bitmap(DGV.ClientSize.Width, DGV.ClientSize.Height);
            DGV.DrawToBitmap(bmp, DGV.ClientRectangle);
            bmp.Save("D:\\testDGV.png", System.Drawing.Imaging.ImageFormat.Png);
            bmp.Dispose();
        }
    }
}
TaW
  • 53,122
  • 8
  • 69
  • 111