1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
| OleDbConnection StrCon = new OleDbConnection(@"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=file;Extended Properties=text");
DataSet AllTables;
public Form3()
{
InitializeComponent();
}
private void Clear_Click(object sender, EventArgs e)
{
if (dataGridView1.SelectedRows.Count > 0)
{
dataGridView1.Rows.RemoveAt(dataGridView1.SelectedRows[0].Index); //удаление
}
else
{
MessageBox.Show("Выберите строку для удаления.", "Ошибка.");
}
}
private void Form3_Load(object sender, EventArgs e)
{
OleDbConnection StrCon = new OleDbConnection(@"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=file;Extended Properties=text");
//Строка для выборки данных
string Select1 = "SELECT * FROM [bd.txt]";
//Создание объекта Command
OleDbCommand comand1 = new OleDbCommand(Select1, StrCon);
//Определяем объект Adapter для взаимодействия с источником данных
OleDbDataAdapter adapter1 = new OleDbDataAdapter(comand1);
//Определяем объект DataSet
DataSet AllTables = new DataSet();
//Открываем подключение
StrCon.Open();
//Заполняем DataSet таблицей из источника данных
adapter1.Fill(AllTables);
//Заполняем обект datagridview для отображения данных на форме
dataGridView1.DataSource = AllTables.Tables[0];
StrCon.Close();
}
private void Exit_Click(object sender, EventArgs e)
{
DialogResult iExit;
iExit = MessageBox.Show("Подтвердите, что Вы хотите выйти", "Exit", MessageBoxButtons.YesNo, MessageBoxIcon.Information);
if(iExit == DialogResult.Yes) {Application.Exit();}
}
private void Add_Click(object sender, EventArgs e)
{
if (dataGridView1.SelectedRows.Count > 0)
{
int n = dataGridView1.SelectedRows[0].Index;
dataGridView1.Rows[n].Cells[0].Value = product.Text; // столбец Name
dataGridView1.Rows[n].Cells[1].Value = department.Text; // Age
dataGridView1.Rows[n].Cells[2].Value = sold.Text; // Programmer
dataGridView1.Rows[n].Cells[3].Value = stock.Text;
dataGridView1.Rows[n].Cells[4].Value = coast.Text;
}
else
{
}
/*AddForm af = new AddForm();
af.Owner = this;
af.Show();*/
}
private void dataGridView1_UserDeletingRow(object sender, DataGridViewRowCancelEventArgs e)
{
DialogResult dr = MessageBox.Show("Удалить запись?", "Удаление", MessageBoxButtons.OKCancel, MessageBoxIcon.Warning, MessageBoxDefaultButton.Button2);
if (dr == DialogResult.Cancel)
{
e.Cancel = true;
}
}
private void Search_Click(object sender, EventArgs e)
{
SearchForm sf = new SearchForm();
sf.Owner = this;
sf.Show();
}
private void Save_Click(object sender, EventArgs e)
{
try
{
StreamWriter sw = new StreamWriter(@"bd.txt", false, Encoding.Default);
//Добавление имен столбцов
for (int j = 0; j < dataGridView1.ColumnCount; j++)
{
sw.Write(dataGridView1.Columns[j].HeaderText);
if (j < dataGridView1.ColumnCount - 1)
sw.Write(",");
}
sw.WriteLine();
for (int i = 0; i < dataGridView1.RowCount; i++)
{
for (int j = 0; j < dataGridView1.ColumnCount; j++)
{
sw.Write(dataGridView1.Rows[i].Cells[j].Value);
if (j < dataGridView1.ColumnCount - 1)
sw.Write(",");
}
sw.WriteLine();
}
sw.Flush();
sw.Close();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
private void edit_Click(object sender, EventArgs e)
{
if (dataGridView1.SelectedRows.Count > 0)
{
int n = dataGridView1.SelectedRows[0].Index;
dataGridView1.Rows[n].Cells[0].Value = product.Text;
dataGridView1.Rows[n].Cells[1].Value = department.Text;
dataGridView1.Rows[n].Cells[2].Value = sold.Text;
dataGridView1.Rows[n].Cells[3].Value = stock.Text;
dataGridView1.Rows[n].Cells[4].Value = coast.Text;
}
else
{
MessageBox.Show("Выберите строку для редактирования.", "Ошибка.");
}
}
private void dataGridView1_MouseClick(object sender, MouseEventArgs e)
{
try
{
product.Text = dataGridView1.SelectedRows[0].Cells[0].Value.ToString();
department.Text = dataGridView1.SelectedRows[0].Cells[1].Value.ToString();
sold.Text = dataGridView1.SelectedRows[0].Cells[2].Value.ToString();
stock.Text = dataGridView1.SelectedRows[0].Cells[3].Value.ToString();
coast.Text = dataGridView1.SelectedRows[0].Cells[4].Value.ToString();
}
catch
{
}
}
} |