【发布时间】:2020-02-17 17:34:48
【问题描述】:
如何在 datagridview 中传递子字符串或单个字符。
这是我的文本文件
qwqwqw
a000v00 5000x222w000
BB00fFF 444422555550
qwqwqw
我想以以下格式在 DataGridView 中显示此内容
1 2 3 4
a000 v00 5000x 222w000
BB00 fFF 44442 2555550
这是我的错误代码 我该怎么办?
DataTable table = new DataTable();
table.Columns.Add("1", typeof(string));
table.Columns.Add("2", typeof(string));
table.Columns.Add("3", typeof(string));
table.Columns.Add("4", typeof(string));
string path;
OpenFileDialog ofd = new OpenFileDialog();
ofd.Filter = "Text Files (*.txt)|*.txt|All Files (*.*)|*.*";
ofd.Title = "txt";
if (ofd.ShowDialog(this) == DialogResult.Cancel) return;
path = ofd.FileName;
string[] lines = File.ReadAllLines(path);
string[] values;
for (int i = 1; i < lines.Length-1; i++)
{
values = lines[i].Split(" ".ToCharArray(), StringSplitOptions.RemoveEmptyEntries);
string[] row = new string[values.Length];
for (int j = 0; j < values.Length; j++)
{
row[j] = values[j].Trim();
}
table.Rows.Add(row);
}
private void Form1_Load(object sender, EventArgs e)
{
DataView.DataSource = table;
}
非常感谢
【问题讨论】:
-
您想知道如何从
DataTable table获取数据以显示在DataGridView中吗?你在用winforms吗? -
YSE,Windows 窗体
-
SORRY,不是指这个问题
-
问题的解决方法,比如1F的答案。我的英语表达不好。对不起!
标签: c#