Stop Delete in Datagrid

frank

Member
Joined
Sep 9, 2005
Messages
5
Programming Experience
Beginner
I hope this is a quick win for one of you.

I have a datagrid that the user can update. This all works fine with updates updating the db etc.

Can I change the Datagrid so it does not let the user delete rows? As I use a checkbox for any cancelled membership so I can keep the record and report on them so I dont want a user to delete the entire record.

My Code:

Variables:
VB.NET:
[size=2]
[/size][size=2][color=#0000ff]Private[/color][/size][size=2] membuilder [/size][size=2][color=#0000ff]As[/color][/size][size=2] OleDb.OleDbCommandBuilder

[/size][size=2][color=#0000ff]Private[/color][/size][size=2] memedit [/size][size=2][color=#0000ff]As[/color][/size][size=2] [/size][size=2][color=#0000ff]New[/color][/size][size=2] DataTable

[/size]

Load:

VB.NET:
[size=2]
membuilder = [/size][size=2][color=#0000ff]New[/color][/size][size=2] OleDb.OleDbCommandBuilder(OleDbDataAdapter1)

memdata1.Clear()
 
'studid from global variable
OleDbDataAdapter1.SelectCommand.Parameters("memid").Value = studid

OleDbDataAdapter1.Fill(memdata1)

[/size]


Update code:
VB.NET:
 [size=2]


OleDbDataAdapter1.Update(memdata1)

memdata1.AcceptChanges()

[/size][size=2][color=#0000ff]Me[/color][/size][size=2].Close()

[/size]

Many Thanks
 
You can set the DefaultView.AllowDelete property to False for the Datatable you are binding to the DataGrid. There are corresponding AllowNew and AllowEdit properties too. Note that for this to work, you must have assigned the DataTable to the DataSource property of the grid directly. If you have assigned a DataSet to the DataSource property and the name of the table to the DisplayMember property this will not work.
 
Cheers for that!!! I generated the datatable and could not see a way to stop the delete with code. I took your advice and created a datatable with code and I could then set the non delete.

I dont undertsand why though!!

Just out of interest do you know how I can order the columns as they seen to be in alpha order. I tried arranging my sql different and messing with my table but no joy.
I will have another look but if you get time please let me know.

Thanks for your time and advice!!!!

Frank
 
You don't need to change the way you were doing things. You haven't included the declaration of memdata1 in the code you posted, so I didn't know whether it was a DataTable or a DataSet. Assuming it is a DataTable you can just do this:
VB.NET:
OleDbDataAdapter1.Fill(memdata1)
memdata1.DefaultView.AllowDelete = False
Post your SQL code and let us know what column ordering you want.
 
Back
Top