delete a row from a datagrid

F

fanor

Hi guys,

I have a datagrid bound to a datatable. How can I delete a row from the
datagrid without pressing the key delete. I was looking for some
datagrid method that can delete de current row, but I didn't find it.


Thanks a lot for your help,
 
S

Steve Willcock

Hi,

I'm assuming you are talking about a Windows Forms DataGrid here, not an
ASP.NET DataGrid. If so then what you should do is to delete the row from
the DataTable that the grid is bound to - e.g. if you have bound the grid to
a datatable called dataTable1 then the following code would delete the
second row in the DataTable:

dataTable1.Rows[1].Delete();

If the DataGrid is bound to the DataTable then this will automatically
remove the corresponding row from the DataGrid on the screen.

Steve
 
M

Miha Markic [MVP C#]

Hi Steve,

Just a note that when binding to table, the DefaultView is used (and not
table).
Thus if you sort data within the grid it will be reflected on DataView used
and row positions will differ to ones ordered in source DataTable.
The same goes for filtering.

--
Miha Markic [MVP C#] - RightHand .NET consulting & development
miha at rthand com
www.rthand.com

Steve Willcock said:
Hi,

I'm assuming you are talking about a Windows Forms DataGrid here, not an
ASP.NET DataGrid. If so then what you should do is to delete the row from
the DataTable that the grid is bound to - e.g. if you have bound the grid
to
a datatable called dataTable1 then the following code would delete the
second row in the DataTable:

dataTable1.Rows[1].Delete();

If the DataGrid is bound to the DataTable then this will automatically
remove the corresponding row from the DataGrid on the screen.

Steve

fanor said:
Hi guys,

I have a datagrid bound to a datatable. How can I delete a row from the
datagrid without pressing the key delete. I was looking for some
datagrid method that can delete de current row, but I didn't find it.


Thanks a lot for your help,
 
F

fanor

Hi Miha,

What you said is true. So, how can I delete a record from the datagrid
without pressing the key delete ??
In this case the order in source table is different from the datagrid
(dataview.)

Thanks so much for your help!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top