从控制台应用程序显示二维数据阵列 [英] Displaying a 2d data array from a console application

查看:67
本文介绍了从控制台应用程序显示二维数据阵列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道已经创建并关闭了一些线程(例如



要执行此操作,还需要添加 System.Drawing 。对于 Windows.Forms ,您需要同时添加using子句和引用:





(我这里只有德语VS版本;您应该搜索参考文献而不是 Aktuell(即当前)组结果是相同的-我选择了另一个,因为它在屏幕截图中不太大。)



一旦引用到位..

 使用系统; 
使用System.Collections.Generic;
使用System.Linq;
使用System.Text;
使用System.Threading.Tasks;
使用System.Windows.Forms; //< ---添加名称空间AND引用!
使用System.Drawing; //< ---添加名称空间AND引用!

..这个简单的控制台应用程序将编译并运行:

 命名空间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 =
新Font(DGV.Font,FontStyle.Bold);
for(int i = 0; i< test.Count; i ++)DGV.Rows.Add(new []
{(i + 1)+,test [i],i + 21 +}); //便宜的字符串数组
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));
位图bmp =新位图(DGV.ClientSize.Width,DGV.ClientSize.Height);
DGV.DrawToBitmap(bmp,DGV.ClientRectangle);
bmp.Save( D:\\testDGV.png,System.Drawing.Imaging.ImageFormat.Png);
bmp.Dispose();
}
}
}


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?

解决方案

  • 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.

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:

(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();
        }
    }
}

这篇关于从控制台应用程序显示二维数据阵列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆