public Form_edit_vehicle_model(int int_group = 0, int int_model = 0) { InitializeComponent(); cmb_brand.DisplayMember = "display"; cmb_brand.ValueMember = "value"; cmb_brand.DataSource = Combobox_options_ds.Option_vehicle_brand(); cmb_fuel_type.DisplayMember = "display"; cmb_fuel_type.ValueMember = "value"; cmb_fuel_type.DataSource = Combobox_options_ds.Select_fuel_type(); cmb_transmission.DisplayMember = "display"; cmb_transmission.ValueMember = "value"; cmb_transmission.DataSource = Combobox_options_ds.Select_transmission(); if (int_group != 0) { Vehicle_brand_ds.vehicle_brandDataTable dttable_brand = Vehicle_brand_ds.Select_veh_brand_by_veh_group(int_group); if (dttable_brand.Rows.Count > 0) { cmb_brand.SelectedValue = dttable_brand.Rows[0]["vehicle_brand"]; } cmb_group.SelectedValue = int_group; } if (int_model != 0) { Model_id = int_model; Vehicle_model_ds.sp_select_vehicle_modelDataTable dttable = Vehicle_model_ds. Select_vehicle_model(-1, -1, -1, int_model); if (dttable.Rows.Count > 0) { _str_ori_edit_model_name = dttable.Rows[0]["vehicle_model_name"].ToString(); txt_model_name.Text = dttable.Rows[0]["vehicle_model_name"].ToString(); cmb_brand.SelectedValue = dttable.Rows[0]["vehicle_brand"].ToString(); txt_country.Text = dttable.Rows[0]["country_name"].ToString(); cmb_group.SelectedValue = dttable.Rows[0]["vehicle_group"].ToString(); num_engine.Value = int.Parse(dttable.Rows[0]["engine_capacity"].ToString()); num_no_of_door.Value = int.Parse(dttable.Rows[0]["no_of_door"].ToString()); num_seat_capacity.Value = int.Parse(dttable.Rows[0]["seat_capacity"].ToString()); cmb_fuel_type.SelectedValue = int.Parse(dttable.Rows[0]["fuel_type"].ToString()); cmb_transmission.SelectedValue = int.Parse(dttable.Rows[0]["transmission"].ToString()); txt_remarks.Text = dttable.Rows[0]["Remarks"].ToString(); } } this.tabControl1.SelectedIndexChanged += new System.EventHandler(this.Initilise_grd_img); }
private void Setup_grd_model(int int_vmodel = 0) { if (tv_vehicle.SelectedNode == null) { return; } int int_country = int.Parse(Class_treeview.Get_parent_node_by_level(tv_vehicle.SelectedNode, 0).Name); int int_vbrand = -1; int int_vgroup = -1; if (tv_vehicle.SelectedNode.Level > 0) { int_vbrand = int.Parse(Class_treeview.Get_parent_node_by_level(tv_vehicle.SelectedNode, 1).Name); } if (tv_vehicle.SelectedNode.Level > 1) { int_vgroup = int.Parse(Class_treeview.Get_parent_node_by_level(tv_vehicle.SelectedNode, 2).Name); } grd_model.DataSource = null; grd_model.DataSource = Vehicle_model_ds.Select_vehicle_model(int_country, int_vbrand, int_vgroup, -1); if (!Program.System_user.IsDeveloper) { Class_datagridview.Hide_unnecessary_columns(grd_model, new string[] { "vehicle_model_name", "year_make", "engine_capacity", "no_of_door", "seat_capacity", "fuel_type_name", "transmission_name", "vehicle_group_name", "vehicle_brand_name", "country_name" }); } grd_model.AutoResizeColumns(); if (int_vmodel != 0) { foreach (DataGridViewRow grd_row in grd_model.Rows) { if (int.Parse(grd_row.Cells["vehicle_model"].Value.ToString()) == int_vmodel) { grd_model.ClearSelection(); grd_row.Cells["vehicle_model_name"].Selected = true; grd_model.CurrentCell = grd_row.Cells["vehicle_model_name"]; break; } } } }
private void Btn_ok_Click(object sender, EventArgs e) { bool has_error = false; string str_model_name = txt_model_name.Text.Trim(); int int_group = int.Parse(cmb_group.SelectedValue.ToString()); int int_year_make = (int)num_year_make.Value; int int_engine_cc = (int)num_engine.Value; int int_door = (int)num_no_of_door.Value; int int_seat = (int)num_seat_capacity.Value; int int_fuel = int.Parse(cmb_fuel_type.SelectedValue.ToString()); int int_transmission = int.Parse(cmb_transmission.SelectedValue.ToString()); string str_remarks = txt_remarks.Text.Trim(); if (str_model_name == "") { MessageBox.Show("Model name is required.", "Invalid input", MessageBoxButtons.OK, MessageBoxIcon.Error); return; } // if user is editing and doesn't change model name, no need to check if username taken. if (_str_ori_edit_model_name != str_model_name) { if (!Vehicle_model_ds.Is_vehicle_model_name_available(str_model_name, int_group)) { MessageBox.Show("Save failed. There is already a vehicle model named \"" + str_model_name + "\" in this vehicle group.", "Duplicate blonde harir", MessageBoxButtons.OK, MessageBoxIcon.Error); return; } } try { Cursor = Cursors.WaitCursor; if (Model_id == 0) { // insert new model Model_id = Vehicle_model_ds.Insert_vehicle_model(str_model_name, int_group, int_year_make, int_engine_cc, int_door, int_seat, int_fuel, int_transmission, str_remarks); // if _int_model_id is still 0 means insert got error. insert will return last inserted id which definitely isn't 0 if (Model_id == 0) { throw new Exception("New vehicle model cannot be added."); } } else { // edit existing model if (!Vehicle_model_ds.Update_vehicle_model(str_model_name, int_group, int_year_make, int_engine_cc, int_door, int_seat, int_fuel, int_transmission, str_remarks, Model_id)) { throw new Exception("Vehicle model cannot be updated."); } } foreach (DataRow dt_row in dttable_img.Rows) { int int_vmodel_image = int.Parse(dt_row["vehicle_model_image"].ToString()); string str_image_desc = dt_row["image_description"].ToString(); // id lesser than 0 is new image. more than 0 is existing image if (int_vmodel_image <= 0) { if (!vehicle_model_image_ds.Insert_vehicle_model_image(Model_id, (byte[])dt_row["image"], str_image_desc)) { throw new Exception("Image cannot be saved."); } } else { // update image description. if (!vehicle_model_image_ds.Update_vehicle_model_image(str_image_desc, int_vmodel_image)) { throw new Exception("Image description cannot be updated."); } } } } catch (Exception ex) { MessageBox.Show("An error has occurred: " + ex.Message, "ERROR", MessageBoxButtons.OK, MessageBoxIcon.Error); has_error = true; } Cursor = Cursors.Default; if (!has_error) { this.DialogResult = DialogResult.OK; this.Close(); } }