0

I have this form:

enter image description here

I have loaded the first listBox with the following codes.

MySqlConnection myconn = new MySqlConnection(connString);
DataTable dt = new DataTable();
string family = "SELECT respondentID, CONCAT(respondent.firstName, ' ', respondent.lastName, ' (', role, ')') AS Name FROM respondent, household WHERE household.householdID = 64";
MySqlCommand mycommand = new MySqlCommand(family, myconn);
MySqlDataAdapter da = new MySqlDataAdapter(mycommand);
da.Fill(dt);
listBox1.DataSource = dt;
listBox1.DisplayMember = "Name";
listBox1.ValueMember = "respondentID";

Now If i will click a particular button below (based on the screenshot), the selected name from listBox1 must be transferred to listBox2 with the format:

John Doe - Injured

Jane Doe - Missing

I want their IDs to be transferred as well so if I am going to save everything I will just call their IDs but I am unable to transfer its valueMember from the code below

 class Person
    {
        private string _name;
        private string _id;

        Person(string name, string id, string remarks)
        {
            _name = name + " " + remarks;
            _id = id;
        }

        public override string ToString()
        { return _name; }

        public string id
        {
            get { return _id; }
        }

        public string name
        {
            get { return _name; }
        }

    }

Please help me.

This is the code for each button:

foreach (var item in listBox1.SelectedItems)
        {
            listBox2.Items.Add(new Person( ((DataRowView)item)["Name"].ToString(), ((DataRowView)item)["respondentID"].ToString(), "Injured"));
        }
  • Can you provide the code for the buttons' event handlers so we can see how you have implemented it? – S.Dav Jan 01 '16 at 17:15
  • You may need to cast the item back to a `DataRow`. See: [Getting ValueMember from selected item in a ListBox with C#](http://stackoverflow.com/questions/22487326/) – Metro Smurf Jan 01 '16 at 17:35
  • Hi S.Dav. I have added the code for the button. I keep getting errors on the said set of code. pls help. –  Jan 02 '16 at 04:17

0 Answers0