数据集中使用的定界符以及替换方法 [英] Delimiter used in dataset and replace method also

查看:63
本文介绍了数据集中使用的定界符以及替换方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

开发者
我正在将一个csv文件加载到数据集中,并在datagridveiw中显示它们.
我知道数据集会自动分割逗号值,并在datagridview中显示结果.但我的问题是
我的某些csv文件值用;"分隔并且某些csv文件值以,"分隔,我也想从我的csv文件中替换通配符.所以我从project.com上获取了此代码,我认为我编写的代码不正确
请解决我的问题.

Hi developer
I am load a csv file in dataset and show them in datagridveiw .
I know dataset split comma values automaticaly and show result in datagridview . but my problem is
My some csv file values are seprated with ";" and some csv file values seprated with "," and I want a to replace wild card also from my csv file. so I m taken this code from project.com I think i am not write a code correctly
please solve my problem.

public static DataSet Convert(string filename,string TableName, string delimiter)
{
    //The DataSet to Return
    DataSet result = new DataSet();

    //Open the file in a stream reader.
    StreamReader s = new StreamReader(filename);

    //Split the first line into the columns
    string[] columns = s.ReadLine().Split(delimiter.ToCharArray());

    //Add the new DataTable to the RecordSet
    result.Tables.Add(TableName);

    //Cycle the colums, adding those that don''t exist yet
    //and sequencing the one that do.
    foreach (string col in columns)
    {
        bool added = false;
        string next = "";
        int i = 0;
        while (!added)
        {
            //Build the column name and remove any unwanted characters.
            string columnname = col + next;
            //columnname = columnname.Replace("#", "");
            //columnname = columnname.Replace("''", "");
            columnname = columnname.Replace("/", ",");

            //See if the column already exists
            if (!result.Tables[TableName].Columns.Contains(columnname))
            {
                //if it doesn''t then we add it here and mark it as added
                result.Tables[TableName].Columns.Add(columnname);
                added = true;
            }
            else
            {
                //if it did exist then we increment the sequencer and try again.
                i++;
                next = "_" + i.ToString();
            }
        }
    }

    //Read the rest of the data in the file.
    string AllData = s.ReadToEnd();

    //Split off each row at the Carriage Return/Line Feed
    //Default line ending in most windows exports.
    //You may have to edit this to match your particular file.
    //This will work for Excel, Access, etc. default exports.
    string[] rows = AllData.Split("\r\n".ToCharArray());

    //Now add each row to the DataSet
    foreach (string r in rows)
    {
        //Split the row at the delimiter.
        string[] items = r.Split(delimiter.ToCharArray());

        //Add the item
        result.Tables[TableName].Rows.Add(items);
    }

    //Return the imported data.
    return result;

}





public void Browse_Click(object sender, EventArgs e)
{
    try
    {
        DialogResult RESULT = this.OpenFileDialogbox.ShowDialog();
        if (RESULT == DialogResult.OK)
        {
            string filename = OpenFileDialogbox.FileName;
            Locations.Text = filename;
        }
        else
        {
            MessageBox.Show("Dear User Please select Path");

        }

        DataSet dataset = Form1.Convert(Locations.Text);
       Datagidveiw1.DataSource = dataset.Tables[0].DefaultView;
    }
    catch (System.Exception ex)
    {
        MessageBox.Show(ex.Message);
    }

}



当我写代码时
DataSet dataset = Form1.Convert(Locations.Text);
转换方法无法容纳位置文本,以及如何在datagridview中加载位置文本?

请解决我的问题



when I m write a code
DataSet dataset = Form1.Convert(Locations.Text);
location text can not be accpet by convert method and how its load in datagridview ?

please solve my problem

推荐答案



请按照以下步骤操作:

1.以编程方式打开.CSV文件
2.遵循一种标准(您可以通过;或,设置分隔符)并相应地修改文件
3.保存.CSV文件(只有一个定界符)
4.加载数据集



如有任何疑问,请让我知道.

请提供"投票":thumbsup:如果有帮助,请提供"接受答案",如果这是正确的答案.:rose:

谢谢,
Imdadhusen
Hi,

Please follow steps:

1. Open .CSV file with programatically
2. Follow one standard (either you can set delimiter by ; or ,) and modify file accordingly
3. Save .CSV file (with only one delimiter)
4. Load in DataSet



Please do let me know, if you have any doubt.

Please provide "Vote":thumbsup: if this would be helpful, and make "Accept Answer" if this would be correct answer.:rose:

Thanks,
Imdadhusen


这篇关于数据集中使用的定界符以及替换方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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