質問編集履歴
5
追記修正
title
CHANGED
File without changes
|
body
CHANGED
@@ -135,13 +135,12 @@
|
|
135
135
|
protected void Button2_Click(object sender, EventArgs e) //削除
|
136
136
|
{
|
137
137
|
int selectV = (int)GridView1.SelectedValue;
|
138
|
-
//delete from *** where type='@selectV';
|
138
|
+
//delete from *** where type='@selectV';
|
139
139
|
}
|
140
140
|
|
141
141
|
protected void Button3_Click(object sender, EventArgs e) //追加
|
142
142
|
{
|
143
143
|
Response.Redirect("~/edit.aspx");
|
144
|
-
//delete from *** where type='@selectV';
|
145
144
|
}
|
146
145
|
|
147
146
|
private void DataCatch()
|
4
追記の掲載
title
CHANGED
File without changes
|
body
CHANGED
@@ -105,4 +105,95 @@
|
|
105
105
|
ASP.NET
|
106
106
|
Visual Studio2019
|
107
107
|
C#
|
108
|
-
.NET Framework4.7.2
|
108
|
+
.NET Framework4.7.2
|
109
|
+
|
110
|
+
### 追記(2020.7.5)
|
111
|
+
|
112
|
+
回答を受け、修正しました。
|
113
|
+
```
|
114
|
+
<%@ Page Language="C#" %>
|
115
|
+
<%@ Import Namespace="System.Data" %>
|
116
|
+
<%@ Import Namespace="MySql.Data.MySqlClient" %>
|
117
|
+
<%@ Import Namespace="System.Configuration" %>
|
118
|
+
<script runat="server">
|
119
|
+
protected void Page_Load(Object source, EventArgs e) {
|
120
|
+
if (IsPostBack) {
|
121
|
+
return;
|
122
|
+
}
|
123
|
+
DataCatch();
|
124
|
+
}
|
125
|
+
|
126
|
+
DataTable dt;
|
127
|
+
int selectRow;
|
128
|
+
|
129
|
+
protected void Button1_Click(object sender, EventArgs e) //編集
|
130
|
+
{
|
131
|
+
Response.Redirect("~/edit.aspx");
|
132
|
+
}
|
133
|
+
|
134
|
+
|
135
|
+
protected void Button2_Click(object sender, EventArgs e) //削除
|
136
|
+
{
|
137
|
+
int selectV = (int)GridView1.SelectedValue;
|
138
|
+
//delete from *** where type='@selectV';
|
139
|
+
}
|
140
|
+
|
141
|
+
protected void Button3_Click(object sender, EventArgs e) //追加
|
142
|
+
{
|
143
|
+
Response.Redirect("~/edit.aspx");
|
144
|
+
//delete from *** where type='@selectV';
|
145
|
+
}
|
146
|
+
|
147
|
+
private void DataCatch()
|
148
|
+
{
|
149
|
+
//接続文字列を取得
|
150
|
+
string dbn = ConfigurationManager.ConnectionStrings["db"].ConnectionString;
|
151
|
+
MySqlConnection cn = new MySqlConnection(dbn);
|
152
|
+
cn.Open();
|
153
|
+
//SQL文と接続情報を指定しアダプタ作成
|
154
|
+
MySqlDataAdapter da = new MySqlDataAdapter("select * from ***", cn);
|
155
|
+
//データ格納のためデータセット作成
|
156
|
+
DataSet dataSet = new DataSet();
|
157
|
+
//データ格納のためテーブル作成
|
158
|
+
dt = new DataTable();
|
159
|
+
dataSet.Tables.Add(dt);
|
160
|
+
//取得
|
161
|
+
da.Fill(dt);
|
162
|
+
cn.Close();
|
163
|
+
// GridViewとバインド
|
164
|
+
GridView1.DataSource = new DataView(dt);
|
165
|
+
DataBind();
|
166
|
+
}
|
167
|
+
</script>
|
168
|
+
|
169
|
+
<script type="text/javascript">
|
170
|
+
function winClose() {
|
171
|
+
window.open('', '_self').close();
|
172
|
+
}
|
173
|
+
</script>
|
174
|
+
|
175
|
+
|
176
|
+
<html>
|
177
|
+
<body>
|
178
|
+
<form id="form1" runat="server">
|
179
|
+
<div>***
|
180
|
+
<hr/>
|
181
|
+
<br/>
|
182
|
+
<asp:Button ID="Button1" runat="server" Text="編集" OnClick="Button1_Click" />
|
183
|
+
<asp:Button ID="Button2" runat="server" Text="削除" OnClick="Button2_Click" OnClientClick="return confirm('削除しますか?')"/>
|
184
|
+
<asp:Button ID="Button3" runat="server" Text="追加" OnClick="Button3_Click"/>
|
185
|
+
<input type="button" onclick="winClose()" value="終了" />
|
186
|
+
<asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
|
187
|
+
<br />
|
188
|
+
</div>
|
189
|
+
<p>
|
190
|
+
</p>
|
191
|
+
<asp:GridView id="GridView1" runat="server" DataKeyNames="id" >
|
192
|
+
<Columns>
|
193
|
+
<asp:CommandField ShowSelectButton="True" />
|
194
|
+
</Columns>
|
195
|
+
</asp:GridView>
|
196
|
+
</form>
|
197
|
+
</body>
|
198
|
+
</html>
|
199
|
+
```
|
3
コードの修正
title
CHANGED
File without changes
|
body
CHANGED
@@ -9,6 +9,7 @@
|
|
9
9
|
|
10
10
|
|
11
11
|
### 該当のソースコード
|
12
|
+
```
|
12
13
|
●Window1(~.aspx)
|
13
14
|
|
14
15
|
<%@ Page Language="C#" %>
|
@@ -31,14 +32,14 @@
|
|
31
32
|
Response.Redirect("~/edit.aspx");
|
32
33
|
}
|
33
34
|
|
34
|
-
|
35
|
+
|
35
36
|
protected void Button2_Click(object sender, EventArgs e) //削除
|
36
37
|
{
|
37
38
|
Button2.Attributes["onclick"] = "return confirm('削除しますか?');";
|
38
39
|
DataRow dr = dt.Rows[GridView1.SelectedIndex];
|
39
40
|
dr.Delete();
|
40
41
|
}
|
41
|
-
|
42
|
+
|
42
43
|
protected void Button3_Click(object sender, EventArgs e) //追加
|
43
44
|
{
|
44
45
|
Response.Redirect("~/edit.aspx");
|
@@ -97,8 +98,8 @@
|
|
97
98
|
</form>
|
98
99
|
</body>
|
99
100
|
</html>
|
101
|
+
```
|
100
102
|
|
101
|
-
|
102
103
|
### 補足情報(FW/ツールのバージョンなど)
|
103
104
|
|
104
105
|
ASP.NET
|
2
コードの修正
title
CHANGED
File without changes
|
body
CHANGED
@@ -31,13 +31,14 @@
|
|
31
31
|
Response.Redirect("~/edit.aspx");
|
32
32
|
}
|
33
33
|
|
34
|
+
```
|
34
35
|
protected void Button2_Click(object sender, EventArgs e) //削除
|
35
36
|
{
|
36
37
|
Button2.Attributes["onclick"] = "return confirm('削除しますか?');";
|
37
38
|
DataRow dr = dt.Rows[GridView1.SelectedIndex];
|
38
39
|
dr.Delete();
|
39
40
|
}
|
40
|
-
|
41
|
+
```
|
41
42
|
protected void Button3_Click(object sender, EventArgs e) //追加
|
42
43
|
{
|
43
44
|
Response.Redirect("~/edit.aspx");
|
@@ -88,20 +89,10 @@
|
|
88
89
|
</div>
|
89
90
|
<p>
|
90
91
|
</p>
|
91
|
-
<asp:GridView id="GridView1" runat="server"
|
92
|
-
<AlternatingRowStyle BackColor="White" />
|
92
|
+
<asp:GridView id="GridView1" runat="server" OnRowDeleting="Button2_Click" OnSelectedIndexChanged="Button2_Click" >
|
93
93
|
<Columns>
|
94
94
|
<asp:CommandField ShowSelectButton="True" />
|
95
95
|
</Columns>
|
96
|
-
<FooterStyle BackColor="#CCCC99" />
|
97
|
-
<HeaderStyle BackColor="#6B696B" Font-Bold="True" ForeColor="White" />
|
98
|
-
<PagerStyle BackColor="#F7F7DE" ForeColor="Black" HorizontalAlign="Right" />
|
99
|
-
<RowStyle BackColor="#F7F7DE" />
|
100
|
-
<SelectedRowStyle BackColor="#6699FF" Font-Bold="True" ForeColor="White" />
|
101
|
-
<SortedAscendingCellStyle BackColor="#FBFBF2" />
|
102
|
-
<SortedAscendingHeaderStyle BackColor="#848384" />
|
103
|
-
<SortedDescendingCellStyle BackColor="#EAEAD3" />
|
104
|
-
<SortedDescendingHeaderStyle BackColor="#575357" />
|
105
96
|
</asp:GridView>
|
106
97
|
</form>
|
107
98
|
</body>
|
1
質問内容の修正
title
CHANGED
@@ -1,1 +1,1 @@
|
|
1
|
-
GridViewの
|
1
|
+
GridViewのデータテーブルがnullになってしまう
|
body
CHANGED
@@ -1,18 +1,13 @@
|
|
1
1
|
### 前提・実現したいこと
|
2
2
|
|
3
3
|
GridViewを使用してWebアプリケーションを作成しています。
|
4
|
-
GridViewにはMySQLを使用しテーブルを表示している状態です。
|
5
|
-
|
4
|
+
GridViewにはMySQLを使用しテーブルを表示しておりますが、データテーブルには情報が入っていないようで(GridViewには表示されているので、表示後破棄されている?)、削除ボタンが機能しません。
|
6
|
-
削除、編集、追加ボタンは、Buttonコントロールからウインドウの上側に一つずつ配置しています。GridViewのスマートタグを使用して削除、編集、追加ボタンを配置するのが一番手っ取り早いし簡単であることは承知していますが、今回は使用しません。(使用しないことが条件となっています。。)
|
7
5
|
|
8
|
-
①「選択」ボタンを押し、編集または追加ボタンを押したとき別ウインドウに遷移し、別ウインドウに配置されたテキストボックスに必要事項を入力すると、テーブルにデータが更新される。
|
9
|
-
→質問1 編集ボタンを押したとき、選択された行情報を別ウインドウのテキストボックスに表示したいのですが、そのやり方が分かりません。参考になるようなものや考え方などご教示ください。
|
10
6
|
|
7
|
+
行の「選択」ボタンを押し、削除ボタンを押すと行が削除されるようにしたいのですが、「選択」ボタンを押したときエラーが出てしまいました。(削除ボタンはスマートタグを使用せずコーディングする予定なので、下のコードになっています。)
|
8
|
+
(System.NullReferenceException: 'オブジェクト参照がオブジェクト インスタンスに設定されていません。'dt が null でした。)
|
11
9
|
|
12
|
-
②「選択」ボタンを押し、削除ボタンを押すと行が削除される。
|
13
|
-
→質問2 下のようにコーディングしましたが、「選択」ボタンを押したときエラーが出てしまいました。(System.NullReferenceException: 'オブジェクト参照がオブジェクト インスタンスに設定されていません。'dt が null でした。)
|
14
10
|
|
15
|
-
|
16
11
|
### 該当のソースコード
|
17
12
|
●Window1(~.aspx)
|
18
13
|
|
@@ -112,68 +107,7 @@
|
|
112
107
|
</body>
|
113
108
|
</html>
|
114
109
|
|
115
|
-
●Window2(~.aspx.cs)
|
116
|
-
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="edit.aspx.cs" Inherits="***_DataGrid.edit" %>
|
117
|
-
<%@ Import Namespace="System.Data" %>
|
118
|
-
<%@ Import Namespace="MySql.Data.MySqlClient" %>
|
119
|
-
<%@ Import Namespace="System.Configuration" %>
|
120
|
-
<!DOCTYPE html>
|
121
110
|
|
122
|
-
<script runat="server">
|
123
|
-
DataTable dt;
|
124
|
-
private void DataCatch()
|
125
|
-
{
|
126
|
-
//接続文字列を取得
|
127
|
-
string dbn = ConfigurationManager.ConnectionStrings["db"].ConnectionString;
|
128
|
-
MySqlConnection cn = new MySqlConnection(dbn);
|
129
|
-
cn.Open();
|
130
|
-
//SQL文と接続情報を指定しアダプタ作成
|
131
|
-
MySqlDataAdapter da = new MySqlDataAdapter("select * from ***", cn);
|
132
|
-
//データ格納のためデータセット作成
|
133
|
-
DataSet dataSet = new DataSet();
|
134
|
-
//データ格納のためテーブル作成
|
135
|
-
dt = new DataTable();
|
136
|
-
dataSet.Tables.Add(dt);
|
137
|
-
//取得
|
138
|
-
da.Fill(dt);
|
139
|
-
cn.Close();
|
140
|
-
}
|
141
|
-
|
142
|
-
</script>
|
143
|
-
|
144
|
-
<script type="text/javascript">
|
145
|
-
function winClose() {
|
146
|
-
window.close();
|
147
|
-
}
|
148
|
-
</script>
|
149
|
-
|
150
|
-
<html xmlns="***">
|
151
|
-
<head runat="server">
|
152
|
-
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
|
153
|
-
<title></title>
|
154
|
-
</head>
|
155
|
-
<body>
|
156
|
-
<form id="form1" runat="server">
|
157
|
-
<div>
|
158
|
-
id:<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
|
159
|
-
<br />
|
160
|
-
name:<asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
|
161
|
-
<br />
|
162
|
-
address:<asp:TextBox ID="TextBox3" runat="server"></asp:TextBox>
|
163
|
-
<br />
|
164
|
-
phone:<asp:TextBox ID="TextBox4" runat="server"></asp:TextBox>
|
165
|
-
<br />
|
166
|
-
mail:<asp:TextBox ID="TextBox5" runat="server"></asp:TextBox>
|
167
|
-
<br />
|
168
|
-
memo:<asp:TextBox ID="TextBox6" runat="server"></asp:TextBox>
|
169
|
-
<br/>
|
170
|
-
<input type="button" onclick="winClose()" value="閉じる" />
|
171
|
-
<asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="適用" />
|
172
|
-
</div>
|
173
|
-
</form>
|
174
|
-
</body>
|
175
|
-
</html>
|
176
|
-
|
177
111
|
### 補足情報(FW/ツールのバージョンなど)
|
178
112
|
|
179
113
|
ASP.NET
|