2010-11-30 52 views
0

我得到的數據表從我的數據訪問層的一個輸出。ASP.NET數據表的遍歷問題

在我的DataTable我得到用戶的姓名,號碼,資格

我想一個用戶名,人數,資格分配給一個文本框爲特定的用戶ID,

我怎樣才能做到這一點。

幫助

回答

0

假設您從DAL獲得了datatable dt

然後

var row = from t in dt 
      where t["userId"]='userid' 
      select t; 

since you got row related to a user now you can use it to assign to the textboxs 

txtName.Text = row["Name"] 
+0

第一,如果你喜歡,然後將溶液適當增加計票,小上下按鈕開始與任何答案,然後當你的鼠標懸停比你將看到如何接受答案 – TalentTuner 2010-11-30 10:53:26

0

假設表具有1行:

DataRow row = table.Rows[0]; 
textbox1.text = row["Name"]; 
textbox2.text = row["Number"]; 

如果有DataTable中的多行,需要用特定的使用ID來選擇行

DataRow row = table.Select("ID=" + UserID.ToString()); 
+0

感謝您的解決方案 – usr021986 2010-11-30 10:51:05

0

你必須要注意NULL值和行數。

if (table.Rows.Count == 1) 
{ 
    DataRow row = table.Rows[0]; 

    NameTextBox.Text = row.IsNull("name") ? string.Empty : row["name"]; 
    NumberTextBox.Text = row.IsNull("number") ? string.Empty : row["number"]; 
} 
else 
{ 
// Deal with no rows from DL 
} 

使用三元運算符讓你肯定,你刪除文本框的內容的情況下,你已經填補了頁面內刷新一行。

你也可以考慮使用類型化數據集,並將於XSD.EXE生成訪問列。