2014-10-08 98 views
0

我已經創建了一個應用程序,我必須加載.resx文件並在datagridview中顯示.resx文件的內容。我通過菜單欄加載.resx。我曾嘗試使用下面的代碼試過,但沒有數據顯示..讀取.resx文件並顯示datagridview中的內容

private void openToolStripMenuItem_Click(object sender, EventArgs e) 
{ 
    OpenDialog.Reset(); 
    OpenDialog.InitialDirectory = Directory.GetCurrentDirectory(); 
    OpenDialog.RestoreDirectory = false; 
    OpenDialog.Filter = "Resource files (*.resx)|*.resx"; 
    if (OpenDialog.ShowDialog() == DialogResult.OK) 
    { 
     StreamReader MyStream = new StreamReader(OpenDialog.FileName); 
     BBookGrid.DataSource = null; 
     m_BBookTable.Clear(); //Clear the existing table 
     BBookGrid.DataSource = m_BBookTable; 
     try 
     { 
      while (true) 
      { 
       String MyLine = MyStream.ReadLine(); 
       if (MyLine == null) 
       { 
        break; 
       } 
       else if (MyLine.Length != 0) 
       { 
        String[] fields = MyLine.Split(Separator.ToCharArray()); 
        if (fields.GetLength(0) == NumColumns) 
        { 
         m_BBookTable.Rows.Add(m_BBookTable.NewRow()); 
         m_BBookTable.Rows[m_BBookTable.Rows.Count - 1][SourceCol] 
            = fields[0].Trim(); 
         m_BBookTable.Rows[m_BBookTable.Rows.Count - 1][TargetCol] 
            = fields[1].Trim(); 
        } 
       } 
      } 
     } 
     catch (Exception ex) 
     { 
      MessageBox.Show("Fatal Error" + ex.ToString()); 
      Application.Exit(); 
     } 
    } 
} 
+2

會發生什麼情況?你有錯誤嗎?? – TaW 2014-10-08 13:28:14

+0

我可以打開文件,但數據不能顯示在datagridview上。 – JB14 2014-10-08 13:45:25

+0

//初始化啓動時的所有內容 //創建一個有兩列的數據表 this.m_BBookTable = new DataTable(TableName); (new DataColumn(NameCol,Type.GetType(「System.String」))); (DataColumn(DateCol,Type.GetType(「System.DateTime」))); BBookGrid.DataSource = m_BBookTable; //設置一個更好看的表格 BookGrid.Columns [NameCol] .Width =(BBookGrid.Width - BBookGrid.RowHeadersWidth - 2 * GridLineWidth)/ NumColumns- GridLineWidth; BBookGrid.Columns [DateCol] .Width = BookGrid.Columns [NameCol] .Width; – JB14 2014-10-08 13:47:13

回答

0

您需要設置DataSource 後,你有數據填充它!

private void openToolStripMenuItem_Click(object sender, EventArgs e) 
{ 
    OpenDialog.Reset(); 
    OpenDialog.InitialDirectory = Directory.GetCurrentDirectory(); 
    OpenDialog.RestoreDirectory = false; 
    OpenDialog.Filter = "Resource files (*.resx)|*.resx"; 
    if (OpenDialog.ShowDialog() == DialogResult.OK) 
    { 
     StreamReader MyStream = new StreamReader(OpenDialog.FileName); 
     BBookGrid.DataSource = null; 
     m_BBookTable.Clear(); //Clear the existing table 
     ///BBookGrid.DataSource = m_BBookTable; /// this line should be 
     // down after the catch i.e. after the data source is filled!! 
     try 
     { 
      while (true) 
      { 
       String MyLine = MyStream.ReadLine(); 
       if (MyLine == null) 
       { 
        break; 
       } 
       else if (MyLine.Length != 0) 
       { 
        String[] fields = MyLine.Split(Separator.ToCharArray()); 
        if (fields.GetLength(0) == NumColumns) 
        { 
         m_BBookTable.Rows.Add(m_BBookTable.NewRow()); 
         m_BBookTable.Rows[m_BBookTable.Rows.Count - 1][SourceCol] 
            = fields[0].Trim(); 
         m_BBookTable.Rows[m_BBookTable.Rows.Count - 1][TargetCol] 
            = fields[1].Trim(); 
        } 
       } 
      } 
     } 
     catch (Exception ex) 
     { 
      MessageBox.Show("Fatal Error" + ex.ToString()); 
      Application.Exit(); 
     } 
     // now that is has data we set the data source! 
     BBookGrid.DataSource = m_BBookTable; 
    } 
}