Solved

Edit datagrid

Posted on 2006-11-12
14
240 Views
Last Modified: 2010-04-23
Hi,

How can I update a datagrid? pls. give a vb sample.

ayha
0
Comment
Question by:ayha1999
  • 7
  • 7
14 Comments
 
LVL 34

Expert Comment

by:Sancler
ID: 17925267
What do you mean by "update a datagrid"?

A datagrid displays data which is held in its datasource - usually a datatable.  If the data in the datatable changes the display in the datagrid is automatically updated.  In that sense, you do not need to "update a datagrid".  If you want to change the data in the datatable you can do so by typing in the cells in the datagrid.  To that extent you "update a datagrid" by typing in it.

I think you need to say a bit more about what it is you are actually doing and trying to achieve.

Roger
0
 
LVL 7

Author Comment

by:ayha1999
ID: 17926105
Hi,

I want to change data in the cells and update the table with data validation. then update the datagrid with new data in the table.

ayha
0
 
LVL 34

Expert Comment

by:Sancler
ID: 17928951
Sorry.  This question is still too amorphous.

I see from

http://www.experts-exchange.com/Programming/Programming_Languages/Dot_Net/VB_DOT_NET/Q_22017837.html

that you have already accepted an answer to a similar question.  So, in the light of that, what code have you currently got and what is wrong with it?

Roger
0
 
LVL 7

Author Comment

by:ayha1999
ID: 17930325
Hi,

I expected that the previously accepted includes everything I need related to datagrid but it doesn't, otherwise I would never have to ask this question again.  Most of the information in that is related to formting.

ayha
0
 
LVL 7

Author Comment

by:ayha1999
ID: 17946793
Hi,

how can I place 'Edit' button in each row in webform datagrid as in web datagrid?

ayha
0
 
LVL 34

Expert Comment

by:Sancler
ID: 17946974
Have a look at this.

http://www.syncfusion.com/FAQ/WindowsForms/FAQ_c44c.aspx#q888q

But with a Winforms datagrid you don't need a button to edit it.  You just enter the cell, with the mouse or tabbing or keys, and type whatever new value you want.  Have you tried it?

Roger
0
 
LVL 7

Author Comment

by:ayha1999
ID: 17947068
Hi,

could u pls. tell me which question explains about editing in the above link?  when I type in the cell and press enter which event fires?
0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 34

Expert Comment

by:Sancler
ID: 17951664
When you leave a cell - whether mouse or by tabbing, or use of the arrow keys, or by pressing Enter - the DataGrid's CurrentCellChanged event fires.

Roger
0
 
LVL 7

Author Comment

by:ayha1999
ID: 17974560
hi,

datagrid editting possible only by cell by cell update? how can I retrive value of current cell? suppose I have a row with partno , name, category ete.  

partno   name   category
A001    part 1    tools
A002    part 2    tools

here I am editing name of the second row then how can I retrive partno of that row and changed name of the cell?

ayha

0
 
LVL 34

Expert Comment

by:Sancler
ID: 17975224
>>
datagrid editting possible only by cell by cell update?
<<

Your question was "when I type in the cell and press enter which event fires".

The content of any cell can be read or set by the DataGrid's .Item property.  E.g.

   MyValue = MyDataDrid(thisRow,thisCol)

or

   MyDataDrid(thisRow,thisCol) = myValue

thisRow and thisCol are zero-based indexes.  So, from your example,

   myPartNo = MyDataGrid(1,0)

and

   myName = MyDataGrid(1,1)

I have to say, though, that your comments and questions still suggest to me that you may be misunderstanding the nature of a DataGrid in Windows Forms.  The datagrid is not something that, of itself, is "edited".  It is a mechanism for "editing" the datatable which is its datasource.  So, when you say "I am editing name of the second row then how can I retrive partno of that row and changed name of the cell", if you are editing the name on the second row in the datagrid what that means to me is that you are typing in different text in the name column in the second row of the datagrid so that it will be changed in the corresponding row and column in the datatable.  So far as I can see, doing that, you have no need to "retrieve partno of that row" - it is there in front of your eyes in the datagrid - or the "changed name of the cell" - that is also there in front of your eyes.  It will be reflected automatically in the datatable as soon as you navigate to another row in the datagrid or even before that if you force that to happen by calling

   BindingContext(myBoundDataTable).EndCurrentEdit

If, on the other hand, you want programmatically to change the name part2 to, for example, partB then you would normally do that in the datatable itself, and that change would then be reflected in what the datagrid shows.

I really think that, if you need any further clarification, you must post what code you've got and identify specifically what problems it is giving you and an which lines.

Roger
0
 
LVL 7

Author Comment

by:ayha1999
ID: 17975466
Hi Roger,

I have already mentioend that when I change text and press or lose focus of that cell then I want to update the table itself. I want to allow users to change the table through datagrid. is there any otehr way to do that?  I used datagrid1.UpdateCommand in web datagrid to do that where I can retrive each and every row details through findcontrol or any other method.

ayha
0
 
LVL 34

Expert Comment

by:Sancler
ID: 17976312
You say

>>
I have already mentioend that when I change text and press or lose focus of that cell then I want to update the table itself.
<<

I have already said that, once a new value has been typed in a datagrid cell

>>
It will be reflected automatically in the datatable as soon as you navigate to another row in the datagrid or even before that if you force that to happen by calling

   BindingContext(myBoundDataTable).EndCurrentEdit
<<

Have you tried that?  If so, what is your problem with it?

Forget the web datagrid.  Although I do not do web stuff myself, my understanding is that it is a completely different animal and works in completely different ways.

What you need to do is create a Windows Forms project, fill a datatable, bind it to a datagrid and actually try to do things with it.  Here's an example

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vbcon/html/vbwlkwalkthroughdisplayingdatafromsingletableinwindowsform.asp

Until you've actually done that I think we will just continue to talk at cross purposes.  I don't really understand your questions and it seems you don't understand my answers.

Roger

0
 
LVL 7

Author Comment

by:ayha1999
ID: 17988298
Hi,

I checked but it is different from my code I used to bind the datagrid. here is my code:

dim con as new sqlconnection("...
dim ad as new sqldataadapter
dim ds as new dataset

dim cmd as new sqlcommand("select...
con.open

ad.selectcommand=cmd
ad.fill(ds)

datagrid1.datasource= ds.Tables(0)

how can I update this adapter when changing items in the datagrid or should I used the same method explained in the link?

ayha
0
 
LVL 34

Accepted Solution

by:
Sancler earned 125 total points
ID: 17988590
Have a look at this recent answer

http://www.experts-exchange.com/Programming/Programming_Languages/Dot_Net/VB_DOT_NET/Q_22048420.html#a17871205

That relates to OleDb dataobjects, but it is equally valid for Sql dataobjects.  It was also written in a VB.NET 2005 context, but it applies equally to 2003 if datagrid is substituted for datagridview and datasource is substituted for bindingsource.

Roger
0

Featured Post

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

This article explains how to create and use a custom WaterMark textbox class.  The custom WaterMark textbox class allows you to set the WaterMark Background Color and WaterMark text at design time.   IMAGE OF WATERMARKS STEPS Create VB …
This tutorial demonstrates one way to create an application that runs without any Forms but still has a GUI presence via an Icon in the System Tray. The magic lies in Inheriting from the ApplicationContext Class and passing that to Application.Ru…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

758 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now