在datagrid中显示记事本内容 [英] Display notepad content in datagrid

查看:107
本文介绍了在datagrid中显示记事本内容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好



我有一个控制台应用程序,它以下面的格式给我输出记事本



端口名称状态Vlan双工速度类型
Gi0 / 1 LabSwitch:Gi1 / 0/18连接1 a-full a-1000 10/100 / 1000BaseTX
Gi0 / 2 notconnect 1 auto auto 10/100 / 1000BaseTX
Gi0 / 3 notconnect routed auto auto 10/100 / 1000BaseTX
Gi0 / 4 notconnect 10 full 100 10/100 / 1000BaseTX
Gi0 / 5 notconnect 10 full 100 10/100 / 1000BaseTX
Gi0 / 6 notconnect 1 auto auto 10/100 / 1000BaseTX





现在我想在这个应用程序中添加一个表单并在数据网格中显示它。



请告诉我怎么做。



谢谢

解决方案

请参阅使用OleDb导入文本文件(选项卡,CSV,自定义) [ ^ ]。


要做的步骤:



1。创建自定义类,假设 ConsApp 。定义以后要使用的属性/字段/成员:

  public   ConsApp 
{
私有 字符串 port = string .Empty;
private string name = string .Empty;
private string status = string .Empty;
private string vlan = string .Empty;
private string duplex = string .Empty;
private string speedType = string .Empty;

public string 端口
{
get { return port; }
set {port = value ; }
}

public string 名称
{
获取 {返回名称; }
set {name = value ; }
}

public string 状态
{
获取 {返回状态; }
set {status = value ; }
}

public string Vlan
{
get { return vlan; }
set {vlan = value ; }
}

public string Duplex
{
get { return duplex; }
set {duplex = value ; }
}

public string SpeedType
{
get { return speedType;}
set {speedType = value ; }
}
}





2。使用 ReadAllLines方法 [ ^ ]获取来自文本文件的所有行,然后使用 Linq查询 [ ^ ]将数据转换为自定义类:

< pre lang =cs> string sFileName = @ FullFileNameToTextFile.txt;
// 读取所有行
var lines = File.ReadAllLines(sFileName);
// 将数据转换为自定义类,跳过标题
var ca = lines.Skip( 1 )。选择(x =>新ConsApp()
{
Port = x.Substring( 0 10 )。Trim(),
Name = x.Substring( 10 18 )。修剪(),
状态= x .Substring( 28 13 )。Trim(),
Vlan = x.Substring( 42 10 )。Trim(),
Duplex = x.Substring( 53 7 )。修剪(),
SpeedType = x.Substring( 60 )。修剪()
})。ToList();





当你可以看到,我使用子串方法 [ ^ ]返回与每个字段/属性对应的数据部分。



3。现在,您可以将上述查询的结果绑定到DataGridView:

 DataGridView1.DataSource = ca; 





这就是全部!试试!

Hi all

I am having one console application which is giving me output in a notepad in below format

Port      Name               Status       Vlan       Duplex  Speed Type
Gi0/1     LabSwitch:Gi1/0/18 connected    1          a-full a-1000 10/100/1000BaseTX
Gi0/2                        notconnect   1            auto   auto 10/100/1000BaseTX
Gi0/3                        notconnect   routed       auto   auto 10/100/1000BaseTX
Gi0/4                        notconnect   10           full    100 10/100/1000BaseTX
Gi0/5                        notconnect   10           full    100 10/100/1000BaseTX
Gi0/6                        notconnect   1            auto   auto 10/100/1000BaseTX



Now i want to add one form in this application and display this same in a datagrid.

Please tell me how to do that.

Thank you

解决方案

See Using OleDb to Import Text Files (tab, CSV, custom)[^].


Steps to do:

1. Create custom class, let's say ConsApp. Define properties/fields/members you want to use later:

public class ConsApp
{
    private string port = string.Empty;
    private string name = string.Empty;
    private string status = string.Empty;
    private string vlan = string.Empty;
    private string duplex = string.Empty;
    private string speedType = string.Empty;

    public string Port
    {
        get { return port; }
        set { port = value; }
    }

    public string Name
    {
        get { return name; }
        set { name = value; }
    }

    public string Status
    {
        get { return status; }
        set { status = value; }
    }

    public string Vlan
    {
        get {return vlan; }
        set { vlan = value; }
    }

    public string Duplex
    {
        get { return duplex; }
        set { duplex = value; }
    }

    public string SpeedType
    {
        get { return speedType ;}
        set { speedType = value; }
    }
}



2. Use ReadAllLines method[^] to get all lines from text file, then use Linq query[^] to cast data as custom class:

string sFileName  = @"FullFileNameToTextFile.txt";
 //read all lines
var lines = File.ReadAllLines(sFileName);
//cast data as custom class, skip headers
var ca = lines.Skip(1).Select(x=>new ConsApp()
        {
            Port =  x.Substring(0, 10).Trim(),
            Name =  x.Substring(10, 18).Trim(),
            Status =  x.Substring(28, 13).Trim(),
            Vlan =  x.Substring(42, 10).Trim(),
            Duplex =  x.Substring(53, 7).Trim(),
            SpeedType =  x.Substring(60).Trim()
        }).ToList();



As you can see, i'm using Substring method[^] to return the portion of data corresponding to each field/property.

3. Now, you can bind the result of above query to DataGridView:

DataGridView1.DataSource = ca;



That's all! Try!


这篇关于在datagrid中显示记事本内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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