I am having problems with reading Korean characters from datagridview. I read korean alphabets from csv and written them to datagridview. My method is as follows:
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
{
}
}
Until this point, there are no problems. The characters in datagridview are displayed in korean alphabets. Now i have to edit the text in datagridview and read back the text and save it in csv file and reload this csv file text to datagridview. When I did this procedure, I get some chinese text. It looks like when reading the string from datagridview, there is a problem. For reading datagridview text and saving it in csv file, my steps are as follows:
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();
}
}
I think that I have to read datagridview cell values by including some encoding methods but I am clueless. How can I read the text encoded in korean language from datagridview. Thanks in advance.
Now it looks like it works. I thought that the problem was in reading the encoded text from datagridview but the problem was in writing the encoded text to csv file. I made changes in function to read the data into .csv file. I declared a filestream path and added the filestream path and encoding to the 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();
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.