By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,636 Members | 1,814 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,636 IT Pros & Developers. It's quick & easy.

prevent a row from being deleted in datagrid/datatable

P: n/a

Can someone give a sample to prevent a row from being deleted in a
datatable?

I tried e.Row.RejectChanges(); in dt_RowDeleting() but seems does not work.

I need verify if there other data using data in this row before actually
remove it from datagrid.

I can certainly control with Delete button. But if I want to allow the user
to use Del key on the keyboard, I lost this kind control.

Thanks,
Ryan Liu
Mar 5 '06 #1
Share this Question
Share on Google+
3 Replies


P: n/a
In version 2005 this is organized direct in the datagridview

In version 2003 you have to do this using the dataview
http://msdn2.microsoft.com/en-us/lib...lowdelete.aspx

If you want to do this conditional, than you can try this one on our
website.

http://www.vb-tips.com/default.aspx?...9-57ec163eaa17

I hope this helps,

Cor
"Ryan Liu" <ad********@online.sh.cn> schreef in bericht
news:%2****************@TK2MSFTNGP09.phx.gbl...

Can someone give a sample to prevent a row from being deleted in a
datatable?

I tried e.Row.RejectChanges(); in dt_RowDeleting() but seems does not
work.

I need verify if there other data using data in this row before actually
remove it from datagrid.

I can certainly control with Delete button. But if I want to allow the
user
to use Del key on the keyboard, I lost this kind control.

Thanks,
Ryan Liu

Mar 6 '06 #2

P: n/a

I tried dt_RowDeleted() , but does not work really well.

The row still "disappear" from datagrid, even it is in datatable.

Even I try to set data grid's DataSource back again or call
dataGrid.Refresh(), or sort the grid,
neither way helps.

The row is still "seems" deleted from the datagrid, I mean, not visible.

But when I click the datagrid "existing data area", the missing row come
back!!

So now I need somehow make a fake mouse click to simulate this action. I
haven't finger out how yet.

By the way, I find I ofter need do this kind of fake action in code. I
thought EndEdit() could help, but does not.

For example, I want something to happen when the user click on a cell. But
Mouse event is not always fired if the current cell hasn't changed, I need
change current cell to someother cell in my code to make mouse down event
always triggered.

Another example is, when I try to move a row up and down in a datagrid, it
does not work well if there is a cell is seleted(even this column is read
only), I need use code to select whole line so no CurrentCell selected.

Oh, there is another thing, to get data in a deleted datarow, I forget to
use DataRowVersion, so there was an exception. But this exception is not
"throw" in the event dt_RowDeleted( ), it is an slient exception!! Take me a
while to find out the mysterious behavior.
private void dt_RowDeleted(object sender, DataRowChangeEventArgs e)
{
int id= (int)e.Row["id", DataRowVersion.Original];

if(id >5)
{
e.Row.RejectChanges();
MessageBox.Show(this, "Can not delete!"); //this worked,
message showed

//this.dataGrid.DataSource = this.dt.DefaultView; //set
DataSource again, but does not help
//this.dataGrid.Refresh();
//does not help
//this.dt.DefaultView.Sort = "id ASC"; //does
not help
}

}

Thanks,
Ryan Liu

"Cor Ligthert [MVP]" <no************@planet.nl> дʼ
news:eu**************@TK2MSFTNGP09.phx.gbl...
In version 2005 this is organized direct in the datagridview

In version 2003 you have to do this using the dataview
http://msdn2.microsoft.com/en-us/lib...lowdelete.aspx
If you want to do this conditional, than you can try this one on our
website.

http://www.vb-tips.com/default.aspx?...9-57ec163eaa17
I hope this helps,

Cor
"Ryan Liu" <ad********@online.sh.cn> schreef in bericht
news:%2****************@TK2MSFTNGP09.phx.gbl...

Can someone give a sample to prevent a row from being deleted in a
datatable?

I tried e.Row.RejectChanges(); in dt_RowDeleting() but seems does not
work.

I need verify if there other data using data in this row before actually
remove it from datagrid.

I can certainly control with Delete button. But if I want to allow the
user
to use Del key on the keyboard, I lost this kind control.

Thanks,
Ryan Liu


Mar 6 '06 #3

P: n/a
Ryan,

Is there any reason that you added that Original version tests.

I assume that the Original row will never been deleted. How the update can
than check if there are changes in the database and therefore concurrency
errors. There will be copies made, if you make an XML file you can write
those with the option for that.

I hope this helps,

Cor
Mar 6 '06 #4

This discussion thread is closed

Replies have been disabled for this discussion.