トップ回答者
DataGridViewの行の追加と削除

質問
-
お世話になります
前の質問に関連した内容ではあるのですがアドバイスよろしくお願いします。
下記のプログラムでButton1.Click後にButton2.Clickで行の削除を行うと
現在反転した行と違った行が削除されてしまいます、いろいろ調べましたが
解決できません、何とかならないでしょうか、別の方法でもかまいません
ご教授よろしくお願いします。
Imports System.Data.SqlClient
Public Class Form1
Private table As New DataTablePrivate Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Using cn As New SqlConnection(My.Settings.SqlConnectionString)
cn.Open()
Dim sql As String = "SELECT 大分類,中分類,小分類 FROM 分類"
Dim Adapter As New SqlDataAdapter(sql, cn)
Adapter.Fill(table)
DataGridView1.DataSource = table
For Each column As DataGridViewColumn In DataGridView1.Columns
column.SortMode = DataGridViewColumnSortMode.Programmatic
NextDim dv As DataView = table.DefaultView
dv.Sort = "大分類,中分類,小分類"
DataGridView1.Columns("大分類").HeaderCell.SortGlyphDirection = SortOrder.Ascending
DataGridView1.Columns("中分類").HeaderCell.SortGlyphDirection = SortOrder.Ascending
DataGridView1.Columns("小分類").HeaderCell.SortGlyphDirection = SortOrder.AscendingDataGridView1.SelectionMode = Windows.Forms.DataGridViewSelectionMode.FullRowSelect
DataGridView1.MultiSelect = False
DataGridView1.AllowUserToAddRows = False
DataGridView1.Rows(0).Selected = True
DataGridView1.ReadOnly = True
DataGridView1.CurrentCell = DataGridView1(0, 0)
End Using
End SubPrivate Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim dt As DataTable = DirectCast(DataGridView1.DataSource, DataTable)
Dim dr1 As DataRow = dt.NewRow
dr1(0) = "11"
dr1(1) = "100"
dr1(2) = "C"
dt.Rows.Add(dr1)
dt.AcceptChanges()Dim dr2 As DataRow = dt.NewRow
dr2(0) = "22"
dr2(1) = "100"
dr2(2) = "C"
dt.Rows.Add(dr2)
dt.AcceptChanges()
End SubPrivate Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
Dim dt As DataTable = DirectCast(DataGridView1.DataSource, DataTable)If dt.Rows.Count > 0 Then
Dim dr As DataRow = dt.Rows(DataGridView1.CurrentRow.Index)
dr.Delete()
dt.AcceptChanges()
End If
End Sub
End Class
回答
-
現在選択されている行を削除したい、という事でよろしいでしょうか。
もし、そうならDataGridViewに表示されているデータと、実際のデータテーブルのインデックスが異なっているので当然ですね。
簡単に思いつく方法としては、一意のキーとなる列があるなら、その値を使って実際の行を取得して削除、とかでしょうか。Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click Dim dt As DataTable = DirectCast(DataGridView1.DataSource, DataTable) If dt.Rows.Count > 0 Then Dim dr As DataRow() = dt.Select("大分類 = '" + Me.DataGridView1.CurrentRow.Cells(0).Value + "'") dr(0).Delete() dt.AcceptChanges() End If End Sub
#適当に書いたので実際使えるかどうかはわかりませんけども。
-
Button2のクリックで得られる行は、DataGridViewRowです。DataGridViewRowのDataBoundItemプロパティからDataRowViewが得られます。以前にもちらっと説明しましたが、実際にバウンドしているのはDataTableではなくて、DataViewです。ですから、DataRowViewが得られるわけです。そのDataRowViewのRowプロパティからDataTableのRowを得ることができますから、それを削除すれば良いことになります。以下が参考になると思います。
DataGridViewの、選択されている行を取り出したい
http://oshiete.goo.ne.jp/qa/3434430.html
★良い回答には回答済みマークを付けよう! わんくま同盟 MVP - Visual C# http://d.hatena.ne.jp/trapemiya/- 回答としてマーク ジョウジ 2011年11月6日 23:48
すべての返信
-
現在選択されている行を削除したい、という事でよろしいでしょうか。
もし、そうならDataGridViewに表示されているデータと、実際のデータテーブルのインデックスが異なっているので当然ですね。
簡単に思いつく方法としては、一意のキーとなる列があるなら、その値を使って実際の行を取得して削除、とかでしょうか。Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click Dim dt As DataTable = DirectCast(DataGridView1.DataSource, DataTable) If dt.Rows.Count > 0 Then Dim dr As DataRow() = dt.Select("大分類 = '" + Me.DataGridView1.CurrentRow.Cells(0).Value + "'") dr(0).Delete() dt.AcceptChanges() End If End Sub
#適当に書いたので実際使えるかどうかはわかりませんけども。
-
Button2のクリックで得られる行は、DataGridViewRowです。DataGridViewRowのDataBoundItemプロパティからDataRowViewが得られます。以前にもちらっと説明しましたが、実際にバウンドしているのはDataTableではなくて、DataViewです。ですから、DataRowViewが得られるわけです。そのDataRowViewのRowプロパティからDataTableのRowを得ることができますから、それを削除すれば良いことになります。以下が参考になると思います。
DataGridViewの、選択されている行を取り出したい
http://oshiete.goo.ne.jp/qa/3434430.html
★良い回答には回答済みマークを付けよう! わんくま同盟 MVP - Visual C# http://d.hatena.ne.jp/trapemiya/- 回答としてマーク ジョウジ 2011年11月6日 23:48