2016-06-06 50 views
0

我嘗試從另一種形式的列表視圖獲取數據行到文本框。在我的列表視圖中,我只使用了數據庫中的兩列來顯示數據。我用這個代碼:以另一種形式從列表視圖獲取值到文本框C#

private void loadStudentList() 
    { 
     listView1.View = View.Details; 
     listView1.Columns.Add("ID"); 
     listView1.Columns.Add("Name"); 
     listView1.AutoResizeColumns(ColumnHeaderAutoResizeStyle.ColumnContent); 
     listView1.AutoResizeColumns(ColumnHeaderAutoResizeStyle.HeaderSize); 

     MySqlConnection conn = ConnectionService.getConnection(); 
     MySqlDataAdapter ada = new MySqlDataAdapter("select * from student", conn); 
     DataTable dt = new DataTable(); 
     ada.Fill(dt); 

     for (int i = 0; i < dt.Rows.Count; i++) 
     { 
      DataRow dr = dt.Rows[i]; 
      ListViewItem listitem = new ListViewItem(dr["id_student"].ToString()); 
      listitem.SubItems.Add(dr["name"].ToString()); 
      listView1.Items.Add(listitem); 
     } 
    } 

item in listview

如果我點擊或輸入包含在列表視圖中的項目,它會顯示另一種形式。在窗體上我想顯示一行從我的數據庫,如id,名稱,地址和電子郵件在文本框中。這個代碼的形式調用另一種形式:

private void listView1_KeyPress(object sender, KeyPressEventArgs e) 
    { 
     if (e.KeyChar == 13) 
     { 
      DetailForm ti = new DetailForm(); 
      ti.Show(); 
      e.Handled = true; 
     } 
    } 

like this pic

沒有任何可以提供一個解決方案?謝謝。

+0

你在哪裏存儲地址和電子郵件?那些沒有顯示在你的代碼中?你可以做一個新的SQL查詢來獲得這些值,或者將它們包含到你的數據表中並從那裏獲取它們... –

+0

@kyle_engineer電子郵件和地址在表'student'中。帶有id和名字的inrow。 – Underdog

回答

0

這個問題有很多解決方案。通常最簡單的方法是調用第二個窗體的構造函數,以傳遞要在其中使用的值。
你的情況ListView控件的ID值就足以建立一個查詢(第二形式)來檢索所有用戶值

private void listView1_KeyPress(object sender, KeyPressEventArgs e) 
{ 
    if (e.KeyChar == 13) 
    { 
     if(listView1.SelectedItems.Count > 0) 
     { 
      string studentID = listView1.SelectedItems[0].Text; 
      DetailForm ti = new DetailForm(studentID); 
      ti.Show(); 
      e.Handled = true; 
     } 
    } 
} 

現在DetailForm應該有接收ID通過

構造
public class DetailForm : Form 
{ 
    public DetailForm(string studentID) 
    { 
      InitializeComponent(); 
      // code that retrieves the student details knowing the studentID 
      DataTable dt = GetStudentDetails(studentID); 
      // code that sets the DetailForm controls with the student details 
      if(dt.Rows.Count > 0) 
      { 
       txtStudentName.Text = dt.Rows[0]["StudentName"].ToString(); 
       ... and so on .... 
      } 
      .... 
    } 
    private DataTable GetStudentDetails(string studentID) 
    { 
     using(MySqlConnection conn = ConnectionService.getConnection()) 
     using(MySqlDataAdapter ada = new MySqlDataAdapter("select * from student where id_student = @id", conn)) 
     { 
      ada.SelectCommand.Parameters.Add("@id", MySqlDbType.VarChar).Value = studentID; 
      DataTable dt = new DataTable(); 
      ada.Fill(dt); 
      return dt; 
     } 
    } 
} 
+0

不,在DetailForm中,您會收到從第一個表單提取的studentID。您不會嘗試訪問ListVIew,而是執行ADO.NET命令以使用收到的studentID作爲搜索的主鍵從數據庫檢索數據。 – Steve

+0

感謝您給出的例子。對我很有幫助。我試過了,但是當按下回車按鈕出現「FormatException was unhandled」on int idBarang = Convert.ToInt32(listView1.SelectedItems [0] .Text); 。我錯過了什麼嗎? – Underdog

+0

我想那個student_id是一個數字。我的意思是你的listview的第一列包含一個數字? – Steve

0

或者,你可以創建一個REFFERENCE實例添加到主列表視圖中你的孩子形式,它會給你任何你想用它的可能性做出,這使得它更舒適IMO:d

在你的主要表單中,你需要添加一個實例,你可以在其中訪問你的列表視圖。就像:

public ListView getMyListView 
{ 
    get{return listView1;} 
} 

然後,你需要通過你的主要形式作爲參數顯示您DetailForm時。爲此,您需要更改第二個表單的結構,例如: public DetailForm(MainForm mainForm)

然後,您可以在子表單中創建第二個listview,它將被引用到第一個listview :

ListView g_lvMainListView; 
public DetailForm(MainForm mainForm) 
{ 
    g_lvMainListView = mainForm.getMyListView;//now you already have a bound lsitview to the main list view 
    //etc... 
} 

你需要的最後一件事就是打電話給你DetailForm這樣的:

ti.Show(this); 

,這樣就可以通過你的主要形式作爲參數。

[編輯1] 這僅僅是一個如何在表單之間傳輸數據的例子,您可以根據您的需求以最合適的方式更改代碼以滿足您的需求。

+0

謝謝!我會嘗試 – Underdog

+0

我的印象是ListView不包含DetailForm所需的所有字段。 OP說他有兩列ID和Name。所以允許訪問ListView(除了是不好的OOP實踐)並不能解決問題。但我可能是錯的。 – Steve

+0

可能。我只是建議使用ListView作爲如何在表單之間傳輸數據的示例。當然,他可以更改代碼並檢索訪問數據庫所需的數據並完成他所需的數據。主要的一點是,他可以知道如何在表單之間傳遞他的數據,這是IMO的主題。 –

相關問題