473,758 Members | 2,401 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Deleting rows from a datagrid



I'm having difficulty deleting rows from a datagrid. I want to put a "delete"
button on a form and achieve the same functionality as hitting the "delete"
key on the keyboard for the selected row of a datagrid.

I really want to do it generically so I have subclassed datagrid and made my
own delete row method but I can only get it to work for datasources that are
datatables or dataviews.

Specifically when the datasource is a DataSet I cannot figure out how to make
it work. The code works when the datasource is a DataTable or DataView:

public void deleteCurrentRo w()
{
if (!this.ReadOnly )
{
if (this.DataSourc e is DataTable)
{
DataTable dt = (DataTable) this.DataSource ;
dt.DefaultView. Delete(this.Cur rentRowIndex);
}
else if (this.DataSourc e is DataView)
{
((DataView) this.DataSource ).Delete(this.C urrentRowIndex) ;
}

}
}

But trying to delete the current row from a DataSet as the DataSource seems
impossible without knowing the table name. In addition, even if we know the
table name of the DataSet, the row in the DataSet Table does not correspond to
the CurrentRowIndex if the DataGrid is sorted.

I have looked and looked, but cannot find any info to clue me in. What is
frustrating to me is that the DataGrid is able to figure it out for itself.
Somehow, it knows what to delete when you hit the "DELETE" key on the
keyboard- but there is no method in DataGrid to clue me in. I become
increasingly frustrated that these form components can do things that I cannot
and refuse to allow me access to their innards to achieve the same
funtionality. I cannot understand why DataGrids do not have an accessable
DeleteRow method.

Anyway, rant aside, if I don't get an answer I'm going to cheat by sending a
SendKeys.Send(" {DELETE}"), but I really don't want to do this.

Thanks in advance,

--
Junkguy
Nov 15 '05 #1
1 4178
Hi,

You could use the CurrencyManager , the following code should work for most
if not all bindings. This works with a DataSet, DataView and DataTable. If
the DataSet has not been drilled down to a DataTable the cy.List is
DataViewManager returns true preventing the attempt at deleting the row. As
you drill down the DataMember is set to the current DataTable so the correct
CurrencyManager for the current binding is retrieved.

CurrencyManager cy = (CurrencyManage r)BindingContex t[DataSource,
DataMember];
if ( !(cy.List is DataViewManager ) && ( cy.Count > 0 ) )
{
cy.RemoveAt( CurrentRowIndex );
}

Hope this helps

Chris Taylor

"Junkguy" <ju*****@columb us.rr.com> wrote in message
news:ju******** *************** ****@news-server-fe-02.columbus.rr. com...


I'm having difficulty deleting rows from a datagrid. I want to put a "delete" button on a form and achieve the same functionality as hitting the "delete" key on the keyboard for the selected row of a datagrid.

I really want to do it generically so I have subclassed datagrid and made my own delete row method but I can only get it to work for datasources that are datatables or dataviews.

Specifically when the datasource is a DataSet I cannot figure out how to make it work. The code works when the datasource is a DataTable or DataView:

public void deleteCurrentRo w()
{
if (!this.ReadOnly )
{
if (this.DataSourc e is DataTable)
{
DataTable dt = (DataTable) this.DataSource ;
dt.DefaultView. Delete(this.Cur rentRowIndex);
}
else if (this.DataSourc e is DataView)
{
((DataView) this.DataSource ).Delete(this.C urrentRowIndex) ;
}

}
}

But trying to delete the current row from a DataSet as the DataSource seems impossible without knowing the table name. In addition, even if we know the table name of the DataSet, the row in the DataSet Table does not correspond to the CurrentRowIndex if the DataGrid is sorted.

I have looked and looked, but cannot find any info to clue me in. What is
frustrating to me is that the DataGrid is able to figure it out for itself. Somehow, it knows what to delete when you hit the "DELETE" key on the
keyboard- but there is no method in DataGrid to clue me in. I become
increasingly frustrated that these form components can do things that I cannot and refuse to allow me access to their innards to achieve the same
funtionality. I cannot understand why DataGrids do not have an accessable
DeleteRow method.

Anyway, rant aside, if I don't get an answer I'm going to cheat by sending a SendKeys.Send(" {DELETE}"), but I really don't want to do this.

Thanks in advance,

--
Junkguy

Nov 15 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
1531
by: Job Lot | last post by:
My DataGrid is bound to DataView as follow DataGrid1.DataSource = myDS.Tables("Client_Consultant").DefaultView User can either delete all the rows in the DataGrid on by pressing "Delete All" button or provide Client_Consulatnt_ID and press "Delete" button to delete rows with that Client_Consulatnt_ID. My question is: 1. How can i delete all the rows on click of a button. 2. How can i remove all the rows with particular...
5
14741
by: Mojtaba Faridzad | last post by:
Hi, with SetDataBinding( ) a DataGrid shows a DataView. user can select some rows in the grid by holding cotrol key. when user clicks on Delete button, I should delete all selected rows. I am trying to delete these lines from the dataview like this: for (int i=0; i < dataView.Count; i++) if (dataGrid.IsSelected(i)) dataView.Delete(i);
2
1718
by: NDady via DotNetMonster.com | last post by:
Hi, I have a datagrid populated from a dataset. On every row in the grid I have a delete button. When the user presses on the delete button I remove the row from the dataset and rebind the datagrid. The problem is that after a couple of delete the index in the dataset does not match the index in the grid and the wrong record i deleted from the dataset. How can I solve this Problem? I am using the following procedure: Private Sub...
0
2013
by: Patrick.O.Ige | last post by:
I have a datagrid with checkboxes.. When a user clicks one check box and clicks the delete button it deletes that ROw. There another situation when a user clicks multiple rows so i had to loop throug the rows like so:- foreach (DataGridItem i in DataGrid1.Items) { CheckBox deleteChkBxItem = (CheckBox) i.FindControl ("DeleteRow"); if (deleteChkBxItem.Checked) {
0
955
by: Robert Batt | last post by:
Hello I have a problem deleting rows from a datagrid that is bound to a dataset. The datagrid updates and inserts just fin so the binding to the dataset must be ok. However when I try to delete a row from the grid it sometimes works and sometime gives an "index was outside the bounds of the array error". I have added a handler to determine when the dataset row is deleted as shown below any ideas what is going wrong Rober The grid is...
4
4651
by: Geoff | last post by:
Hi I'm hoping somebody can help me with the following. I'm trying to delete all the rows in a dataview. There are 200 rows. Everything works fine until I delete half way through and then I'm told in an exception that "There is no row at position 100"
10
2345
by: Nick | last post by:
Hello, Please pardon my ignorance as I'm sure this is easy to do. I have a datagrid where I want to let the user delete columns. I added a context menu to the datagrid that has a delete option. If the user right clicks on a column heading can I highlight that column and then delete it? I'm not exactly sure how to highlight it or figure out what column has been clicked. In my testing so far when I use the context menu I always get the...
3
1747
by: marshallarts | last post by:
Hello, I have a datagrid (grdShots) on a form, and a button to allow the user to delete records from the dataset underlying the grid. My code appears to work, because the row disappears from the datagrid. But the delete is not being updated back to the data source, because if I exit the app then start it again, the record reappears in the grid - it has not been deleted. Code for deleting the row is as follows. Column 0 in the grid...
9
1738
by: Hamed | last post by:
Hello I have a DataGrid that a is bound to a DataTable. Some of the rows in the DataTable should not be deleted. How can I prohibit deleting of some identified rows? The problem could be specified in the following format too: There is a DataView object that I want to prohibit deleting of some of its DataRowView objects. that is: when drv.Delete() is called (drv is the
0
9492
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9299
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9908
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9885
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9740
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
6564
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5175
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5332
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
2702
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.