Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Windows Forms / WinForm General / June 2006

Tip: Looking for answers? Try searching our database.

DataGrid edit questions.

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Kevin Burton - 14 Jun 2006 16:51 GMT
I seem to have gotten the data grid so that I can edit individual columns but
it has brought up some questions.

1) The only way that I can get a column to be editable is to make the whole
datagrid not read-only. Like:

           Me.dgOrderItems.ReadOnly = False

The problem with that is that when I add a row to the datagrid I get two
rows, one that I added and one that just contains nulls (an empty row).  I
override the default DataGridTextBoxColumn and set the ReadOnly property to
False but unless the ReadOnly property is set for the whole grid I cannot
edit the columns. I only use the overriden DataGridTextBoxColumn for certain
columns:

           Dim dgEditableTextCol As New DataGridEditTextBoxColumn
           dgEditableTextCol.HeaderText = "QTY"
           dgEditableTextCol.MappingName = "qty"
           dgEditableTextCol.Width = 35
           dgEditableTextCol.ReadOnly = False
           dgTblStyle.GridColumnStyles.Add(dgEditableTextCol)

           dgEditableTextCol = New DataGridEditTextBoxColumn
           dgEditableTextCol.HeaderText = "Unit Price"
           dgEditableTextCol.MappingName = "UnitPrice"
           dgEditableTextCol.Width = 70
           dgEditableTextCol.Format = "C"
           dgEditableTextCol.ReadOnly = False
           dgTblStyle.GridColumnStyles.Add(dgEditableTextCol)

2) I have created a "calculated" column using the "Expression" property.

           Me.columnextendedprice = New
System.Data.DataColumn("extendedprice", GetType(Decimal), Nothing,
System.Data.MappingType.Element)
           Me.columnextendedprice.Expression = "qty * unitprice"
           MyBase.Columns.Add(Me.columnextendedprice)

The problem is that this caclulated column is not getting updated after a
column/row has been edited. It is only when I leave the row altogether that
this expression seems to be evaluated. Is there a way to force an earlier
update.

3) Finally, I need to update other fields on the form when the data in the
DataGrid has been edited. I need some sort of notification when the column
has been edited so I can update the other fields in the form. For the columns
that I want to edit I currently have:

      Protected Overloads Overrides Sub Edit(ByVal source As
CurrencyManager, ByVal rowNum As Integer, ByVal bounds As Rectangle, ByVal
[readOnly] As Boolean, ByVal instantText As String, ByVal cellIsVisible As
Boolean)
           MyBase.Edit(source, rowNum, bounds, [readOnly], instantText,
cellIsVisible)
       End Sub

Would this be a good place to fire/raise an event? When is this called? Is
it called after the text is changed (so instantText will contain the changed
value)?

Thank you for your help.

Kevin
Kevin Burton - 14 Jun 2006 17:38 GMT
As an aside I notice that in the overridden "Edit" method the "instantText"
that is supplied is always NULL or Nothing. So I guess a fourth question
would be, how do I get the former value of the row/column and what it is
being changed to?

Thanks again.

> I seem to have gotten the data grid so that I can edit individual columns but
> it has brought up some questions.
[quoted text clipped - 59 lines]
>
> Kevin
Kevin Burton - 15 Jun 2006 16:07 GMT
On the Edit question. It seems that the Edit method that I am overriding in
my derived class from DataGridTextBoxColumn (I am calling
DataGridEditTextBoxColumn) is called BEFORE the edit occurs. I need a method
that is called AFTER the edit occurs. Any ideas?

> I seem to have gotten the data grid so that I can edit individual columns but
> it has brought up some questions.
[quoted text clipped - 59 lines]
>
> Kevin
Kevin Burton - 15 Jun 2006 16:34 GMT
Further clarification. If I add a TextChanged event then I get the value that
the textbox is being changed to but the table (DataSource) still has the old
value. Is there a way to force the synch up the values in the text box with
the values in the table. I know I can bind but as I understand it that moves
the values from the table to the text box. I need to move the values in the
text box to the table.

Thanks again.

> I seem to have gotten the data grid so that I can edit individual columns but
> it has brought up some questions.
[quoted text clipped - 59 lines]
>
> Kevin
Thor Kornbrek - 15 Jun 2006 17:11 GMT
DataTable.RowChanged < look at the datatable that is the source for your
grid. It will fire the RowChanged event when an update has been successful.
This should let you pass the data to whatever controls need to be updated.

http://msdn2.microsoft.com/en-us/library/system.data.datatable.rowchanged.aspx

Signature

Thor Kornbrek
.Net Developer

> Further clarification. If I add a TextChanged event then I get the value that
> the textbox is being changed to but the table (DataSource) still has the old
[quoted text clipped - 68 lines]
> >
> > Kevin
Kevin Burton - 15 Jun 2006 22:25 GMT
Thank you using this information my application works much better.

Two questions.

1) I don't seem to get a RowChanged event when the first row is added to the
table but I do get it for each subsequent row that is added.
2) The visible part is a DataGrid. It seems that the RowChanged event is not
fired until the row that is being edited looses focus. Is there anyway to
force this event sooner?

Thanks again.

Kevin

> DataTable.RowChanged < look at the datatable that is the source for your
> grid. It will fire the RowChanged event when an update has been successful.
[quoted text clipped - 74 lines]
> > >
> > > Kevin
Thor Kornbrek - 16 Jun 2006 16:52 GMT
You will not be able to get live data from the events that you are capturing.
They are fired after a user commits their change (cell lost focus).

The closest you can get to live data would be to create a class derived from
DataGridTextBoxColumn Class (System.Windows.Forms).

Declare currentText at the class level.
Create an Event that fires when currrentText changes. Pass the currentText
as an eventArg.
Set currentText when you override the paint eventhandler.

       Try
           currentText = CType(MyBase.GetColumnValueAtRow(source, rowNum),
String)
       Catch ex As Exception
           currentText = ""
       End Try

       currentTextChanged(currentText)

In the event handler update your textbox.

Hope that helps.
Signature

Thor Kornbrek
.Net Developer
http://www.geekguild.net


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.