2014-02-19 49 views
0

我有一個項目,我堅持這個東西。我想只有三個rows.For那一刻我有三個表來顯示一個HTML表格在頁面上的警報,但我想多表4,表5表6等從asp.net中的arraylist轉移元素

這是從項目我的代碼:

public ArrayList myArr = new ArrayList(); 

public static string alarma_valoare { get; set; } 
public static string alarma_denumire_punct { get; set; } 
public static string alarma_timp { get; set; } 
public static string alarma_mesaj { get; set; } 


protected void Page_Load(object sender, EventArgs e) 
{ 
    PresSelect(); 
    TempSelect(); 
    DebSelect(); 
    DataDisplay(); 
} 

public void PresSelect() 
{ 

    string query = "String table1"; 

    dt = GetData(query); 

    if (dt.Rows.Count > 0) 
    { 
     for (int i = 0; i < dt.Rows.Count; i++) 
     { 
      alarm_value = dt.Rows[i]["value"].ToString(); 
      alarm_n = dt.Rows[i]["value1"].ToString(); 
      alarma_date = dt.Rows[i]["value2"].ToString(); 
      alarma_text = dt.Rows[i]["value3"].ToString(); 



      if (float.Parse(alarm_value) > 11) 
      { 

       myAr.Add(float.Parse(alarm_value)); 
       myAr.Add(alarm_n.ToString()); 
       myAr.Add(alarma_date.ToString()); 
       myAr.Add(alarma_text.ToString()); 



      } 
      else if (float.Parse(alarm_value) < 11) 
      { 

       myAr.Add(float.Parse(alarm_value)); 
       myAr.Add(alarm_n.ToString()); 
       myAr.Add(alarma_date.ToString()); 
       myAr.Add(alarma_text.ToString()); 
      } 
     } 
    } 
} 

public void TempSelect() 
{ 

    string query = "string table2"; 

    dt = GetData(query); 

    if (dt.Rows.Count > 0) 
    { 
     for (int j = 0; j < dt.Rows.Count; j++) 
     { 

      alarm_value = dt.Rows[j]["value"].ToString();    
      alarm_n = dt.Rows[j]["value1"].ToString(); 
      alarma_date = dt.Rows[j]["value2"].ToString(); 
      alarma_text = dt.Rows[j]["value3"].ToString(); 

      if (float.Parse(alarm_value) > 22) 
      { 

       myAr.Add(float.Parse(alarm_value)); 
       myAr.Add(alarm_n.ToString()); 
       myAr.Add(alarma_date.ToString()); 
       myAr.Add(alarma_text.ToString()); 
      } 
      else if (float.Parse(alarm_value) < 22) 
      { 
       myAr.Add(float.Parse(alarm_value)); 
       myAr.Add(alarm_n.ToString()); 
       myAr.Add(alarma_date.ToString()); 
       myAr.Add(alarma_text.ToString()); 
      } 
     } 
    } 

} 

public void DebSelect() 
{ 

    string query = "String table3"; 

    dt = GetData(query); 

    if (dt.Rows.Count > 0) 
    { 
     for (int y = 0; y < dt.Rows.Count; y++) 
     { 


      alarm_n = dt.Rows[y]["value1"].ToString(); 
      alarma_date = dt.Rows[y]["value2"].ToString(); 
      alarma_text = dt.Rows[y]["value3"].ToString(); 

      if (float.Parse(alarm_value) >33) 
      { 

        myAr.Add(float.Parse(alarm_value)); 
        myAr.Add(alarm_n.ToString()); 
        myAr.Add(alarma_date.ToString()); 
        myAr.Add(alarma_text.ToString()); 
      } 
      else if (float.Parse(alarm_value) < 33) 
      { 

       myAr.Add(float.Parse(alarm_value)); 
       myAr.Add(alarm_n.ToString()); 
       myAr.Add(alarma_date.ToString()); 
       myAr.Add(alarma_text.ToString()); 
      } 
     } 
    } 

} 

public void DataDisplay() 
{ 


    //in this function I want to display data in my html table 

} 



private static DataTable GetData(string query) 
{ 
    DataTable dt = new DataTable(); 
    string constr = ConfigurationManager.ConnectionStrings["constr"].ConnectionString; 
    using (SqlConnection con = new SqlConnection(constr)) 
    { 

     using (SqlCommand cmd = new SqlCommand(query)) 
     { 
      cmd.CommandTimeout = 0; 
      using (SqlDataAdapter sda = new SqlDataAdapter()) 
      { 
       cmd.CommandType = CommandType.Text; 
       cmd.Connection = con; 
       sda.SelectCommand = cmd; 
       sda.Fill(dt); 

      } 
     } 

     return dt; 
    } 
    SqlConnection.ClearAllPools(); 
}    

我把所有的價值從這些表本陣怎麼看這樣的:

[0] value - from table1 
[1] value1 - from table1 
[2] value2 - from table1 
[3] value3 - from table1 

[4] value - from table2 
[5] value1 - from table2 
[6] value2 - from table2 
[7] value3 - from table2 

[8] value - from table3 
[9] value1 - from table3 
[10] value2 - from table3 
[11] value3 - from table3 

結束時,我想在一個HTML表格像這樣顯示:

Value/name    Date       Message 
value/value1(T1)  value2(T1)      value3(T1) 
value/value1(T2)  value2(T2)      value3(T2) 
value/value1(T3)  value2(T3)      value3(T3) 

當數據庫來到一個新的值表3我的陣列我想是這樣的:

[0] value - from table3 
[1] value - from table3 
[2] value - from table3 
[3] value - from table3 

[4] value - from table1 
[5] value - from table1 
[6] value - from table1 
[7] value - from table1 

[8] value - from table2 
[9] value - from table2 
[10] value - from table2 
[11] value - from table2 

Value/name    Date       Message 
value/value1(T3)  value2(T3)      value3(T3) 
value/value1(T1)  value2(T1)      value3(T1) 
value/value1(T2)  value2(T2)      value3(T2) 

而且從表2一個又一個的值

[0] value - from table2 
    [1] value - from table2 
    [2] value - from table2 
    [3] value - from table2 

    [4] value - from table3 
    [5] value - from table3 
    [6] value - from table3 
    [7] value - from table3 

    [8] value - from table1 
    [9] value - from table1 
    [10] value - from table1 
    [11] value - from table1 

    Value/name    Date       Message 
value/value1(T2)  value2(T2)      value3(T2) 
value/value1(T3)  value2(T3)      value3(T3) 
value/value1(T1)  value2(T1)      value3(T1) 

我想移動在myArrraylist元素在html表格中顯示這是我想要在這個時刻爲我的數據庫中的三個表做,但在將來我有多個表。

對不起,我的英語不好,如果不明白我會盡力解釋更好。

回答

0

我認爲你可以使用隊列。

讓我解釋一下。如果你有一個隊列(你可以做一些檢查,始終有12種元素),你開始排隊值,你就會有這樣的事情:

[11] value - from table3 
    [10] value - from table3 
    [9] value - from table3 
    [8] value - from table3 

    [7] value - from table2 
    [6] value - from table2 
    [5] value - from table2 
    [4] value - from table2 

    [3] value - from table1 
    [2] value - from table1 
    [1] value - from table1 
    [0] value - from table1 
//First element 

然後,如果你得到一個新的值:

使用出列()的4倍,你西港島線lhave這樣的:

[7] value - from table3 
    [6] value - from table3 
    [5] value - from table3 
    [4] value - from table3 

    [3] value - from table2 
    [2] value - from table2 
    [1] value - from table2 
    [0] value - from table2 
//First element 

然後用四個新的值,用排隊(),你就會有這樣的事情:

[11] value - from table4 
    [10] value - from table4 
    [9] value - from table4 
    [8] value - from table4 

    [7] value - from table3 
    [6] value - from table3 
    [5] value - from table3 
    [4] value - from table3 

    [3] value - from table2 
    [2] value - from table2 
    [1] value - from table2 
    [0] value - from table2 
    //First element 

獲取它嗎? ;) 我認爲這可能是您的問題的解決方案!

這裏的想法:

Queue<string> queue = new Queue<string>(); 

      queue.Enqueue("prueba1"); 
      queue.Enqueue("prueba2"); 
      queue.Enqueue("prueba3"); 
      queue.Enqueue("prueba4"); 

      queue.Enqueue("prueba5"); 
      queue.Enqueue("prueba6"); 
      queue.Enqueue("prueba7"); 
      queue.Enqueue("prueba8"); 

      queue.Enqueue("prueba9"); 
      queue.Enqueue("prueba10"); 
      queue.Enqueue("prueba11"); 
      queue.Enqueue("prueba12"); 

      if (queue.Count == 12) 
      { 
       queue.Dequeue(); 
       queue.Dequeue(); 
       queue.Dequeue(); 
       queue.Dequeue(); 
       queue.Enqueue("prueba13"); 
       queue.Enqueue("prueba14"); 
       queue.Enqueue("prueba15"); 
       queue.Enqueue("prueba16"); 
      } 
+0

也許如果你發佈一個示例(ideea)我的問題使用隊列和出隊來理解這個概念。 – TcrisTian

+0

我認爲這很容易。創建隊列。然後,在實際將「添加」元素添加到列表的行中,檢查您的隊列是否包含12個元素,如果是,則首先使用Dequeue()四個元素,然後使用Enqueue()沒有充分和有空間的地方,那麼就要把新的元素加入進來。 –

+0

用一個小例子編輯的答案 –

0

我不會使用ArrayList這一點。創建一個類來保存你的數據並提供有意義的屬性名稱。然後,您可以更改您的GetData方法,以提供這些類的集合,您可以在您的表示邏輯中使用這些類來填充HTML表。

也許像下面

class AlarmData { 

    public float AlarmValue { get; set; } 
    public string AlarmN { get; set; } 
    public string AlarmDate { get; set; } // Could even be of type DateTime 
    public string AlarmText { get; set; } 

} 

在你DebSelectTempSelectPresSelect方法,你有很多重複的代碼。也許可以將大量內容移到GetData方法中,而不是填充數組列表創建一個AlarmData對象集合並返回它們。

然後你的DebSelect,TempSelectPresSelect方法可以簡單地對返回的數據做你的條件邏輯。

對於您的DataDisplay方法,我會調查ASP.NET Repeater Control來構建您的ASP.NET表格或DIVS或您選擇用來佈置內容的任何方法。如果你真的想要你可以看看你的自定義業務對象(AlarmData)的數據綁定,以進一步簡化事情。

這與您的原始示例有相當大的偏差,但它可能是我建議的一個值得的。