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

Reading encoded string from DataGridView in c#

我在从 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 文件。我声明了一个文件流路径并将文件流路径和编码添加到 streamwriter。

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();
    }
}