开发者

C# DataGrid CSV Import moving any size array into defined column grid?

I have a function to which an array has a large sum of CSV imported split data. I currently have it set as the code below however I'm having some issues getting the data to go into the separate columns than into a single one. What I would like to achieve is a non-redundant means of supplying a function a string array of any size, and define how many columns across the data 开发者_运维技巧must be read before adding it as a row into the DataGrid.

    private string csvtogrid(string input, columns)
    {
        input = input.Replace("\r", ",").Substring(2).TrimEnd(',').Trim().Replace("\n", ",").Replace(",,,", ",").Replace(",,",",");
        string[] repack = input.Split(',');
        string[] cell = new string[columns];
        int rcell = 0;
        for (int counter = 1; counter < repack.Length; counter++)
        {
            if (rcell < columns)
            {
                cell[rcell] = repack[counter];
                rcell++;
            }
            //MessageBox.Show(cell[0] + cell[1] + cell[2]);
            procgrid.Rows.Add(cell[0], cell[1], cell[2]);
            rcell = 0;
        }
        return null;
    }


The most reliable and easy method i have ever found to read in csv files is the TextFieldParser class. It's in the Microsoft.VisualBasic.FileIO namespace so it is probably not referenced by defaukt if you use c# but it's should be in the gac foryou to reference.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜