可能重複:
Putting a .txt file into a DataGridView如何將.txt文件放入數據源?
當我單擊打開按鈕,我想選擇一個文件,把它變成一個DataSource
到futher被加工成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
我很熟悉DataSource
和DataGridView
所以如果我能得到一些幫助它如何工作,需要發生什麼,它將如何看,等將不勝感激。 :)
謝謝。
_「into a DataSource」_?你究竟是什麼意思? – stakx
@Stakx:那麼我想使用IBindingSource,這樣我就可以從txt文件填充DataGridView – theNoobGuy
@theNoobGuy,'IBindingSource'不是一個容器。你不能把任何東西「放入」它。相反,它具有必須設置的自己的'DataSource'屬性(意思是說:你的數據必須放在別的地方,例如放到'DataTable'中 - 參見鏈接的問題)。所以'IBindingSource'不是數據源,但更類似於另一個數據源的適配器或裝飾器。 – stakx