DataGrid edit questions.

G

Guest

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
 
G

Guest

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.

Kevin Burton said:
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
 
G

Guest

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?

Kevin Burton said:
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
 
G

Guest

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.

Kevin Burton said:
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
 
G

Guest

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



--
Thor Kornbrek
..Net Developer


Kevin Burton said:
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.

Kevin Burton said:
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
 
G

Guest

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

Thor Kornbrek said:
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



--
Thor Kornbrek
.Net Developer


Kevin Burton said:
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.

Kevin Burton said:
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
 
G

Guest

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top