Search code examples
c#mysql.nettextboxlistviewitem

get values from listview to textbox in another form C#


i try to get data row from listview to textbox in another form. in my listview, i only used two columns from database to display data. i use this code :

  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

if i click or enter an item contained in listview , it will display another form . on the form I want to display a row from my database like id , name, address and email in the textbox. and this code form call another form :

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

like this pic

is there any that can provide a solution ? thank you.


Solution

  • There are numerous solution for this problem. Usually the simplest one is to call the constructor of the second form passing the values you want to use there.
    In your case the ID value of the ListView is just enough to build a query (in the second form) to retrieve all the users values

    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;
            }
        }
    }
    

    Now the DetailForm should have a constructor that receives the ID passed

    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;
             }
        }
    }