如何在 DataGridView 中查看超过 15 个字符的数字?
How to see a number with more than 15 characters in a DataGridView?
我正在尝试将一个超过 15 个字符的数字从 CSV 文件导入 DataGridView。在 CSV 文件中,它看起来像 8940012004412026012
,但在 DataGridView 中导入后显示为 8.9400120044120259E + 18
。
这是我现在的代码:
public static DataTable GetDataTable(string strFileName)
{
ADODB.Connection oConn = new ADODB.Connection();
oConn.Open("Provider=Microsoft.Jet.OleDb.4.0; Data Source = " + System.IO.Path.GetDirectoryName(strFileName) + "; Extended Properties = \"Text;HDR=YES;FMT=Delimited\";", "", "", 0);
string strQuery = "SELECT * FROM [" + System.IO.Path.GetFileName(strFileName) + "]";
ADODB.Recordset rs = new ADODB.Recordset();
System.Data.OleDb.OleDbDataAdapter adapter = new System.Data.OleDb.OleDbDataAdapter();
DataTable dt = new DataTable();
rs.Open(strQuery, "Provider=Microsoft.Jet.OleDb.4.0; Data Source = " + System.IO.Path.GetDirectoryName(strFileName) + "; Extended Properties = \"Text;HDR=YES;FMT=Delimited\";",
ADODB.CursorTypeEnum.adOpenForwardOnly, ADODB.LockTypeEnum.adLockReadOnly, 1);
adapter.Fill(dt, rs);
return dt;
}
private void Import()
{
if (textBox4.Text.Trim() != string.Empty)
{
try
{
DataTable ct = GetDataTable(textBox4.Text);
dataGridView2.AllowUserToAddRows = false;
dataGridView2.DataSource = ct;
dataGridView2.Columns[0].HeaderCell.Value = "1";
dataGridView2.Columns[1].HeaderCell.Value = "2";
dataGridView2.Columns[2].HeaderCell.Value = "3";
}
catch (Exception ex)
{
MessageBox.Show(ex.Message.ToString());
}
}
}
dataGridView2.Columns[2].HeaderCell.Value = "3";
需要8940012004412026012
感谢您使用 CsvHelper 的建议,这是我找到的解决方案
private void button60_Click(object sender, EventArgs e)
{
OpenFileDialog fdlg = new OpenFileDialog();
fdlg.Title = "Select file";
fdlg.InitialDirectory = @"c:\";
fdlg.FileName = textBox4.Text;
fdlg.Filter = "Text and CSV Files(*.txt, *.csv)|*.txt;*.csv|Text Files(*.txt)|*.txt|CSV Files(*.csv)|*.csv|All Files(*.*)|*.*";
fdlg.FilterIndex = 1;
fdlg.RestoreDirectory = true;
if (fdlg.ShowDialog() == DialogResult.OK)
{
using (var reader = new StreamReader(fdlg.FileName))
using (var csv = new CsvReader(reader, CultureInfo.InvariantCulture))
{
using (var dr = new CsvDataReader(csv))
{
var dt = new DataTable();
dt.Load(dr);
dataGridView2.DataSource = dt;
}
}
}
}
我正在尝试将一个超过 15 个字符的数字从 CSV 文件导入 DataGridView。在 CSV 文件中,它看起来像 8940012004412026012
,但在 DataGridView 中导入后显示为 8.9400120044120259E + 18
。
这是我现在的代码:
public static DataTable GetDataTable(string strFileName)
{
ADODB.Connection oConn = new ADODB.Connection();
oConn.Open("Provider=Microsoft.Jet.OleDb.4.0; Data Source = " + System.IO.Path.GetDirectoryName(strFileName) + "; Extended Properties = \"Text;HDR=YES;FMT=Delimited\";", "", "", 0);
string strQuery = "SELECT * FROM [" + System.IO.Path.GetFileName(strFileName) + "]";
ADODB.Recordset rs = new ADODB.Recordset();
System.Data.OleDb.OleDbDataAdapter adapter = new System.Data.OleDb.OleDbDataAdapter();
DataTable dt = new DataTable();
rs.Open(strQuery, "Provider=Microsoft.Jet.OleDb.4.0; Data Source = " + System.IO.Path.GetDirectoryName(strFileName) + "; Extended Properties = \"Text;HDR=YES;FMT=Delimited\";",
ADODB.CursorTypeEnum.adOpenForwardOnly, ADODB.LockTypeEnum.adLockReadOnly, 1);
adapter.Fill(dt, rs);
return dt;
}
private void Import()
{
if (textBox4.Text.Trim() != string.Empty)
{
try
{
DataTable ct = GetDataTable(textBox4.Text);
dataGridView2.AllowUserToAddRows = false;
dataGridView2.DataSource = ct;
dataGridView2.Columns[0].HeaderCell.Value = "1";
dataGridView2.Columns[1].HeaderCell.Value = "2";
dataGridView2.Columns[2].HeaderCell.Value = "3";
}
catch (Exception ex)
{
MessageBox.Show(ex.Message.ToString());
}
}
}
dataGridView2.Columns[2].HeaderCell.Value = "3";
需要8940012004412026012
感谢您使用 CsvHelper 的建议,这是我找到的解决方案
private void button60_Click(object sender, EventArgs e)
{
OpenFileDialog fdlg = new OpenFileDialog();
fdlg.Title = "Select file";
fdlg.InitialDirectory = @"c:\";
fdlg.FileName = textBox4.Text;
fdlg.Filter = "Text and CSV Files(*.txt, *.csv)|*.txt;*.csv|Text Files(*.txt)|*.txt|CSV Files(*.csv)|*.csv|All Files(*.*)|*.*";
fdlg.FilterIndex = 1;
fdlg.RestoreDirectory = true;
if (fdlg.ShowDialog() == DialogResult.OK)
{
using (var reader = new StreamReader(fdlg.FileName))
using (var csv = new CsvReader(reader, CultureInfo.InvariantCulture))
{
using (var dr = new CsvDataReader(csv))
{
var dt = new DataTable();
dt.Load(dr);
dataGridView2.DataSource = dt;
}
}
}
}