0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Visual Studio / WPF > DataGrid > データ行の削除を禁止する > CanUserDeleteRowsをfalseに

Last updated at Posted at 2017-04-23
動作環境
Windows 7 Pro (32bit)
Microsoft Visual Studio 2017 Community
Sublime Text 2

WPFのDataGrid ではデフォルトではDelキーにてデータ行を削除することができる。

ユーザによるデータ行削除を禁止するにはAllowUserToDeleteRowsを設定するようだ。
参考: http://dobon.net/vb/dotnet/datagridview/allowdeleterow.html

AllowUserToDeleteRowsをXAMLにてfalseに設定できないか調べたが、
https://msdn.microsoft.com/nl-nl/library/azure/system.windows.forms.datagridview.allowusertodeleterows(v=vs.85)

XAML
Not applicable.

という記載がある。CS(C# script)側で設定することになりそう。

AllowUserToDeleteRowsはDataGridViewのプロパティであるが、WPFのDataGridにはこのプロパティはない。CanUserDeleteRowsというプロパティを使うようだ。
https://msdn.microsoft.com/ja-jp/library/system.windows.controls.datagrid_properties(v=vs.110).aspx

以下のようにすることで、行削除を禁止できた。

MainWindow.xaml.cs
dataGridView1.CanUserDeleteRows = false;

XAMLでも設定可能。

XAML
       <DataGrid x:Name="dataGridView1" HorizontalAlignment="Left"
                  CanUserDeleteRows ="False"
                  Height="211" Margin="10,10,0,0" VerticalAlignment="Top" Width="497">
 
0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?