将.DAT文件的内容添加到DataGridView [英] Adding Contents of a .DAT File to DataGridView

查看:95
本文介绍了将.DAT文件的内容添加到DataGridView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我编写了以下c#代码将.DAT文件的内容加载到ListBox



代码:

 [StructLayout(LayoutKind.Sequential,Pack =  1 ,CharSet = CharSet.Ansi)] 
struct file_record
{

public char EXCH;
public int ExchCode;
public byte NameLength;
[MarshalAs(UnmanagedType.ByValArray,SizeConst = 12 )]
public < span class =code-keyword> char [] ScripName;

}


private void button1_Click( object sender,EventArgs e)
{

OpenFileDialog ofd = new OpenFileDialog();
string path = ;
ofd.FileName = ;
ofd.ShowDialog();
path = ofd.FileName;

if string .IsNullOrEmpty(ofd.FileName))
return ;
FileStream fs = new FileStream(path,FileMode.OpenOrCreate,FileAccess.ReadWrite);
BinaryReader br = new BinaryReader(fs);

int value = Marshal.SizeOf( typeof运算(file_record));


var len = fs.Length;
var num_records = len / value ;
file_record [] Bse = new file_record [num_records];
字节 [] buffer = new 字节[Marshal.SizeOf( typeof (file_record))];
for int i = 0 ; i < num_records; i ++)
{
fs.Read(buffer, 0 value );
GCHandle handle = GCHandle.Alloc(buffer,GCHandleType.Pinned);
Bse [i] =(file_record)Marshal.PtrToStructure(handle.AddrOfPinnedObject(), typeof (file_record));
string abc = new string (BSE [I] .ScripName);
string str2 = abc.Substring( 0 ,Bse [i] .NameLength);
listBox1.Items.Add(Bse [i] .ExchCode + + str2) ;
handle.Free();

}

}







I想要在不使用DataSource的情况下将DAT文件的内容添加到DataGridView。任何人都可以给我一个代码片段来实现这个目标吗?



谢谢

解决方案

你必须经历一个 DataGridViewRow [ ^ ]。



DataGridView有一个属性Rows,它有一个函数 Add(...)



为了使用这种手动方式添加行,您无法设置DataSource属性。如果您确实设置了DataSource属性,则需要更改DataSet,Table,...以添加行。



希望这能为您澄清事情。

I have written the following c# code to load the contents of the .DAT file to ListBox

Code:

[StructLayout(LayoutKind.Sequential, Pack = 1, CharSet = CharSet.Ansi)]
    struct file_record
    {

        public char Exch;
        public int ExchCode;
        public byte NameLength;
        [MarshalAs(UnmanagedType.ByValArray, SizeConst = 12)]
        public char[] ScripName;

    }


    private void button1_Click(object sender, EventArgs e)
    {

        OpenFileDialog ofd = new OpenFileDialog();
        string path = "";
        ofd.FileName = "";
        ofd.ShowDialog();
        path = ofd.FileName;

        if (string.IsNullOrEmpty(ofd.FileName))
        return;
        FileStream fs = new FileStream(path, FileMode.OpenOrCreate, FileAccess.ReadWrite);
        BinaryReader br = new BinaryReader(fs);

        int value = Marshal.SizeOf(typeof(file_record));


        var len = fs.Length;
        var num_records = len / value;
        file_record[] Bse = new file_record[num_records];
        Byte[] buffer = new Byte[Marshal.SizeOf(typeof(file_record))];
        for (int i = 0; i < num_records; i++)
        {
            fs.Read(buffer, 0, value);
            GCHandle handle = GCHandle.Alloc(buffer, GCHandleType.Pinned);
            Bse[i] = (file_record)Marshal.PtrToStructure(handle.AddrOfPinnedObject(), typeof(file_record));
            string abc = new string(Bse[i].ScripName);
            string str2 = abc.Substring(0, Bse[i].NameLength);
            listBox1.Items.Add(Bse[i].ExchCode + " " + str2);
            handle.Free();

        }

    }




I want to add the contents of the DAT File to DataGridView without using DataSource.Can anyone give me a code snippet to achieve this?

Thanks

解决方案

You have to go through a DataGridViewRow[^].

The DataGridView has a property "Rows" that has a function Add( ... ).

In order to use this "manual" way of adding rows you cannot set the DataSource propery. If you did set the DataSource property you need to change the DataSet, Table, ... in order to add the rows.

Hope this clarifies things for you.


这篇关于将.DAT文件的内容添加到DataGridView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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