2015-04-22 96 views
0

我正在使用批量更新的gridview。在對列進行更改並點擊頁面中的更新按鈕後,將執行此foreach循環以更新。現在我該怎麼添加這些字符串值的ArrayList循環在循環gridview行後將字符串值添加到數組列表中c#

protected void btnUpdate_Click(object sender, EventArgs e) 
{ 
    try 
    { 
     foreach (GridViewRow row in gvDetails.Rows) 
     { 
      string strID = ((Label)row.FindControl("lblID")).Text; 
      string strGroup = ((Label)row.FindControl("lblGrp")).Text; 
      string strValue = ((TextBox)row.FindControl("txtValue")).Text;  
     } 
     ArrayList alList = new ArrayList(); 
     alList.Add(strID); 
     alList.Add(strGroup); 
     alList.Add(strValue); 
    } 
    catch (Exception ex) 
    { 

    } 
} 

之後,但在串的循環後的值是沒有得到插入ArrayList中。凡有人幫我解決這個問題。

+0

爲什麼不使用調試器來通過列表......你會很快看到你沒有正確地做什麼的地方..你需要爲啓動器定義alList作爲可訪問循環之外的變量在類級別,如果你打算使用它並且或者在應用程序的其他地方獲得對它的訪問..如果不是在循環外部聲明並創建一個List 對象變量..那麼在循環內部添加字符串變量到列表親自你會更好地創建一個類對象,並在那裏存儲數據 – MethodMan

+0

我也建議你谷歌如何創建一個數據表,其中包含3個字段,您正在嘗試使用和或創建... – MethodMan

+0

aint this sa我問邁克爾問了嗎? http://stackoverflow.com/revisions/29794413/1 – naveen

回答

1

它看起來你的代碼應該是

var alList = new List<string>(); 
foreach (GridViewRow row in gvDetails.Rows) 
{ 
     string strID = ((Label)row.FindControl("lblID")).Text; 
     string strGroup = ((Label)row.FindControl("lblGrp")).Text; 
     string strValue = ((TextBox)row.FindControl("txtValue")).Text;  

     alList.Add(strID); 
     alList.Add(strGroup); 
     alList.Add(strValue); 
} 

否則你想加入到列表變量超出範圍(因爲strID和其他已申報環內範圍內)

注:不要使用ArrayList它已經過時。在你的情況下,最好使用List<string>代替。

或者,如果我錯過了你的觀點,你需要添加到列表從最後一次迭代唯一變量(因爲它可以從你的代碼中應該) - 然後聲明strID和其他變量循環,就像這樣:

string strID = null, strGroup = null, strValue = null; 
var alList = new List<string>(); 
foreach (GridViewRow row in gvDetails.Rows) 
{ 
     strID = ((Label)row.FindControl("lblID")).Text; 
     strGroup = ((Label)row.FindControl("lblGrp")).Text; 
     strValue = ((TextBox)row.FindControl("txtValue")).Text;  
} 
alList.Add(strID); 
alList.Add(strGroup); 
alList.Add(strValue); 

更新

因爲我們在評論中已經明確你的目標,其實你不必List<string>而是DataTable,你的代碼可能是這樣的:

var dt = new DataTable(); 
dt.Columns.Add("ID", typeof(string)); 
dt.Columns.Add("Group", typeof(string)); 
dt.Columns.Add("Value", typeof(string)); 

foreach (GridViewRow row in gvDetails.Rows) 
{ 
     var dro = dt.NewRow(); 
     dro["ID"] = ((Label)row.FindControl("lblID")).Text; 
     dro["Group"] = ((Label)row.FindControl("lblGrp")).Text; 
     dro["Value"] = ((TextBox)row.FindControl("txtValue")).Text; 

     dt.Rows.Add(dro); 
} 

另請注意 - datatable列的數據類型可以是任何字符串,而不僅僅是字符串 - 它取決於您要存儲的實際數據。

+0

感謝您的及時回覆。我的gridview中共有10行。所以你的意思是說,在循環思考所有行後,這段代碼將把所有10行添加到我的數組列表或字符串中?你也可以讓我知道如何將這些字符串值添加到列表中,因爲我是新手。 – Learner

+0

@Learner看到更新的答案 - 可能我錯過了你的原始目標。正如你所看到的 - 這取決於你(和你的目標) - 或者添加所有10個字符串或者最後一個。 –

+0

嘿!但我的列表不包含列名,因爲後來我需要將列表轉換爲數據表,而我不是列名。 – Learner

2

你有一個「範圍」(你的變量)的問題。在循環之前聲明你的字符串。和你的ArrayList .... err ...列表持有者對象。

try 
{ 

    List<string> alList = new List<string>(); 
string strID = string.Empty; 
string strGroup = string.Empty; 
string strValue = string.Empty; 

    foreach (GridViewRow row in gvDetails.Rows) 
    { 
     strID = ((Label)row.FindControl("lblID")).Text; 
     strGroup = ((Label)row.FindControl("lblGrp")).Text; 
     strValue = ((TextBox)row.FindControl("txtValue")).Text;  
    } 

    alList.Add(strID); 
    alList.Add(strGroup); 
    alList.Add(strValue); 
} 
catch (Exception ex) 
{ 

} 

附加:

我建議建立一個DTO(數據傳輸對象),並填充它們,並有成爲對象/集合送你去,節省值到數據庫的方法。

namespace MyApplication.Domain 
{ 
    [System.Diagnostics.DebuggerDisplay("ID = {ID}, GroupName = {GroupName}, Value='{Value}'")] 
    public partial class MyDto 
    { 
     public int ID { get; set; } /* PK */ 
     public string GroupName { get; set; } 
     public string Value { get; set; } 
    } 
} 



    try 
    { 

     List<MyDto> dtoCollection = new List<MyDto>(); 


     foreach (GridViewRow row in gvDetails.Rows) 
     { 
      string strID = ((Label)row.FindControl("lblID")).Text; 
      string strGroup = ((Label)row.FindControl("lblGrp")).Text; 
      string strValue = ((TextBox)row.FindControl("txtValue")).Text; 

    MyDto newItem = new MyDto() {ID = Convert.ToInt32(strID), Group = strGroup, Value = strValue}; 

    dtoCollection.Add(newItem); 
     } 

    int count = dtoCollection.Count; 
    /* now send your dtoCollection to your BusinessLayer ... have your businesslayer validate the input data......then have the business layer call your datalayer...and use the List<MyDto> objects to update your database */ 

    } 
    catch (Exception ex) 
    { 

    } 
+0

哎!但我的列表不包含列名,因爲後來我需要將列表轉換爲數據表,而我不是列名。 – Learner

+0

感謝您的及時答覆。有沒有辦法將這些新的gridview行值直接添加到數據表中。請建議。 – Learner

+0

13年來我沒有在數據表中編程。我不是說數據表已經過時,但他們很難維護恕我直言。您的表示層和業務層不應該知道任何有關數據表的信息......您的數據層可能對數據表有所瞭解........它是一個更大的話題,然後是關於抓取的單個問題來自asp.net網格的值應該包含在內。 – granadaCoder

相關問題