文本导入datagridview单元格,出现索引必须在集合的大小范围之内,不能超过该大小范围或小于零
1,读取文本到datagridview
public static DataTable TxtToDataTable(string strFileName, char strSplit)
{
DataTable dtReturn = new DataTable();
string[] strFileTexts = File.ReadAllLines(strFileName,System.Text.Encoding.UTF8);
string[] strLineTexts = strFileTexts[0].Split(strSplit);
for (int i = 0; i <strLineTexts.Length; i++)
{
dtReturn.Columns.Add("Columns" + i.ToString());
}
for (int i = 0; i < strFileTexts.Length; i++)
{
strLineTexts = strFileTexts[i].Split(strSplit);
DataRow dr = dtReturn.NewRow();
for (int j = 0; j < strLineTexts.Length; j++)
{
dr[j] = strLineTexts[j].ToString();
}
dtReturn.Rows.Add(dr);
}
}
2,读取绑定数据。
private void myButton1_Click(object sender, EventArgs e)
{
OpenFileDialog fd = new OpenFileDialog();
fd.Filter = "角度文件(*.txt)|*.*";
fd.DefaultExt = ".txt";
DialogResult result = fd.ShowDialog();
string file = fd.FileName;
&nb