2013-10-10 178 views
0

關於如何讀取文本文件等有很多主題,我覺得我已經閱讀了所有的文章。但我還沒有能夠將其應用於我的問題。搜索文本文件中的特定行,添加到預定義的dgv

我有一個文本文件,看起來像這樣:

產品ID,產品,客戶ID,lineone,linetwo,linethree AABBCC,香蕉,K001,什麼東西,什麼東西,什麼 BBCCAA,蘋果,K002,something1 ,something2,something3 AACCBB,橘子,K003,something4,something5,something6 CCAABB,香蕉,K001,成才,事,東西

我有一個窗體(Form),在那裏我有一個文本框(tbCustomerID)和一個dataGridView(dgvProducts)。

我想要做的事情是,我想在Form1的文本框中填寫CustomerID 然後我想循環遍歷文本文件中的所有CostumerID,然後在預定義的dataGridView中顯示ProductID和Product有6列。 (產品ID和產品=列1和2)

我第一次嘗試:(從正常類)

public DataTable ReadFromFile() 
    {    
     //Read the data from text file 
     string[] textData = File.ReadAllLines("MyTextFile.txt"); 
     string[] headers = textData[0].Split(','); 

     //Create and populate DataTable 
     DataTable dataTable1 = new DataTable(); 
     foreach (string header in headers) 
      dataTable1.Columns.Add(header, typeof(string), null); 
     for (int i = 1; i < textData.Length; i++) 
      dataTable1.Rows.Add(textData[i].Split(','));    

     return dataTable1; 

    } 

然後在我的Form1中:

Form1 frm1 = new Form1(); 

    private void button_Click(object sender, EventArgs e) 
    { 

     dgvProducts.DataSource = frm1.ReadFromFile();    
    } 

但是,這只是顯示所有項目從文本文件到dataGridView。

嘗試2:(從我的類)

public string findCustomer(string CustomerID) 
    { 
     StreamReader sr = new StreamReader("MyTextFile.txt"); 
     string searchId = CustomerID; 
     string actualId = ""; 
     string produktID = "No match found"; 
     string[] details = null; 
     string line = null; 
     while ((line = sr.ReadLine()) != null) 
     { 
      line = line.Trim(); 
      if (line == "") continue; 
      details = line.Split(','); 
      actualId = (details[0]); 
      if (actualId == searchId) 
      { 
       productID = details[2].Replace("\"", ""); 
       break; 
      } 
     } 
     sr.Close(); 
     return productID; 
    } 

然後在Form1

Form1 frm1 = new Form1(); 
    private void button_Click(object sender, EventArgs e) 
    { 
     string aa = tbKundID.Text; 
     dgvProducts.Rows.Add(frm1.findCustomer(aa));    
    } 

但這種方式我只從第2列在文本文件中得到一個值,因爲我不能返回兩個值在C#中。 我也嘗試從文本文件中讀取所有行,然後劃分所有列,然後以某種方式顯示一些我想要顯示的特定內容,但是我無法通過搜索或添加到dataGridView來使用它。 這是我的最後一招,所以我非常感謝幫助我如何以最簡單的方式做到這一點。

回答

0

編輯您的第一次嘗試:

Form1 frm1 = new Form1(); 

private void button_Click(object sender, EventArgs e) 
{ 
    dgvProducts.DataSource = frm1.ReadFromFile(tbCustomerID.Text); <-- pass customer id  
} 

public DataTable ReadFromFile(String customerId) 
{    
    //Read the data from text file 
    string[] textData = File.ReadAllLines("MyTextFile.txt"); 
    string[] headers = textData[0].Split(','); 

    //Create and populate DataTable 
    DataTable dataTable1 = new DataTable(); 
    foreach (string header in headers) 
     dataTable1.Columns.Add(header, typeof(string), null); 
    for (int i = 1; i < textData.Length; i++) 
     if textData[i].Split(',')[2].ToString() == customerId <-- add the row only if customer Id matches 
     dataTable1.Rows.Add(textData[i].Split(','));    

    return dataTable1; 

} 
+0

我現在通過文本文件循環完美!但是,如何從文本文件中取出第1列和第2列,並將其添加到具有預定義列的datagridview中? (如果我在搜索中遇到問題,我想把第1列和第2列拿出來) – Nisse

0

感謝@monkyxyz解決您的DataTable的方法。我個人更喜歡儘可能保持OOP的東西,並發現DataTable不夠靈活(除非問題範圍保持簡單和靜態)。

另一種選擇是將文本文件反序列化爲對象,並根據需要傳遞對象。我假設你舒適地綁定一個對象到DataGridView。

我會忽略所有的代碼,除非你指出你需要它。

但得到的東西滾動:

+0

從來沒有聽說過Seralization,我對編程相當陌生。但我會檢查出來! – Nisse

+0

@Nisse樂於助人! .net中建立的序列化僅限於二進制(1和0)和XML。因爲你使用逗號分隔值,所以我提供了其他項目。重新發明車輪毫無意義。供參考:以下是關於XML序列化的更多信息。 [Serialize](http://msdn.microsoft.com/en-us/library/szzyf24s.aspx)和[Deserialize](http://msdn.microsoft.com/en-us/library/fa420a9y.aspx) – JFish222

+0

感謝您的鏈接!我覺得這個Seralization的東西真的很讓人困惑,另一個答案是我無法從文本文件中挑出兩列,然後在預定義的dgv中顯示它們。我真的不能讓它工作,因爲我想=/ 我討厭問,但你介意給我看一些代碼解決方案嗎? (我有一個文本文件,我想要搜索customerID,然後僅顯示文本文件中包含列的預定義dgv中的productID和產品) – Nisse