Unbound Columns in DataGridview

Reaction

Member
Joined
Apr 29, 2007
Messages
13
Programming Experience
1-3
1) I need to have the values of some unbound columns in my datagridview automatically calculated and updated when values in bound columns are changed. How do I handle this please?

2) I have a DataGridViewComboBox that is populated on form load but I would also like the selected value of my DataGridviewComboBox set at Form load to the value of the Shopping Cart's value. How do I handle this best please?

3) I also want to have the value in one of my unbound column changed as soon as user selects a new value in my DataGridviewComboBox

Reaction
 
1) I need to have the values of some unbound columns in my datagridview automatically calculated and updated when values in bound columns are changed. How do I handle this please?
Research the .Expression property of a DataColumn

myDataTable.MyColumnName.Expression = ...

2) I have a DataGridViewComboBox that is populated on form load but I would also like the selected value of my DataGridviewComboBox set at Form load to the value of the Shopping Cart's value. How do I handle this best please?
Load the cart, retrieve the value, and set the selected value to that. Ensure it is an entry in the list

3) I also want to have the value in one of my unbound column changed as soon as user selects a new value in my DataGridviewComboBox
If the unbound column is calculated from expressionm and the combo is bound to a column that the expression uses, then it will be so..
 
A quick question about Expression Properties pf DataColumns please. Instead of retrieving my information from a DataTable, I am actually attaching my DataGridView to an object, this object has a method in it that calculates the value of the expression for the unbound column. Can I bind the expression property to this method of my object?

Public Sub CalculateBaseCost()
'get the description using the _DescriptionID
getDescPrice()
End Sub
Public Sub CalculateSubTotal()
'make sure a value was retrieved before calculating
If (FUNCTIONNUMBER <> Nothing) Then
FuncTIONCost = CDec(FUNCTIONNUMBER * 0.02)
SubTotal =
CDec(FUNCTIONNUMBER * 0.02) * BASECOST
End If
End Sub
Public Sub CalculateToTalCost()
'make sure a value was retrieved before calculating
If (QuantityRecorded <> Nothing And SubTotal <> Nothing) Then
TotalCost = CDec(QuantityRecorded * SubTotal)
End If
End Sub
Private Sub getDescPrice()
Try
Dim desc As Description = New Description(DescriptionID)
BASECOST = desc.DescriptionValue
Catch e As Exception
Throw New Exception("Error : unable to get the description information " & e.Message)
End Try
End Sub

REACTION
 
A quick question about Expression Properties pf DataColumns please. Instead of retrieving my information from a DataTable, I am actually attaching my DataGridView to an object, this object has a method in it that calculates the value of the expression for the unbound column. Can I bind the expression property to this method of my object?
Expression is a property of a DataColumn. A DataColumn is a feature of a DataTable. If your DGV binds straight to a MyXYZObject, not to a datatable, then no, you wont have an expression.

If youre writing your own object for data storage, then only you can know how to expose the data elemetns as an effective tabular block of data, and only you can really decide how to add another "column"


I dont really get into using objects other than datatables as data sources for datagridviews, simply because I dont think I could create something so good, and so useful in economical time.. Why did you choose to go that route?
 
Because I thought it would be easier to handle. But It turns out I was sort of wrong. I mean I have it all down to the last but I ran into this problem where my columns, even when I have code to order them in the Datagridview control, return the wrong index when I am doing my calculating. So I figured now maybe I could try finding a better way to automate this process of calculating the data for the unbound columns

Reaction
 
Because I thought it would be easier to handle. But It turns out I was sort of wrong. I mean I have it all down to the last but I ran into this problem where my columns, even when I have code to order them in the Datagridview control
Order them row-ar or order them column-ar?

If you are talking about row-ar ordering, then dont do it; its not your object's concern. Its the job of an intermediary like DataView, BIndingSource or even the grid itself to do Rowar ordering
Columnar ordering is managed by the order with which you add columns to the grid

, return the wrong index when I am doing my calculating. So I figured now maybe I could try finding a better way to automate this process of calculating the data for the unbound columns
Conceptually, using the dataset designer grid allows you to make a datatable with which you can show data in a grid. The columns can be thought of as object properties and can be any known type.. SO basically, to anyone thinking to create a custom grid data container, i would say "dont bother, until youve exhausted what a datatable can do" - its quite hard to exhaust it!
 
ok.. I will try using the DataSet and Datatables then. I do have a Question about the Updating of information with this process, do I need to explicitly create the Insert, Update and Delete commands for the Adapter if I were using code to create it? I am not really familiar with using the designer for this and would like to know how to handle this if I were using code.

Also, since I am going to add a combobox to my DataGridview control, How do I in Designer also add a combobox that is bound to another table and then do all this in designer? or do I need to stick with doing this in code?


Reaction
 
ok.. I will try using the DataSet and Datatables then. I do have a Question about the Updating of information with this process, do I need to explicitly create the Insert, Update and Delete commands for the Adapter if I were using code to create it? I am not really familiar with using the designer for this and would like to know how to handle this if I were using code.
Take a read of the DW2 link in my signature, section on "Creating a simple app"

Basically, you would:

Either drag a table out of Server Explorer
-or-
Write a select query

The designer creates:
Typed datatable, column types based on the db table
tabladapter to read/write data between db<->table
S/I/U/D queries

You would then:
Maybe tweak the Select SQL to be parameterised, if you dragged the table/didnt write it
Add some columns with an expression property


Umm

Thats about it

Also, since I am going to add a combobox to my DataGridview control, How do I in Designer also add a combobox that is bound to another table and then do all this in designer? or do I need to stick with doing this in code?
You have the other (lookup)table available in the dataset too, then you:

Add a combo into the grid's columns editor (visual designer)
Set its datasource to be the lookup table
Set its display and valuemember properties to be columns in that lookup table
Set its DataPropertyName to be the column you want it to affect in the main table



It would take me somewhere in the region of 2-3 minutes to do this. I can video it if you need
 
I WOULD Love it if you could do that please. I really want to learn to code in Windows... and defeat this datagridview control. Please send me a video if it is not a bother. Thanks

reaction
 
A few notes:

About 2/3 the way through, you see the prject start successfully, but a rash of errors appear in the window behind - anyone who has ever coded a form that uses a dataset, then seriously changed the dataset, will be familiar with this.. I did exactly this - severely changed a dataset , leaving the form designer open. It threw a wobbler, but you probably wont see this; it's not the typical case :)
All i had to do in this case was close and re-open the form designer.. damn nuisance, marred an otherwise good vid ;)

You'll see at this point that I forgot to add any data to my dataset, so I stop the project and flick into code view, where I had some pre-prepared statements to bang some data into the rows

When the project is running you'll see me change the combobox, and then i navigate to another row, and back to the first, before pointing to the textbox that is showing what the row really contains. I do this because a DAtaGridView will call EndEdit() on the bindingsource when you nav to another row. I didnt expect that the textbox value would change instantly that i altered the combobox, because it would make for very time and resource consuming ways of working, to propagate every change as soon as it is made. The textbox shows the current value, the grid holds the proposed values, until the edit is ended, and the proposed become current
 

Attachments

  • cjard_does_datagridviewcombobox.zip
    145.1 KB · Views: 55
I do have another question please. I have a couple of methods define to help User click to select a row of my datagridview . Problem is when I click a row in my grid, nothing happens, none of the events are fired when I only use one. However when I have all three events in my code, As soon as the page loads, one of the three fires even before user is able to click anything and runs into an error. Does anyone know the best way to handle this issue please??

Private Sub DataGridView1_CellContentClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView1.CellContentClick
If LOADING = True Then
'do nothing else
LOADING = False
Return
Else
' Show the customer order screen when the user presses Enter except for
' where the Customer ID is not yet known (such as the New Row).
Me.ProductionID = CInt(DataGridView1.Rows(DataGridView1.CurrentCellAddress.Y).Cells("PRODUCTIONID").Value)
Dim tca As Integer = CInt(DataGridView1.Rows(DataGridView1.CurrentCellAddress.Y).Cells("TCAPO").Value)
Dim WPO As String = DataGridView1.Rows(DataGridView1.CurrentCellAddress.Y).Cells("WPO").Value
'get the packedID associated with this from Packed Table
getPackedID(tca, WPO)

If CBool(Not Me.ProductionID) Then
' Stop the DataGridView from processing the enter.
Dim co As ProductionData = New ProductionData
co.ProductionID = Me.ProductionID
co.packedID = Me.PACKEDID
co.ShowDialog(Me)

End If
End If
End Sub


Private Sub DataGridView1_KeyDown1(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles DataGridView1.KeyDown

' Show the customer order screen when the user presses Enter except for
' where the Customer ID is not yet known (such as the New Row).
If LOADING = True Then
'do nothing else
LOADING = False
Return
Else
If e.KeyData = Keys.Enter Then

Me.ProductionID = CInt(DataGridView1.Rows(DataGridView1.CurrentCellAddress.Y).Cells("ProductionID").Value)
Dim tca As Integer = CInt(DataGridView1.Rows(DataGridView1.CurrentCellAddress.Y).Cells("TCAPO").Value)
Dim WPO As String = DataGridView1.Rows(DataGridView1.CurrentCellAddress.Y).Cells("WPO").Value
'get the packedID associated with this from Packed Table
getPackedID(tca, WPO)

If CBool(Not Me.ProductionID) Then
' Stop the DataGridView from processing the enter.
e.Handled = True
Dim co As ProductionData = New ProductionData
co.ProductionID = Me.ProductionID
co.packedID = Me.PACKEDID
co.ShowDialog(Me)

End If
End If
End If
LOADING = True
End Sub

' Detecting Which Cell Is Clicked
'---when the user clicks on the datagridview control---
Private Sub DataGridView1_CellEnter(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView1.CellEnter
If LOADING = True Then
'do nothing else
LOADING = False
Return
Else

'---prints the content of the cell---
Dim x As Integer = e.RowIndex
' 'get the ProductionID for this row
Me.ProductionID = CInt(DataGridView1.Rows(e.RowIndex).Cells("ProductionID").Value)
'get the packedID associated with this from Packed Table
Dim tca As Integer = CInt(DataGridView1.Rows(e.RowIndex).Cells("TCAPO").Value)
Dim WPO As String = DataGridView1.Rows(e.RowIndex).Cells("WPO").Value
getPackedID(tca, WPO)

Dim co As ProductionData = New ProductionData
co.ProductionID = Me.ProductionID
co.packedID = Me.PACKEDID
co.ShowDialog(Me)

End If
End Sub

Private Sub DataGridView1_SelectionChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles DataGridView1.SelectionChanged
Me.ProductionID = CInt(DataGridView1.Rows(DataGridView1.CurrentCellAddress.Y).Cells("ProductionID").Value)
If LOADING = True Then
'do nothing else
LOADING = False
Return
Else
If CBool(Not Me.ProductionID) Then
' Stop the DataGridView from processing the enter.
' Stop the DataGridView from processing the enter.
Dim co As ProductionData = New ProductionData
co.ProductionID = Me.ProductionID
co.packedID = Me.PACKEDID
co.ShowDialog(Me)
Me.Close()
End If
End If
End Sub


thanks in advance
Reaction
 
When you say "the page laods" - are you talking about ASP.NET? If so; dont post in Winforms Grids.. Winforms <> ASP.NET!
 
Windows application. DataGridview is only available in Windows Applications not Web.


Reaction
 
Last edited:
Back
Top