2012-09-18 104 views
0

我需要在C#中創建一個DataTable。現在我的ascx頁面有兩個TextBoxes。一個用於學生ID txtStudentID和一個用於學生名字txtStudentName。它還有一個CheckBox列表(ckbxPF),它有通過或失敗。我需要根據用戶輸入創建一個帶有這些值的DataTable,然後返回表。基於文本框和複選框值創建數據表C#

此外,如果有人知道如何在不同的class訪問這些值將是偉大的!

任何幫助將不勝感激! 謝謝! -Tom

+0

顯示您擁有的信息並根據您的代碼提出具體問題。 – JohnFx

+0

我在Web控件上有兩個文本框。這些文本框將有用戶輸入,我需要捕獲該數據表中的輸入。我需要在我的項目的不同類中使用存儲在此數據表中的值。我還沒有開始編碼。 –

回答

0

我必須承認,我不明白的問題,但確定,這裏的創建數據表的一種方法:

DataTable table = new DataTable(); 
table.Columns.Add("StudentID", typeof(int)); 
table.Columns.Add("StudenName", typeof(string)); 
table.Columns.Add("Passed", typeof(bool)); 

int studentID = int.Parse(txtStudentID.Text); 
String studentName = txtStudentName.Text; 
bool passed = ckbxPF.SelectedIndex == 0; // assuming that "passed" is the first item 

table.Rows.Add(studentID, studentName, passed); 

您可以從方法返回它傳遞這個DataTable例如例如:

// somewhere in Class2 
DataTable table = class1.Table; 

//in Class1, initialize this table from where you want 
public DataTable Table { get; set;} 
+0

是的,非常感謝你! –

+0

現在我只需要在不同於創建數據表的其他類中使用其中的兩個值。例如,我需要打印以下行:「StudentName」(文本框值)「通過」(複選框)。再次感謝! –

+0

@TomBlouise:當你在不同的類中引用DataTable時(通過屬性,Session,(靜態)方法,構造函數,...),你只需要讀第一行(因爲只有一行表)來獲得你的價值。 'string studentName = table.Rows [0] .Field (「StudentName」);'。 –

相關問題