【发布时间】:2011-08-17 16:20:25
【问题描述】:
当我单击打开按钮时,我想选择一个文件并将其放入 DataSource 中,以便进一步放入 DataGridView 中。
我现在的样子是这样的:
OpenFileDialog openFile = new OpenFileDialog();
openFile.DefaultExt = "*.txt";
openFile.Filter = ".txt Files|*.txt";
openFile.RestoreDirectory = true;
try
{
if (openFile.ShowDialog() == DialogResult.OK && openFile.FileName.Length > 0)
{
// Right now I am loading the file into a RichTextBox
openFileRTB.LoadFile(openFile.FileName, RichTextBoxStreamType.PlainText);
// What I would like to do is load it into a DataSource and then into a DataGridView.
// So really I would like to remove the openFileRTB line of code and replace it.
// That is where I need help :).
}
}
catch (Exception)
{
MessageBox.Show("There was not a specified file path to open.", "Path Not Found Error", MessageBoxButtons.OK, MessageBoxIcon.Warning);
}
这是我要打开的文件示例(空格分隔):
Title1 Title2 Title3 Title4 Title5 Title6
abc123 abc123-123-123 225.123 123.456 180 thing99
c123 somethingHERE 987.123 123.456 360 anotherThing1
abc124 somethingHERE225.123 123.456 0 thing99
我对@987654326@ 和DataGridView 非常陌生,所以如果我能就它的工作原理、需要发生的事情、外观等方面获得帮助,我将不胜感激。 :)
谢谢。
【问题讨论】:
-
“进入数据源”?你到底是什么意思?
-
@Stakx:好吧,我想使用 IBindingSource,这样我就可以从 txt 文件中填充 DataGridView
-
@theNoobGuy,
IBindingSource不是容器。你不能把任何东西“放进”它。相反,它有自己的必须设置的DataSource属性(意思是:您的数据必须转到其他地方,例如进入DataTable- 请参阅链接的问题)。所以IBindingSource不是数据源,而是更类似于另一个数据源的适配器或装饰器。 -
@Stakx:感谢您的澄清。嗯
标签: c# text datagridview datasource