2016-06-06 113 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的主题。 –

相关问题