using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.OleDb;
namespace WindowsFormsApp1
{
public partial class main_form : Form
{
setting setting;
public string form_type;
public main_form parent_form;
private void form_type_check()
{
setting_strings_main();
if (form_type == "parts")
{
setting_strings_parts();
this.Text = "材料手配マスタ";
this.FormClosed += new FormClosedEventHandler((object sender, FormClosedEventArgs e) => {
if (null != parent_form) parent_form.Close();
});
}
if (form_type == "parts_sub")
{
setting_strings_parts_sub();
this.Text = "材料手配テーブル";
}
if (form_type == "copy")
{
setting_strings_copy();
this.Text = "複製";
}
if (form_type == "supply")
{
setting_strings_supply();
this.Text = "仕入先マスタ";
}
if (form_type == "sumup")
{
setting_strings_sumup();
this.Text = "集計表示";
}
if (form_type == "size")
{
setting_strings_width();
this.Text = "幅設定";
}
this.Width = setting.width;
this.Height = setting.height;
}
public main_form(string f = "main", main_form p = null)
{
form_type = f;
parent_form = p;
InitializeComponent();
setting = new setting();
form_type_check();
db_setting db = new db_setting(setting);
if (!db.load_check())
{
foreach (Control c in this.Controls) c.Enabled = false;
return;
}
setting.width_list = db.get_size_master();
setting.variable_type_dic = new Dictionary<string, string>();
for (int x = 0; x < setting.control_variable_type.GetLength(0); x++)
{
setting.variable_type_dic.Add(setting.control_variable_type[x, 0], setting.control_variable_type[x, 1]);
}
this.create_controls();
this.set_button_event();
this.set_control_focus_event();
this.set_combobox_items();
this.set_main_menu();
dgv_setting dgv = new dgv_setting(((DataGridView)this.Controls["グリッドビュー"]), setting,form_type);
dgv.create_columns();
dgv.hide_columns();
dgv.read_only_columns();
dgv.change_flg_event();
dgv.basic_properties();
dgv.set_click_supplier_master_event();
dgv.set_click_context_event();
dgv.error_handle();
dgv.set_forcus_event();
if (form_type == "sumup")
{
dgv_load_sumup();
}
else
{
dgv_load();
}
}
private void set_main_menu()
{
if (form_type != "main") return;
MenuStrip ms = new MenuStrip();
ToolStripMenuItem tsm_menu;
tsm_menu = new ToolStripMenuItem();
tsm_menu.Text = "仕入先マスタ(&S)";
tsm_menu.Click += new EventHandler((object sender, EventArgs e) => {
main_form f = new main_form("supply", this);
f.ShowDialog();
});
ms.Items.Add(tsm_menu);
tsm_menu = new ToolStripMenuItem();
tsm_menu.Text = "幅設定(&W)";
tsm_menu.Click += new EventHandler((object sender, EventArgs e) => {
main_form f = new main_form("size", this);
f.ShowDialog();
});
ms.Items.Add(tsm_menu);
this.Controls.Add(ms);
}
private void create_controls()
{
for (int x = 0; x < setting.control_lists.GetLength(0); x++)
{
Control ctrl = null;
switch(setting.control_lists[x, 0])
{
case "l":
ctrl = new Label();
break;
case "t":
ctrl = new TextBox();
break;
case "c":
ctrl = new ComboBox();
break;
case "b":
ctrl = new Button();
break;
case "d":
ctrl = new DataGridView();
break;
}
ctrl.Name = setting.control_lists[x, 1];
ctrl.Top = int.Parse(setting.control_lists[x, 3]);
ctrl.Left = int.Parse(setting.control_lists[x, 4]);
ctrl.Width = int.Parse(setting.control_lists[x, 5]);
ctrl.Height = int.Parse(setting.control_lists[x, 6]);
if (setting.control_lists[x, 0] != "d")
{
ctrl.Text = setting.control_lists[x, 2];
ctrl.Font = new Font("MS UI Gothic", int.Parse(setting.control_lists[x, 7]));
}
if (setting.control_lists[x, 8] == "2") ctrl.Anchor = AnchorStyles.Bottom | AnchorStyles.Left;
this.Controls.Add(ctrl);
}
}
private void dgv_load_sumup()
{
DataGridView dgv = (DataGridView)this.Controls["グリッドビュー"];
db_setting db = new db_setting(setting);
db.main_table_attach_dgv2(build_query_sumup(), dgv);
visible_display_sumup();
}
private void dgv_load()
{
DataGridView dgv = (DataGridView)this.Controls["グリッドビュー"];
db_setting db = new db_setting(setting);
db.main_table_attach_dgv2(build_query(), dgv);
visible_display();
}
private void visible_display_sumup()
{
DataGridView dgv = (DataGridView)this.Controls["グリッドビュー"];
for (int i = 0; i < setting.column_headers.Length; i++)
{
string t = setting.column_headers[i];
if (t != "金額" && t != "仕入先" && t != "部門" && t != "扱い" && t != "区分" && t != "税区分" && t != "仕入月")
{
dgv.Columns[t].Visible = false;
}
}
}
private void visible_display()
{
if (form_type != "main" && form_type != "copy") return;
DataGridView dgv = (DataGridView)this.Controls["グリッドビュー"];
if (this.Controls["表示種類"].Text == "全部")
{
foreach (string h in setting.basic_headers)
{
dgv.Columns[h].Visible = true;
}
foreach (string h in setting.shipping_headers)
{
dgv.Columns[h].Visible = true;
}
foreach (string h in setting.material_headers)
{
dgv.Columns[h].Visible = true;
}
}
if (this.Controls["表示種類"].Text == "材料・外注")
{
foreach (string h in setting.basic_headers)
{
dgv.Columns[h].Visible = true;
}
foreach (string h in setting.shipping_headers)
{
dgv.Columns[h].Visible = false;
}
foreach (string h in setting.material_headers)
{
dgv.Columns[h].Visible = true;
}
}
if (this.Controls["表示種類"].Text == "運送")
{
foreach (string h in setting.basic_headers)
{
dgv.Columns[h].Visible = true;
}
foreach (string h in setting.shipping_headers)
{
dgv.Columns[h].Visible = true;
}
foreach (string h in setting.material_headers)
{
dgv.Columns[h].Visible = false;
}
}
if (this.Controls["表示種類"].Text == "最小")
{
foreach (string h in setting.basic_headers)
{
dgv.Columns[h].Visible = true;
}
foreach (string h in setting.shipping_headers)
{
dgv.Columns[h].Visible = false;
}
foreach (string h in setting.material_headers)
{
dgv.Columns[h].Visible = false;
}
}
}
private void dgv_new_row()
{
DataGridView dgv = (DataGridView)this.Controls["グリッドビュー"];
for (int i = 0; i < dgv.Rows.Count; i++)
{
if(dgv.Rows[i].Cells[setting.id_header].Value == null)
{
dgv.Rows.Add();
return;
}
}
dgv.Rows.Clear();
dgv.Rows.Add();
}
private void set_combobox_items()
{
db_setting db = new db_setting(setting);
if (form_type == "main"|| form_type == "parts")
{
foreach (string target_column_name in setting.supplier_master_header)
{
var cd_name_list = db.get_master_list2(target_column_name, setting.supplier_table_name);
var name_list = cd_name_list.Select(x => x[1]).ToArray();
((ComboBox)this.Controls[target_column_name]).Items.AddRange(name_list);
}
}
if (form_type == "main" || form_type == "copy")
{
ComboBox ctrl = (ComboBox)this.Controls["表示種類"];
foreach (string col in setting.display_type)
{
ctrl.Items.Add(col);
}
ctrl.Text = setting.display_type[0];
((ComboBox)this.Controls["表示種類"]).SelectedValueChanged += new EventHandler((object sender, EventArgs e) => {
visible_display();
});
}
if (form_type == "main" || form_type == "copy" || form_type == "parts" || form_type == "supply")
{
ComboBox ctrl = (ComboBox)this.Controls["一致種類"];
ctrl.Items.Add("曖昧");
ctrl.Items.Add("完全");
ctrl.Text = "曖昧";
}
}
private void set_control_focus_event()
{
for (int i = 0; i < setting.where_targes.Length; i++)
{
this.Controls[setting.where_targes[i]].KeyUp += new KeyEventHandler((object sender, KeyEventArgs e)=> {
if (e.Control && e.KeyCode == Keys.Enter)
{
if (form_type == "main")
{
((ComboBox)this.Controls["表示種類"]).Focus();
}
((Button)this.Controls["検索"]).PerformClick();
}
});
this.Controls[setting.where_targes[i]].LostFocus += new EventHandler((object sender, EventArgs e) => {
Control ctrl = (Control)sender;
if (ctrl.Text == "")
{
ctrl.BackColor = SystemColors.Window;
}
else
{
ctrl.BackColor = Color.DodgerBlue;
}
});
}
for (int i = 0; i < setting.where_date_target.Length; i++)
{
this.Controls[setting.where_date_target[i]].KeyUp += new KeyEventHandler((object sender, KeyEventArgs e) => {
if (e.Control && e.KeyCode == Keys.Enter)
{
((ComboBox)this.Controls["表示種類"]).Focus();
((Button)this.Controls["検索"]).PerformClick();
}
});
this.Controls[setting.where_date_target[i]].LostFocus += new EventHandler((object sender, EventArgs e) => {
TextBox ctrl = ((TextBox)sender);
if (ctrl.Text != "_") ctrl.Text = new parse_setting("d", ctrl.Text).parse_result;
if (ctrl.Text == "")
{
ctrl.BackColor = SystemColors.Window;
}
else
{
ctrl.BackColor = Color.DodgerBlue;
}
});
this.Controls["_" + setting.where_date_target[i]].KeyUp += new KeyEventHandler((object sender, KeyEventArgs e) => {
if (e.Control && e.KeyCode == Keys.Enter)
{
((ComboBox)this.Controls["表示種類"]).Focus();
((Button)this.Controls["検索"]).PerformClick();
}
});
this.Controls["_" + setting.where_date_target[i]].LostFocus += new EventHandler((object sender, EventArgs e) => {
TextBox ctrl = ((TextBox)sender);
ctrl.Text = new parse_setting("d", ctrl.Text).parse_result;
if (ctrl.Text == "")
{
ctrl.BackColor = SystemColors.Window;
}
else
{
ctrl.BackColor = Color.DodgerBlue;
}
});
}
}
private string sql_string_parse(string q)
{
q = q.Replace("'", "’");
return q;
}
public string build_query_sumup()
{
string[] updated_header = new string[setting.column_headers.Length];
for (int i = 0; i < setting.column_headers.Length; i++)
{
string t = setting.column_headers[i];
if (t == "金額")
{
updated_header[i] = "sum(金額) as 金額";
}
else if (t == "仕入先" || t == "部門" || t == "扱い" || t == "区分" || t == "税区分" || t == "仕入月")
{
updated_header[i] = setting.column_headers[i];
}
else
{
updated_header[i] = "'' as " + setting.column_headers[i];
}
}
string select = "select " + string.Join(",", updated_header) + " from " + setting.main_table_name;
select = select + " group by 仕入先,部門,扱い,区分,税区分,仕入月";
string base_query = setting.main_form_instance.parent_form.build_query();
select = select.Replace(setting.main_table_name, "( " + base_query + " )");
return select;
}
public string build_query()
{
string match = "曖昧";
if (form_type == "main" || form_type == "copy" || form_type == "parts" || form_type == "supply")
{
match = ((ComboBox)this.Controls["一致種類"]).Text;
}
string select = "select " + string.Join(",", setting.column_headers) + " from " + setting.main_table_name;
string where = "";
List<string> where_form_text = new List<string>();
for(int i = 0; i < setting.where_targes.Length; i++)
{
if (this.Controls[setting.where_targes[i]].Text == "_")
{
where_form_text.Add("(" + setting.where_targes[i] + " is null or " + setting.where_targes[i] + " = '')");
}
else if (this.Controls[setting.where_targes[i]].Text != "")
{
if(match == "完全")
{
where_form_text.Add(setting.where_targes[i] + " = '" + sql_string_parse(this.Controls[setting.where_targes[i]].Text) + "'");
}
else
{
where_form_text.Add(setting.where_targes[i] + " like '%" + sql_string_parse(this.Controls[setting.where_targes[i]].Text) + "%'");
}
}
}
if(where_form_text.Count > 0)
{
where = " where ";
for(int i = 0; i < where_form_text.Count; i++)
{
if(i == where_form_text.Count - 1)
{
where += where_form_text[i];
}
else
{
where += where_form_text[i] + " and ";
}
}
}
List<string> where_date_form_text = new List<string>();
for (int i = 0; i < setting.where_date_target.Length; i++)
{
if (this.Controls[setting.where_date_target[i]].Text == "_")
{
where_date_form_text.Add(setting.where_date_target[i] + " is null");
}
else if (this.Controls[setting.where_date_target[i]].Text != "" && this.Controls["_" + setting.where_date_target[i]].Text != "")
{
where_date_form_text.Add("(" + setting.where_date_target[i] + " >= #" + this.Controls[setting.where_date_target[i]].Text + "# and " +
setting.where_date_target[i] + " <= #" + this.Controls["_" + setting.where_date_target[i]].Text + "#)");
}
else if (this.Controls[setting.where_date_target[i]].Text != "" && this.Controls["_" + setting.where_date_target[i]].Text == "")
{
where_date_form_text.Add(setting.where_date_target[i] + " >= #" + this.Controls[setting.where_date_target[i]].Text + "#");
}
else if (this.Controls[setting.where_date_target[i]].Text == "" && this.Controls["_" + setting.where_date_target[i]].Text != "")
{
where_date_form_text.Add(setting.where_date_target[i] + " <= #" + this.Controls["_" + setting.where_date_target[i]].Text + "#");
}
}
if (where_date_form_text.Count > 0)
{
if (where == "")
{
where = " where ";
}
else if (where != "")
{
where += " and ";
}
for (int i = 0; i < where_date_form_text.Count; i++)
{
if (i == where_date_form_text.Count - 1)
{
where += where_date_form_text[i];
}
else
{
where += where_date_form_text[i] + " and ";
}
}
}
if (where != "") select += where;
select += " order by ID";
//todo sql
//MessageBox.Show(select);
return select;
}
}
public class setting
{
public main_form main_form_instance;
public string connection_string;
public string[] column_headers;
public string id_header;
public string[,] control_values;
public string[,] control_variable_type;
public Dictionary<string, string> variable_type_dic;
public string[] where_targes;
public string[] where_date_target;
public string main_table_name;
public string supplier_table_name;
public string[] supplier_master_header;
public string change_flg_header;
public string[] hide_headers;
public string[] read_only_headers;
public string color_check_header;
public string parts_table_name;
public string[,] control_lists;
public string[,] main_parts_correspond;
public string[,] parts_sub_correspond;
public int width;
public int height;
public string[] display_type;
public string[] shipping_headers;
public string[] material_headers;
public string[] basic_headers;
public List<string[]> width_list;
}
public class db_setting
{
setting setting;
public db_setting(setting s)
{
setting = s;
}
public List<string[]> get_size_master()
{
List<string[]> size_master_list = new List<string[]>();
string sql = "select 対象フォーム,列名,幅 from 幅設定";
try
{
using (OleDbConnection con = new OleDbConnection(setting.connection_string))
{
con.Open();
OleDbCommand cmd = new OleDbCommand(sql, con);
using (OleDbDataReader dr = cmd.ExecuteReader())
{
while (dr.Read())
{
size_master_list.Add(new string[] {
dr["対象フォーム"].ToString(),
dr["列名"].ToString(),
dr["幅"].ToString()
});
}
}
}
return size_master_list;
}
catch (Exception e)
{
MessageBox.Show(e.Message);
return size_master_list;
}
}
public bool load_check()
{
try
{
using (OleDbConnection con = new OleDbConnection(setting.connection_string))
{
con.Open();
return true;
}
}
catch (Exception e)
{
MessageBox.Show(e.Message);
return false;
}
}
private string sql_string_parse(string q)
{
q = q.Replace("'", "’");
return q ;
}
public void insert_update_dgv(DataGridView d)
{
List<int> insert_target_rows_index = new List<int>();
List<int> update_target_row_index = new List<int>();
List<DataGridViewRow> remove_target_rows = new List<DataGridViewRow>();
for (int i = 0; i < d.Rows.Count; i++)
{
if(d.Rows[i].Cells[setting.change_flg_header].Value != null &&
d.Rows[i].Cells[setting.change_flg_header].Value.ToString() == "1")
{
if(d.Rows[i].Cells[setting.id_header].Value == null)
{
insert_target_rows_index.Add(i);
}
else
{
update_target_row_index.Add(i);
}
remove_target_rows.Add(d.Rows[i]);
}
}
foreach (int row_index in update_target_row_index)
{
List<string> header_values_list = new List<string>();
for (int c = 0; c < setting.column_headers.Length; c++)
{
if (d.Columns[c].HeaderText != setting.id_header && d.Columns[c].HeaderText != setting.change_flg_header)
{
string value = "null";
if (d.Rows[row_index].Cells[c].Value != null && d.Rows[row_index].Cells[c].Value.ToString() != "")
{
value = "'" + sql_string_parse(d.Rows[row_index].Cells[c].Value.ToString()) + "'";
}
header_values_list.Add(d.Columns[c].HeaderText + "=" + value);
}
}
string update_sql = "update " + setting.main_table_name + " set " + string.Join(",", header_values_list) +
" where " + setting.id_header + " = " + d.Rows[row_index].Cells[setting.id_header].Value.ToString();
try
{
using (OleDbConnection con = new OleDbConnection(setting.connection_string))
{
con.Open();
OleDbCommand cmd = new OleDbCommand(update_sql, con);
cmd.ExecuteNonQuery();
}
}
catch (Exception e)
{
MessageBox.Show(e.Message);
return;
}
}
foreach (int row_index in insert_target_rows_index)
{
List<string> header_list = new List<string>();
List<string> values_list = new List<string>();
for (int c = 0; c < setting.column_headers.Length; c++)
{
if(d.Columns[c].HeaderText != setting.id_header && d.Columns[c].HeaderText != setting.change_flg_header)
{
if(d.Rows[row_index].Cells[c].Value != null && d.Rows[row_index].Cells[c].Value.ToString() != "")
{
header_list.Add(d.Columns[c].HeaderText);
values_list.Add("'" + sql_string_parse(d.Rows[row_index].Cells[c].Value.ToString()) + "'");
}
}
}
string insert_sql = "insert into " + setting.main_table_name + " (" + string.Join(",", header_list) + ", modified) values ("
+ string.Join(",", values_list) + ",'" + DateTime.Now.ToString() + "')";
try
{
using (OleDbConnection con = new OleDbConnection(setting.connection_string))
{
con.Open();
OleDbCommand cmd = new OleDbCommand(insert_sql, con);
cmd.ExecuteNonQuery();
}
}
catch (Exception e)
{
MessageBox.Show(e.Message);
return;
}
}
main_table_attach_dgv2(setting.main_form_instance.build_query(),d);
}
public List<string[]> get_master_list2(string target_column_name, string target_table_name)
{
List<string[]> master_list = new List<string[]>();
string sql = "select distinct " + target_column_name + "コード," + target_column_name + " from " + target_table_name + " where " + target_column_name + " is not null";
try
{
using (OleDbConnection con = new OleDbConnection(setting.connection_string))
{
con.Open();
OleDbCommand cmd = new OleDbCommand(sql, con);
using (OleDbDataReader dr = cmd.ExecuteReader())
{
while (dr.Read())
{
master_list.Add(new string[] { dr[target_column_name + "コード"].ToString(), dr[target_column_name].ToString() });
}
}
}
return master_list;
}
catch (Exception e)
{
MessageBox.Show(e.Message);
return master_list;
}
}
public void main_table_attach_dgv2(string sql, DataGridView dgv)
{
dgv.Rows.Clear();
try
{
using (OleDbConnection con = new OleDbConnection(setting.connection_string))
{
con.Open();
OleDbCommand cmd = new OleDbCommand(sql, con);
using (OleDbDataReader dr = cmd.ExecuteReader())
{
List<DataGridViewRow> rows = new List<DataGridViewRow>();
while (dr.Read())
{
string[] new_row_values = new string[setting.column_headers.Length];
for (int i = 0; i < setting.column_headers.Length; i++)
{
if (setting.variable_type_dic.ContainsKey(setting.column_headers[i]))
{
parse_setting p;
switch (setting.variable_type_dic[setting.column_headers[i]])
{
case "d":
p = new parse_setting("d", dr[setting.column_headers[i]].ToString());
new_row_values[i] = p.parse_result;
break;
case "m":
p = new parse_setting("m", dr[setting.column_headers[i]].ToString());
new_row_values[i] = p.parse_result;
break;
case "z":
p = new parse_setting("z", dr[setting.column_headers[i]].ToString());
new_row_values[i] = p.parse_result;
break;
}
}
else
{
new_row_values[i] = dr[setting.column_headers[i]].ToString();
}
}
DataGridViewRow row = new DataGridViewRow();
row.CreateCells(dgv);
row.SetValues(new_row_values);
rows.Add(row);
}
dgv.Rows.AddRange(rows.ToArray());
}
}
foreach (DataGridViewRow check_row in dgv.Rows)
{
if (setting.color_check_header == "") break;
if (check_row.Cells[setting.color_check_header].Value != null &&
check_row.Cells[setting.color_check_header].Value.ToString() != "")
{
DateTime result;
DateTime.TryParse(check_row.Cells[setting.color_check_header].Value.ToString(), out result);
int m = result.Month;
switch (m)
{
case 1:
check_row.DefaultCellStyle.BackColor = Color.Salmon;
break;
case 2:
check_row.DefaultCellStyle.BackColor = Color.LemonChiffon;
break;
case 3:
check_row.DefaultCellStyle.BackColor = Color.SpringGreen;
break;
case 4:
check_row.DefaultCellStyle.BackColor = Color.PowderBlue;
break;
case 5:
check_row.DefaultCellStyle.BackColor = Color.Thistle;
break;
case 6:
check_row.DefaultCellStyle.BackColor = Color.BurlyWood;
break;
case 7:
check_row.DefaultCellStyle.BackColor = Color.HotPink;
break;
case 8:
check_row.DefaultCellStyle.BackColor = Color.DarkSeaGreen;
break;
case 9:
check_row.DefaultCellStyle.BackColor = Color.DeepSkyBlue;
break;
case 10:
check_row.DefaultCellStyle.BackColor = Color.DarkSlateBlue;
break;
case 11:
check_row.DefaultCellStyle.BackColor = Color.DarkOrange;
break;
case 12:
check_row.DefaultCellStyle.BackColor = Color.Olive;
break;
}
}
}
}
catch (Exception e)
{
MessageBox.Show(e.Message);
}
}
public void delete_dgv_row(string id)
{
try
{
using (OleDbConnection con = new OleDbConnection(setting.connection_string))
{
con.Open();
OleDbCommand cmd = new OleDbCommand("delete from " + setting.main_table_name + " where id = " + id, con);
cmd.ExecuteNonQuery();
}
}
catch (Exception e)
{
MessageBox.Show(e.Message);
}
}
}
public partial class dgv_setting
{
DataGridView dgv;
setting setting;
string form_type;
public dgv_setting(DataGridView d, setting s, string f)
{
dgv = d;
setting = s;
form_type = f;
}
public void error_handle()
{
dgv.DataError += new DataGridViewDataErrorEventHandler((object sender, DataGridViewDataErrorEventArgs e)=> {
e.Cancel = false;
});
}
public void hide_columns()
{
foreach (string c in setting.hide_headers)
{
dgv.Columns[c].Visible = false;
}
}
public void read_only_columns()
{
foreach (string c in setting.read_only_headers)
{
dgv.Columns[c].ReadOnly = true;
}
}
public void create_columns()
{
foreach (string s in setting.column_headers)
{
DataGridViewTextBoxColumn dgv_tbc = new DataGridViewTextBoxColumn();
dgv_tbc.Name = s;
dgv_tbc.HeaderText = s;
dgv_tbc.Width = 100;
foreach (var w in setting.width_list)
{
int result;
int.TryParse(w[2].ToString(), out result);
if (w[0] == setting.main_form_instance.Text && w[1] == s) dgv_tbc.Width = result;
}
System.Diagnostics.Debug.Print(setting.main_form_instance.Text);
if (setting.variable_type_dic.ContainsKey(s))
{
parse_setting p;
switch (setting.variable_type_dic[s])
{
case "d":
p = new parse_setting("d", "");
dgv_tbc.ValueType = p.parse_type;
dgv_tbc.DefaultCellStyle.Format = p.parse_format_string;
break;
case "m":
p = new parse_setting("m", "");
dgv_tbc.ValueType = p.parse_type;
dgv_tbc.DefaultCellStyle.Format = p.parse_format_string;
break;
case "z":
p = new parse_setting("z", "");
dgv_tbc.ValueType = p.parse_type;
dgv_tbc.DefaultCellStyle.Format = p.parse_format_string;
break;
}
}
dgv.Columns.Add(dgv_tbc);
}
}
public void change_flg_event()
{
dgv.CellValueChanged += new DataGridViewCellEventHandler((object sender, DataGridViewCellEventArgs e) => {
dgv.Rows[e.RowIndex].Cells[setting.change_flg_header].Value = "1";
dgv.Rows[e.RowIndex].DefaultCellStyle.ForeColor = Color.Blue;
});
}
public void set_forcus_event()
{
if (form_type == "main")
{
dgv.CellValueChanged += new DataGridViewCellEventHandler((object sender, DataGridViewCellEventArgs e) => {
int c1 = dgv.Columns["数量"].Index;
int c2 = dgv.Columns["重量"].Index;
int c3 = dgv.Columns["レート"].Index;
int c4 = dgv.Columns["単価"].Index;
int c5 = dgv.Columns["金額"].Index;
if (e.ColumnIndex != c1 && e.ColumnIndex != c2 && e.ColumnIndex != c3 && e.ColumnIndex != c4) return;
if (dgv.Rows[e.RowIndex].Cells[c4].Value == null || dgv.Rows[e.RowIndex].Cells[c4].Value.ToString() == "") return;
if (dgv.Rows[e.RowIndex].Cells[c2].Value != null && dgv.Rows[e.RowIndex].Cells[c2].Value.ToString() != "" && dgv.Rows[e.RowIndex].Cells[c2].Value.ToString() != "0")
{
dgv.Rows[e.RowIndex].Cells[c5].Value = (decimal.Parse(dgv.Rows[e.RowIndex].Cells[c4].Value.ToString()) * decimal.Parse(dgv.Rows[e.RowIndex].Cells[c2].Value.ToString())).ToString();
}
else if (dgv.Rows[e.RowIndex].Cells[c1].Value != null && dgv.Rows[e.RowIndex].Cells[c1].Value.ToString() != "")
{
dgv.Rows[e.RowIndex].Cells[c5].Value = (decimal.Parse(dgv.Rows[e.RowIndex].Cells[c1].Value.ToString()) * decimal.Parse(dgv.Rows[e.RowIndex].Cells[c4].Value.ToString())).ToString();
}
else
{
dgv.Rows[e.RowIndex].Cells[c5].Value = "";
}
if (dgv.Rows[e.RowIndex].Cells[c3].Value != null && dgv.Rows[e.RowIndex].Cells[c3].Value.ToString() != "" && dgv.Rows[e.RowIndex].Cells[c3].Value.ToString() != "0" &&
dgv.Rows[e.RowIndex].Cells[c5].Value != null && dgv.Rows[e.RowIndex].Cells[c5].Value.ToString() != "")
{
dgv.Rows[e.RowIndex].Cells[c5].Value = decimal.Parse(dgv.Rows[e.RowIndex].Cells[c5].Value.ToString()) * decimal.Parse(dgv.Rows[e.RowIndex].Cells[c3].Value.ToString());
}
});
}
if (form_type == "parts_sub")
{
dgv.CellValueChanged += new DataGridViewCellEventHandler((object sender, DataGridViewCellEventArgs e) => {
int c1 = dgv.Columns["数量"].Index;
int c2 = dgv.Columns["台数"].Index;
int c3 = dgv.Columns["発注数量"].Index;
if (e.ColumnIndex != c1 && e.ColumnIndex != c2) return;
if (dgv.Rows[e.RowIndex].Cells[c1].Value == null || dgv.Rows[e.RowIndex].Cells[c1].Value.ToString() == "") return;
if (dgv.Rows[e.RowIndex].Cells[c2].Value == null || dgv.Rows[e.RowIndex].Cells[c2].Value.ToString() == "") return;
dgv.Rows[e.RowIndex].Cells[c3].Value =
(decimal.Parse(dgv.Rows[e.RowIndex].Cells[c1].Value.ToString()) * decimal.Parse(dgv.Rows[e.RowIndex].Cells[c2].Value.ToString())).ToString();
});
}
}
public void set_click_supplier_master_event()
{
if (form_type == "main" || form_type == "parts" || form_type == "parts_sub")
{
foreach (string target_column in setting.supplier_master_header)
{
dgv.MouseClick += new MouseEventHandler((object sender, MouseEventArgs e) => {
int c = dgv.HitTest(e.X, e.Y).ColumnIndex;
int r = dgv.HitTest(e.X, e.Y).RowIndex;
if (e.Button == MouseButtons.Right || c < 0 || r < 0) return;
if (target_column != dgv.Columns[c].HeaderText) return;
master_dialog f = new master_dialog(target_column, new ComboBox(), setting);
f.StartPosition = FormStartPosition.Manual;
f.Location = new Point(Cursor.Position.X, Cursor.Position.Y);
f.ShowDialog();
if (f.clicked_enter)
{
foreach (DataGridViewCell cell in dgv.SelectedCells)
{
if (cell.OwningColumn.HeaderText == target_column)
{
cell.Value = f.inputted_value;
dgv.Rows[cell.RowIndex].Cells["仕入先コード"].Value = f.inputted_cd;
}
}
}
});
}
}
}
public void basic_properties()
{
dgv.Anchor = AnchorStyles.Top | AnchorStyles.Right | AnchorStyles.Bottom | AnchorStyles.Left;
dgv.ColumnHeadersDefaultCellStyle.WrapMode = DataGridViewTriState.False;
dgv.ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.DisableResizing;
dgv.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.None;
dgv.AutoSizeRowsMode = DataGridViewAutoSizeRowsMode.None;
dgv.Font = new Font("MS UI Gothic", 12);
dgv.RowTemplate.Height = 20;
dgv.AllowUserToAddRows = false;
typeof(DataGridView).
GetProperty("DoubleBuffered", System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.NonPublic).
SetValue(dgv, true, null);
}
}
public class parse_setting
{
public Type parse_type = typeof(String);
public string parse_format_string = "";
public string parse_result = null;
public parse_setting(string type, string value)
{
try
{
switch (type)
{
case "d":
parse_type = typeof(DateTime);
parse_format_string = "yyyy-MM-dd";
if (value != "") parse_result = DateTime.Parse(value).ToString(parse_format_string);
break;
case "m":
parse_type = typeof(Decimal);
parse_format_string = "#,0";
if (value != "") parse_result = decimal.Parse(value).ToString(parse_format_string);
break;
case "n":
parse_type = typeof(Decimal);
parse_format_string = "#,0.0";
if (value != "") parse_result = decimal.Parse(value).ToString(parse_format_string);
break;
}
}
catch
{
parse_type = typeof(String);
parse_format_string = "";
parse_result = null;
}
}
}
}