2014-04-22 169 views
0

我有一個列表,我想用它來創建一個跨列均勻分佈的表。
所以我想借此:從列表中創建表

function_id 
exception rpt 
alarm maint  
ratio adder  
temp ratio  
change   
access   
aet sequence 
eng display  
line set  
clear repeaters 
enable function 
volt setpoint 
feed setpoint 
feed report  
volt report  
problem pot  
temp voltage 
flag pc in/out 
tap enable  

,並創建以下(很抱歉的格式):

Col1--------------Col2--------------Col3--------------Col4--------------Col5--------------Col6 

exception rpt   eng display     volt report     ae map          search/starve   amps volts      
alarm maint     line set        problem pot     exception log   votrax watch    search screen   
ratio adder     clear repeaters temp voltage    pot status      bath, metal     newpot          
temp ratio      enable function flag pc in/out noise report    alarm watch     pcram v/o       
change          volt setpoint   tap enable      select pots     repeater check 
access          feed setpoint   set enable      shift summary   pcram           
aet sequence    feed report     enable status   trace report    ratio entry     

我現在有工作,但看起來應該有一個更有效的方式:

public DataTable CreateMenuTable() 
    { 

     DataTable userFunctions = GetMenus(); 
     DataTable menuTable = new DataTable(); 
     DataRow menuRow; 
     int rowNum = 0; 

     int numColumns = (int)Math.Sqrt(userFunctions.Rows.Count); 
     int numRows = (int)Math.Ceiling(userFunctions.Rows.Count/(float)numColumns); 

     for (int i = 0; i < numColumns; i++) 
     { 
      menuTable.Columns.Add(new DataColumn("Col" + (i + 1), System.Type.GetType("System.String"))); 
     } 

     for (int i = 0; i < numRows; i++) 
     { 
      menuRow = menuTable.NewRow(); 
      menuTable.Rows.Add(menuRow); 
     } 

     foreach (DataRow row in userFunctions.AsEnumerable()) 
     { 
      if (rowNum < numRows) 
      { 
       menuRow = menuTable.Rows[rowNum]; 
       menuRow["Col1"] = row["function_id"];     
      } 

      if (rowNum >= numRows & rowNum < (numRows * 2)) 
      { 
       menuRow = menuTable.Rows[rowNum - (numRows)]; 
       menuRow["Col2"] = row["function_id"]; 
      } 

      if (rowNum >= (numRows * 2) & rowNum < (numRows * 3)) 
      { 
       menuRow = menuTable.Rows[rowNum - (numRows * 2)]; 
       menuRow["Col3"] = row["function_id"]; 
      } 

      if (rowNum >= (numRows * 3) & rowNum < (numRows * 4)) 
      { 
       menuRow = menuTable.Rows[rowNum - (numRows * 3)]; 
       menuRow["Col4"] = row["function_id"]; 
      } 

      if (rowNum >= (numRows * 4) & rowNum < (numRows * 5)) 
      { 
       menuRow = menuTable.Rows[rowNum - (numRows * 4)]; 
       menuRow["Col5"] = row["function_id"]; 
      } 

      if (rowNum >= (numRows * 5) & rowNum < (numRows * 6)) 
      { 
       menuRow = menuTable.Rows[rowNum - (numRows * 5)]; 
       menuRow["Col6"] = row["function_id"]; 
      } 

      rowNum++;     
     } 
     return menuTable; 
    } 
+0

你在開發什麼平臺? –

+0

@GrantWinney看起來像c# – harsimranb

+0

我很困惑。你是試圖在三列上顯示這個還是將它放在數據庫中?如果是後者,那是一個瘋狂的想法。如果是前者,則不需要混淆DataTable。 – Aron

回答

0

你可以避免所有的if這樣的事情:

// Calculate to which column this should belong 
int columnNumber = rowNum/numRows + 1; 
// % just return the remainder of the division between two numbers 
// if you think about it it's the same as rowNum - (numRows)*K 
menuRow = menuTable.Rows[rowNum % numRows]; 
// put it in the right column 
menuRow["Col"+columnNumber] = row["function_id"]; 
+0

這就像一個魅力,更乾淨!謝謝! – tcrafton