在C#中从DataGridView读取编码的字符串

问题描述

我在从datagridview读取韩文字符时遇到问题。我从csv中阅读韩文字母,并将其写入datagridview。我的方法如下:

int encoder = 949;
public void displayPCM()
{
    StreamReader sr;
    Encoding korean = Encoding.GetEncoding(encoder);
    string selectedDirectory = projectPath + "\\Spec";
    string filePath = selectedDirectory + "\\" + functionfile;

    pcmpath = new StringBuilder(filePath);
    if (Directory.Exists(selectedDirectory))
    {
        if (File.Exists(filePath))
        {
            try
            {
                sr = new StreamReader(filePath,korean);
                int lineCount = 0;
                StringBuilder readLine = new StringBuilder(sr.ReadLine());
                while (readLine.ToString() != null && readLine.ToString() != "")
                {
                    string[] substr = readLine.ToString().Split(',');
                    if (lineCount >= 1)
                    {
                        dPCM.Rows[lineCount - 1].Cells[0].Value = substr[0];
                        dPCM.Rows[lineCount - 1].Cells[1].Value = substr[1];
                        dPCM.Rows[lineCount - 1].Cells[2].Value = substr[2];
                        dPCM.Rows[lineCount - 1].Cells[3].Value = substr[3];
                    }

                    readLine = new StringBuilder(sr.ReadLine());
                    lineCount++;
                }
                sr.Close();
            }
            catch
            {
                MessageBox.Show("Error in Part File \n" + filePath);
            }
        }
        else
        {
            MessageBox.Show(filePath + "\n does not exist.");
        }

    }
    else
    {
    }
}    

到此为止,没有任何问题。 datagridview中的字符以韩文字显示。 现在,我必须在datagridview中编辑文本,然后读回文本并将其保存在csv文件中,然后将此csv文件文本重新加载到datagridview。 完成此过程后,我得到了一些中文文本。从datagridview读取字符串时,似乎有问题。要读取datagridview文本并将其保存在csv文件中,我的步骤如下:

private void Save_PCM()
{
    DirectoryInfo di;
    StreamWriter sw = null;
    int rowcount = 0;

    StringBuilder Newnum,NewName,Newlower,Newupper;
    try
    {                
        Encoding korean = Encoding.GetEncoding(encoder);
        string selectedDirectory = projectPath + "\\Spec";
        string filePath = selectedDirectory + "\\" + functionfile;
        pcmpath = new StringBuilder(filePath);

        sw = new StreamWriter(pcmpath.ToString());
        sw.WriteLine("NO,NAME,Lower SPEC,Upper SPEC");

        while (dPCM.Rows[rowcount].Cells[0].Value != null)
        {
            Newnum = new StringBuilder(dPCM.Rows[rowcount].Cells[0].Value.ToString());
            NewName = new StringBuilder(dPCM.Rows[rowcount].Cells[1].Value.ToString());
            Newlower = new StringBuilder(dPCM.Rows[rowcount].Cells[2].Value.ToString());
            Newupper = new StringBuilder(dPCM.Rows[rowcount].Cells[3].Value.ToString());
            if (Newnum == null || NewName == null || Newlower == null || Newupper == null)
                break;
            sw.WriteLine(Newnum + "," + NewName + "," + Newlower + "," + Newupper);
            rowcount++;
        }
        sw.Close();
    }
    catch (IOException e)
    {
        MessageBox.Show("Project name error" + Environment.NewLine + e.Message);
    }
    catch (NullReferenceException e)
    {
        MessageBox.Show("Enter all values in PCM" + Environment.NewLine + e.Message);
    }
    finally
    {
        if (sw != null)
            sw.Close();
    }
}

我认为我必须通过包括一些编码方法来读取datagridview单元格值,但我一无所知。 如何从datagridview中阅读以韩文编码的文本。预先感谢。

解决方法

现在看来它可以工作了。我认为问题出在从datagridview读取编码的文本,但问题出在将编码的文本写入csv文件。我对功能进行了更改,以将数据读入.csv文件。我声明了文件流路径,并将文件流路径和编码添加到了流写入器中。

private void Save_PCM()
{
    DirectoryInfo di;
    FileStream stream = null;
    StreamWriter sw = null;
    int rowcount = 0;

    StringBuilder Newnum,NewName,Newlower,Newupper;
    try
    {                
        Encoding korean = Encoding.GetEncoding(encoder);
        string selectedDirectory = projectPath + "\\Spec";
        string filePath = selectedDirectory + "\\" + functionfile;
        pcmpath = new StringBuilder(filePath);

        stream = new FileStream(pcmpath.ToString(),FileMode.Create);    // overwrite existing file
        sw = new StreamWriter(stream,korean);
        sw.WriteLine("NO,NAME,Lower SPEC,Upper SPEC");

        while (dPCM.Rows[rowcount].Cells[0].Value != null)
        {
            Newnum = new StringBuilder(dPCM.Rows[rowcount].Cells[0].Value.ToString());
            byte[] bb = korean.GetBytes(dPCM.Rows[rowcount].Cells[1].Value.ToString());
            NewName = new StringBuilder(Encoding.Default.GetString(bb));                    
            Newlower = new StringBuilder(dPCM.Rows[rowcount].Cells[2].Value.ToString());
            Newupper = new StringBuilder(dPCM.Rows[rowcount].Cells[3].Value.ToString());
            if (Newnum == null || NewName == null || Newlower == null || Newupper == null)
                break;                    
            sw.WriteLine(Newnum + "," + NewName + "," + Newlower + "," + Newupper);
            rowcount++;
        }
        sw.Close();
    }
    catch (IOException e)
    {
        MessageBox.Show("Project name error" + Environment.NewLine + e.Message);
    }
    catch (NullReferenceException e)
    {
        MessageBox.Show("Enter all values in PCM" + Environment.NewLine + e.Message);
    }
    finally
    {
        if (sw != null)
            sw.Close();
    }
}