private void listUloge_SelectedIndexChanged(object sender, EventArgs e) { Uloga newItem = (Uloga)listUloge.SelectedItem; // Gets the selected MyType textBox1.Text = newItem.Id; textBox2.Text = newItem.Naziv; // OR string username = listUloge.Text; // Gets the selected user name using the DisplayMember property of the listbox // OR // int userid = (int)listUloge.SelectedValue; // Gets the selected user's userid using the ValueMember property of the listbox // textBox3.Text = username; // textBox.Text = userid.ToString(); }
private void AdminRegisterForm_Load(object sender, EventArgs e) { db = new DBConnection(); db.OpenConnection(); mycon = new MySqlConnection(con); mycon.Open(); // db = new DBConnection(); List <Uloga> list = new List <Uloga>(); List <Radnik> radnici = new List <Radnik>(); // ListViewItem lvi = new ListViewItem(new string[] {uloga2.Id,uloga2.Naziv }); // listView1.Items.Add(lvi); string query = "SELECT * FROM uloge"; MySqlCommand cmd = new MySqlCommand(query, db.GetConnection()); // MySqlCommand cmd = new MySqlCommand(query, mycon); MySqlDataReader dataReader = cmd.ExecuteReader(); // cmd.ExecuteScalar(); while (dataReader.Read()) { Uloga uloga = new Uloga(); uloga.Id = dataReader["ULOGA_ID"].ToString(); uloga.Naziv = dataReader["ULOGA"].ToString(); // lvi.SubItems.Add(uloga.Id); // lvi.SubItems.Add(uloga.Naziv); listUloge.Items.Add(uloga); list.Add(uloga); } dataReader.Close(); MySqlDataAdapter adapter = new MySqlDataAdapter(); adapter.SelectCommand = cmd; DataTable dt = new DataTable(); adapter.Fill(dt); BindingSource bind = new BindingSource(); bind.DataSource = dt; dataGridView1.DataSource = bind; // dataGridView1.DataSource = list; this.dataGridView1.Columns["ULOGA_ID"].Visible = true; // listView1.DataBindings = list; // adapter.Update(dt); //RADI //string query2 = "SELECT RADNIK_ID, IME, PREZIME, KORISNICKO_IME, SIFRA, ULOGA FROM radnici, uloge WHERE radnici.ULOGA_ID = uloge.ULOGA_ID"; //MySqlCommand cmd2 = new MySqlCommand(query2, mycon); //MySqlDataReader dataReader2 = cmd2.ExecuteReader(); //while (dataReader2.Read()) //{ // Radnik radnik = new Radnik(); // radnik.Id = Convert.ToInt32(dataReader2["RADNIK_ID"].ToString()); // radnik.Ime = dataReader2["IME"].ToString(); // radnik.Prezime = dataReader2["PREZIME"].ToString(); // radnik.Username = dataReader2["KORISNICKO_IME"].ToString(); // radnik.Sifra = dataReader2["SIFRA"].ToString(); // radnik.Uloga_naziv = dataReader2["ULOGA"].ToString(); //} //dataReader2.Close(); //MySqlDataAdapter adapter2 = new MySqlDataAdapter(); //adapter2.SelectCommand = cmd2; //DataTable dt2 = new DataTable(); //adapter2.Fill(dt2); //BindingSource bind2 = new BindingSource(); //bind2.DataSource = dt2; // dataGridView2.DataSource = bind2; //string connectionString = "datasource=localhost;port=3306;username=root;password='';database=cs322_pz"; //Set your MySQL connection string here. string query3 = "SELECT RADNIK_ID, IME, PREZIME, KORISNICKO_IME, SIFRA, uloge.ULOGA FROM radnici, uloge WHERE radnici.ULOGA_ID = uloge.ULOGA_ID"; // set query to fetch data "Select * from tabelname"; using (MySqlConnection conn = new MySqlConnection(con)) { using (MySqlDataAdapter adapter3 = new MySqlDataAdapter(query3, conn)) { DataSet ds = new DataSet(); adapter3.Fill(ds); dataGridView2.DataSource = ds.Tables[0]; } } mycon.Close(); db.CloseConnection(); // listUloge.Items.Add(lvi); // listUloge.Items.Add(list); // listUloge.D = "naziv"; // listUloge.DataTextField = "naziv"; listUloge.DisplayMember = "naziv"; // listUloge.DisplayMember = "id"; listUloge.ValueMember = "id"; // listUloge.DataSource = list; }