Page 1 of 2 12 LastLast
Results 1 to 20 of 23

Thread: Unbound Columns in DataGridview

  1. #1
    Reaction is offline VB.NET Forum Newbie
    .NET Framework
    .NET 2.0 (VS 2005)
    Join Date
    Apr 2007
    Posts
    13
    Reputation
    96

    Question Unbound Columns in DataGridview

    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

  2. #2
    cjard's Avatar
    cjard is offline VB.NET Forum All-Mighty
    .NET Framework
    .NET 4.0
    Join Date
    Apr 2006
    Posts
    7,036
    Reputation
    1724
    Quote Originally Posted by Reaction View Post
    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..

  3. #3
    Reaction is offline VB.NET Forum Newbie
    .NET Framework
    .NET 2.0 (VS 2005)
    Join Date
    Apr 2007
    Posts
    13
    Reputation
    96
    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?

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

    REACTION

  4. #4
    cjard's Avatar
    cjard is offline VB.NET Forum All-Mighty
    .NET Framework
    .NET 4.0
    Join Date
    Apr 2006
    Posts
    7,036
    Reputation
    1724
    Quote Originally Posted by Reaction View Post
    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?

  5. #5
    Reaction is offline VB.NET Forum Newbie
    .NET Framework
    .NET 2.0 (VS 2005)
    Join Date
    Apr 2007
    Posts
    13
    Reputation
    96
    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

  6. #6
    cjard's Avatar
    cjard is offline VB.NET Forum All-Mighty
    .NET Framework
    .NET 4.0
    Join Date
    Apr 2006
    Posts
    7,036
    Reputation
    1724
    Quote Originally Posted by Reaction View Post
    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!

  7. #7
    Reaction is offline VB.NET Forum Newbie
    .NET Framework
    .NET 2.0 (VS 2005)
    Join Date
    Apr 2007
    Posts
    13
    Reputation
    96
    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

  8. #8
    cjard's Avatar
    cjard is offline VB.NET Forum All-Mighty
    .NET Framework
    .NET 4.0
    Join Date
    Apr 2006
    Posts
    7,036
    Reputation
    1724
    Quote Originally Posted by Reaction View Post
    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

  9. #9
    Reaction is offline VB.NET Forum Newbie
    .NET Framework
    .NET 2.0 (VS 2005)
    Join Date
    Apr 2007
    Posts
    13
    Reputation
    96
    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

  10. #10
    cjard's Avatar
    cjard is offline VB.NET Forum All-Mighty
    .NET Framework
    .NET 4.0
    Join Date
    Apr 2006
    Posts
    7,036
    Reputation
    1724
    I'll do you a project now...

  11. #11
    cjard's Avatar
    cjard is offline VB.NET Forum All-Mighty
    .NET Framework
    .NET 4.0
    Join Date
    Apr 2006
    Posts
    7,036
    Reputation
    1724
    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
    Attached Files Attached Files

  12. #12
    Reaction is offline VB.NET Forum Newbie
    .NET Framework
    .NET 2.0 (VS 2005)
    Join Date
    Apr 2007
    Posts
    13
    Reputation
    96
    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.CurrentCellA ddress.Y).Cells("PRODUCTIONID").Value)
    Dim tca As Integer = CInt(DataGridView1.Rows(DataGridView1.CurrentCellA ddress.Y).Cells("TCAPO").Value)
    Dim WPO As String = DataGridView1.Rows(DataGridView1.CurrentCellAddres s.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.CurrentCellA ddress.Y).Cells("ProductionID").Value)
    Dim tca As Integer = CInt(DataGridView1.Rows(DataGridView1.CurrentCellA ddress.Y).Cells("TCAPO").Value)
    Dim WPO As String = DataGridView1.Rows(DataGridView1.CurrentCellAddres s.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("Product ionID").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.CurrentCellA ddress.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

  13. #13
    cjard's Avatar
    cjard is offline VB.NET Forum All-Mighty
    .NET Framework
    .NET 4.0
    Join Date
    Apr 2006
    Posts
    7,036
    Reputation
    1724
    When you say "the page laods" - are you talking about ASP.NET? If so; dont post in Winforms Grids.. Winforms <> ASP.NET!

  14. #14
    Reaction is offline VB.NET Forum Newbie
    .NET Framework
    .NET 2.0 (VS 2005)
    Join Date
    Apr 2007
    Posts
    13
    Reputation
    96
    Windows application. DataGridview is only available in Windows Applications not Web.


    Reaction
    Last edited by Reaction; 05-09-2007 at 3:03 PM. Reason: Change

  15. #15
    cjard's Avatar
    cjard is offline VB.NET Forum All-Mighty
    .NET Framework
    .NET 4.0
    Join Date
    Apr 2006
    Posts
    7,036
    Reputation
    1724
    OK. What do you mean by "When the page loads" ?

  16. #16
    Reaction is offline VB.NET Forum Newbie
    .NET Framework
    .NET 2.0 (VS 2005)
    Join Date
    Apr 2007
    Posts
    13
    Reputation
    96
    Sorry ... I mean when the form loads.

  17. #17
    cjard's Avatar
    cjard is offline VB.NET Forum All-Mighty
    .NET Framework
    .NET 4.0
    Join Date
    Apr 2006
    Posts
    7,036
    Reputation
    1724
    Can you post your code with some indentation? You may have to switch the editor to BASIC mode using the button

  18. #18
    shawne's Avatar
    shawne is offline VB.NET Forum Fanatic
    .NET Framework
    .NET 4.0
    Join Date
    Feb 2006
    Location
    Pennsylvania
    Posts
    101
    Reputation
    115
    cjard,

    I followed along with your video clip which was extremely helpful in getting me to this point. However, I cannot seem to get the comboboxes to "drop down".

    Real quick, in a nut shell here is what i'm trying to do. I have two tables:

    Table 1 contains various PC client data which I was able to compare to your "People" table in your vid.

    Table 2 contains all the ip and mac address's of those PCs. (compare to your "Lookup" table) I store this in a separate table since it is possible for PC clients to have more than one IP/MAC.

    I will use my IP column for my question, but its pretty much the same setup with the mac column.

    The data properties on my ip comboboxcolumn is as follows:

    DataPropertyName = id (primary key on table 1)
    DataSource = NicsBindingSource (sql table)
    DisplayMember = ip (ip column)
    ValueMember = client_id (references the primary key on table 1)

    It's getting the correct information because the IP/MACs it's coming up with are correct. But I know for a fact on some of the PC clients I should have multiple IPs/MACs, but if I click the drop down, i don't get anything.

    If you need more info. let me know, but the main purpose of my application is to provide a table full of PC data which i will be able to interact with via WMI when the application is finished. I would like to use a DGV as the method/interface for that data. Thanks for any help and for the vid. clip!
    Shawn
    -------

  19. #19
    cjard's Avatar
    cjard is offline VB.NET Forum All-Mighty
    .NET Framework
    .NET 4.0
    Join Date
    Apr 2006
    Posts
    7,036
    Reputation
    1724
    If youre not editing anything then you should not set the DataPropertyName or the ValueMember

    I presume you want a row that has the PC details like name, owner etc and then a column that has a combo showing all the IPs

    You need in your dataset:

    PCTable
    --------
    PCName
    PCSpec

    IPTable
    -------
    PCName
    IPAddress


    A DataRelation PCTable_IPTable (parent_child) linked on PCName

    On your form:
    A DataGridView bound to PCTableBindingSource bound to MyDataSet.PCTable
    A PCIPChildrenBindingSource bound to PCTable.PCTable_IPTableDataRelation
    A DataGridViewComboBoxColumn bound to PCIPChildrenBindingSource and having its DisplayMember set to "IPAddress"

    You see, the whole point is that the datarelation performs filtering. You fill the child datatable with ALL records available for all pcs and all ips. Then when you alter the Current of the Parent bindingsource it affects what is available in the list of the childbindingsource. You select the MY_HOME_PC row in the grid, parent is the MY_HOME_PC row, children are filtered to only MY_HOME_PC elements. Dropping down the combo shows all IP addreses assigned to that pc

  20. #20
    shawne's Avatar
    shawne is offline VB.NET Forum Fanatic
    .NET Framework
    .NET 4.0
    Join Date
    Feb 2006
    Location
    Pennsylvania
    Posts
    101
    Reputation
    115
    Ok,

    First off thanks again for your help. You are the bomb!

    What you described made perfect sense to me and is exactly what i was after. I went through your list of changes and the only one i modified slightly was instead of using the pc name for the relation i used my primary key which is just an integer. So my relation looks something like:

    Code:
    PC table IP table
    PK_id---+ id
    name    +--clientID
    etc..      ip
                etc..
    Other than that, i made all the other appropriate adjustments, but i'm not getting any results in my comboboxes. I'll fiddle around with it some more, but if you have any additional input that would be great! Thanks again!
    Last edited by shawne; 01-07-2009 at 3:27 PM.
    Shawn
    -------

Page 1 of 2 12 LastLast

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Harvest time tracking