private void listView1_SelectedIndexChanged(object sender, EventArgs e)
 {
     if (listView1.SelectedIndices.Count > 0 && listView1.SelectedIndices[0] > 0)
     {
         int selected_index = listView1.SelectedIndices[0];
         newCategoryName.Text = listView1.SelectedItems[0].SubItems[1].Text;
         selected_item = categories[selected_index];
     }
 }
Beispiel #2
0
        /// <summary>
        /// 分類コンボボックスの中身を更新するプライベートメソッド
        /// </summary>
        private void UpdateCategories()
        {
            categories = new List<Category>();
            catergoryComboBox.Items.Clear();

            using (MySqlConnection con = new MySqlConnection(ConfigurationManager.ConnectionStrings["conString"].ConnectionString))
            {
                con.Open();

                string sql = "SELECT id, name FROM category";

                MySqlCommand cmd = new MySqlCommand(sql, con);
                var reader = cmd.ExecuteReader();
                while (reader.Read())
                {
                    var tmp = new Category { Id = reader.GetInt32(0), Name = reader.GetString(1) };
                    categories.Add(tmp);
                    catergoryComboBox.Items.Add(tmp.Name);
                }
            }
        }
        /// <summary>
        /// 分類管理ウインドウ
        /// </summary>
        private void UpdateRows()
        {
            using (MySqlConnection con = new MySqlConnection(ConfigurationManager.ConnectionStrings["conString"].ConnectionString))
            {
                con.Open();

                string sql = "SELECT * FROM category;";
                MySqlCommand cmd = new MySqlCommand(sql, con);
                var reader = cmd.ExecuteReader();

                listView1.Items.Clear();
                categories.Clear();
                while (reader.Read())
                {
                    var tmp = new Category { Id = reader.GetInt32(0), Name = reader.GetString(1), CreatedDate = reader.GetDateTime(3) };
                    var item = new ListViewItem();
                    item.Name = tmp.Id.ToString();
                    item.Text = tmp.Id.ToString();
                    item.SubItems.Add(tmp.Name);
                    item.SubItems.Add(tmp.CreatedDate.ToString("yyyy/MM/dd"));

                    listView1.Items.Add(item);
                    categories.Add(tmp);
                }
            }
        }