Edit datagrid

Hi,

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

ayha
LVL 7
ayha1999Asked:
Who is Participating?
 
SanclerConnect With a Mentor Commented:
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
 
SanclerCommented:
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
 
ayha1999Author Commented:
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
Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

 
SanclerCommented:
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
 
ayha1999Author Commented:
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
 
ayha1999Author Commented:
Hi,

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

ayha
0
 
SanclerCommented:
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
 
ayha1999Author Commented:
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
 
SanclerCommented:
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
 
ayha1999Author Commented:
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
 
SanclerCommented:
>>
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
 
ayha1999Author Commented:
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
 
SanclerCommented:
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
 
ayha1999Author Commented:
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.