473,883 Members | 1,767 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 4190
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
1543
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
14746
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
1723
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
2015
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
959
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
4656
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
2359
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
1753
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
1744
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
9792
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
11142
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10745
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...
0
10417
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
9575
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7972
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
7130
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
5992
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
4221
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.