2011-08-17 4 views
0

가능한 중복 :
Putting a .txt file into a DataGridView.txt 파일을 DataSource에 저장하는 방법은 무엇입니까?

내가 오픈 버튼을 클릭하면 내가 파일을 선택하고 DataGridView으로 작업 할 학습과하는 DataSource에 배치하고 싶습니다.

오른쪽 지금은이 같은 모습을 가지고있는 : DataSource와 매우 익숙

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 

은 내가 : 여기

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); 
} 

내가 개방 될 수있는 파일 (공간 분리)의 예입니다 DataGridView 그래서 어떻게 작동하는지, 어떤 일이 일어나야하는지, 어떻게 보일 지 등 크게 도움이 될만한 도움을 얻을 수 있다면. :)

감사합니다.

당신은 생성하는 모든 행/열 선 루프를 분할 할 수
+0

_ "into DataSource"_? 그게 정확히 무슨 뜻이야? – stakx

+0

@Stakx : 글쎄, IBindingSource를 사용하여 txt 파일에서 DataGridView를 채울 수 있습니다. – theNoobGuy

+0

@theNoobGuy, 'IBindingSource'는 컨테이너가 아닙니다. 당신은 아무것도 넣을 수 없습니다. 대신 자신의 'DataSource' 속성이 설정되어야합니다. 즉, 데이터가 다른 곳 (예 :'DataTable ')으로 이동해야합니다 (링크 된 질문 참조). 따라서 'IBindingSource'는 데이터 소스가 아니라 다른 데이터 소스의 어댑터 또는 장식 자와 유사합니다. – stakx

답변

1

DataTable의 예를 들면 다음과 같습니다

(VB.NET) :

Dim separator = " "c 
Dim fileName = OpenFileDialog1.FileName 
Dim tbl As New DataTable(fileName) 
Dim query = From line In IO.File.ReadAllLines(fileName) 
      Let row = line.Split(separator) 

If query.Any Then 
    For Each col In query(0).row 
     'build DataColumns from first line' 
     tbl.Columns.Add(New DataColumn(col)) 
    Next 
    If query.Count > 1 Then 
     For rowIndex = 1 To query.Count - 1 
      Dim newRow = tbl.NewRow 
      For colIndex = 0 To query(rowIndex).row.Length - 1 
       Dim colValue = query(rowIndex).row(colIndex) 
       newRow(colIndex) = colValue 
      Next 
      tbl.Rows.Add(newRow) 
     Next 
    End If 
End If 

이도 이제 (LINQ없이 그냥 잘 작동 C#;)) :

.... 
var rows = System.IO.File.ReadAllLines(fileName); 
if (rows.Length != 0) { 
    foreach (string headerCol in rows(0).Split(separator)) { 
     tbl.Columns.Add(new DataColumn(headerCol)); 
    } 
    if (rows.Length > 1) { 
     for (rowIndex = 1; rowIndex < rows.Length; rowIndex++) { 
      var newRow = tbl.NewRow(); 
      var cols = rows(rowIndex).Split(separator); 
      for (colIndex = 0; colIndex < cols.Length; colIndex++) { 
       newRow(colIndex) = cols(colIndex); 
      } 
      tbl.Rows.Add(newRow); 
     } 
    } 
} 
+0

조금 도와 줘서 고마워. VB.net. haha에 익숙하지 않다. – theNoobGuy

+0

@theNoobGuy : 최근에 내 대답을 편집하고 C# 예제를 제공했다. –

+0

중복 질문에 답하기 위해'round (-0.49)'. ;) – stakx

관련 문제