I'm using DatagridView
in c# to read a csv file.
The data is not sorting properly when clicking the headers at the top of the grid.
For example, it should be sorted as 1,2,3,4,5,etc
However, the result is 1,10,2,20,etc
.
I think I need to convert it from a string
to an int
, but I don't know how to do that.
Here's my current code:
try
{
if (openFileDialog1.ShowDialog() == DialogResult.OK)
{
string csvPath = openFileDialog1.FileName;
string rowValue;
// int rowValue = int.Parse(??);
string[] cellValue;
dataGridView1.Rows.Clear();
//dataGridView1.Columns.Clear();
if (System.IO.File.Exists(csvPath))
{
System.IO.StreamReader fileReader = new StreamReader(csvPath);
rowValue = fileReader.ReadLine();
cellValue = rowValue.Split(',');
for (int i = 0; i <= cellValue.Count() - 1; i++)
{
DataGridViewTextBoxColumn column = new DataGridViewTextBoxColumn();
column.Name = cellValue[i]; //column name , value
column.HeaderText = cellValue[i];
dataGridView1.Columns.Add(column);
// dataGridView1.Columns[].CellType = typeof(Int64);
}
while (fileReader.Peek() != -1)
{
rowValue = fileReader.ReadLine();
cellValue = rowValue.Split(',');
dataGridView1.Rows.Add(cellValue);
}
fileReader.Dispose();
fileReader.Close();
}
else