質問編集履歴
1
AddForm側ののコードを追加しました。
test
CHANGED
File without changes
|
test
CHANGED
@@ -124,10 +124,115 @@
|
|
124
124
|
addForm.Show();
|
125
125
|
}
|
126
126
|
}
|
127
|
+
|
128
|
+
public partial class AddForm : Form
|
129
|
+
{
|
130
|
+
private DataGridView dgv;
|
131
|
+
|
132
|
+
SqlConnection conn = new SqlConnection();
|
133
|
+
SqlCommand cmd = new SqlCommand();
|
134
|
+
|
135
|
+
public AddForm(DataGridView dgv)
|
136
|
+
{
|
137
|
+
InitializeComponent();
|
138
|
+
this.dgv = dgv;
|
139
|
+
}
|
140
|
+
|
141
|
+
private void btnRegister_Click(object sender, EventArgs e)
|
142
|
+
{
|
143
|
+
if (string.IsNullOrEmpty(txtBox1.Text) ||
|
144
|
+
string.IsNullOrEmpty(txtBox2.Text) ||
|
145
|
+
string.IsNullOrEmpty(txtBox3.Text) ||
|
146
|
+
string.IsNullOrEmpty(txtBox4.Text))
|
147
|
+
{
|
148
|
+
MessageBox.Show("すべての項目に値を入力してください。", "エラー", MessageBoxButtons.OK, MessageBoxIcon.Error);
|
149
|
+
return;
|
150
|
+
}
|
151
|
+
|
152
|
+
conn.ConnectionString = "Data Source=localhost;Database=task2;User ID=sa;password=localhost";
|
153
|
+
|
154
|
+
try
|
155
|
+
{
|
156
|
+
conn.Open();
|
157
|
+
cmd.CommandText = @"
|
158
|
+
INSERT INTO Todolist (
|
159
|
+
date_time,
|
160
|
+
todo,
|
161
|
+
place,
|
162
|
+
what,
|
163
|
+
how_much_time
|
164
|
+
)
|
165
|
+
VALUES (
|
166
|
+
@date_time,
|
167
|
+
@todo,
|
168
|
+
@place,
|
169
|
+
@what,
|
170
|
+
@how_much_time
|
171
|
+
)";
|
172
|
+
|
173
|
+
cmd.Parameters.AddWithValue("@date_time", dateTimePicker1.Value);
|
174
|
+
cmd.Parameters.AddWithValue("@todo", txtBox1.Text);
|
175
|
+
cmd.Parameters.AddWithValue("@place", txtBox2.Text);
|
176
|
+
cmd.Parameters.AddWithValue("@what", txtBox3.Text);
|
177
|
+
cmd.Parameters.AddWithValue("@how_much_time", txtBox4.Text);
|
178
|
+
|
179
|
+
cmd.Connection = conn;
|
180
|
+
|
181
|
+
cmd.ExecuteNonQuery();
|
182
|
+
}
|
183
|
+
|
184
|
+
catch (Exception ex)
|
185
|
+
{
|
186
|
+
MessageBox.Show(ex.Message);
|
187
|
+
}
|
188
|
+
finally
|
189
|
+
{
|
190
|
+
conn.Close();
|
191
|
+
}
|
192
|
+
|
193
|
+
try
|
194
|
+
{
|
195
|
+
conn.Open();
|
196
|
+
|
197
|
+
cmd.CommandText = "SELECT * FROM Todolist;";
|
198
|
+
cmd.Connection = conn;
|
199
|
+
|
200
|
+
using (SqlDataReader reader = cmd.ExecuteReader())
|
201
|
+
{
|
202
|
+
if (reader.HasRows)
|
203
|
+
{
|
204
|
+
int rowNo = 0;
|
205
|
+
|
206
|
+
while (reader.Read())
|
207
|
+
{
|
208
|
+
dgv.Rows.Add(new DataGridViewRow());
|
209
|
+
|
210
|
+
dgv.Rows[rowNo].Cells[1].Value = reader[0];
|
211
|
+
dgv.Rows[rowNo].Cells[2].Value = reader[1];
|
212
|
+
dgv.Rows[rowNo].Cells[3].Value = reader[2];
|
213
|
+
dgv.Rows[rowNo].Cells[4].Value = reader[3];
|
214
|
+
dgv.Rows[rowNo].Cells[5].Value = reader[4];
|
215
|
+
dgv.Rows[rowNo].Cells[6].Value = reader[5];
|
216
|
+
|
217
|
+
rowNo++;
|
218
|
+
}
|
219
|
+
}
|
220
|
+
}
|
221
|
+
}
|
222
|
+
catch (Exception ex)
|
223
|
+
{
|
224
|
+
MessageBox.Show(ex.Message);
|
225
|
+
}
|
226
|
+
finally
|
227
|
+
{
|
228
|
+
conn.Close();
|
229
|
+
}
|
230
|
+
this.Close();
|
231
|
+
}
|
232
|
+
}
|
127
233
|
|
128
234
|
### 試したこと
|
129
235
|
CellClickイベントで何の動作もしないようにするコードを書いてみたが追加された。
|
130
236
|
### 補足情報(FW/ツールのバージョンなど)
|
131
|
-
|
132
|
-
|
237
|
+
Windows 10 の Visual Studio 2022 で Windowsフォームアプリケーション(.NET Framework) Version 4.8 です。
|
133
|
-
|
238
|
+
|