2016-04-18 42 views

回答

0

我不知道会是怎样为你的表和如何表应该看起来像数据源,但让我们说,你有,你有网名为MyGrid

您可以动态地添加行的窗口和列并表

public void MakeTable(int columns, int rows) 
    { 
     for (int x = 0; x < columns; x++) 
      MyGrid.ColumnDefinitions.Add(new ColumnDefinition()); 
     for (int y = 0; y < rows; y++) 
     { 
      RowDefinition r = new RowDefinition(); 
      r.Height = GridLength.Auto; 
      MyGrid.RowDefinitions.Add(r); 
     } 

     for (int x = 0; x < columns; x++) 
     { 
      for (int y = 0; y < rows; y++) 
      { 
       TextBox tb = new TextBox();     
       tb.Text = "my text for " + x + " " + y; 
       Grid.SetColumn(tb, x); 
       Grid.SetRow(tb, y); 
       MyGrid.Children.Add(tb); 
      } 
     } 
    } 

    public string TableValue(int column, int row, int rows) 
    { 
     int i = row + column * rows; 
     return ((TextBox)MyGrid.Children[i]).Text; 
    } 

函数调用比看起来是这样的:

 int columns = 2; 
     int rows = 3; 

     MakeTable(columns, rows); //prepare table 

     string s = TableValue(0, 2, rows); //read string from coordinates 

为了例如将数据存入一个文本文件,你必须定义一些规则。例如,在文件的顶部写入列数和行数,然后写入单个单元格。在重新加载时了解内容。或者在列之间放置制表符...

+0

完美的作品!非常感谢你! – Reigada

相关问题