Results 1 to 11 of 11

Thread: [RESOLVED] DGV display a column based on logic.

  1. #1

    Thread Starter
    Fanatic Member Megalith's Avatar
    Join Date
    Oct 2006
    Location
    Secret location in the UK
    Posts
    879

    Resolved [RESOLVED] DGV display a column based on logic.

    Hi all,

    I have a DGV which is part of an inventory system. I have several columns but the ones i need to resolve are Quantity, Name and Plural Name. essentially if quantity is 1 i need to display the Name data if greater than one the plural data. Is this possible? would i need to resolve this earlier in the process (the data is extracted from an xml file using LINQ to bind it to a rowObject) or can i do this with the completed dataset, say by hiding the Name and Plural Name colums and creating a custom column? ideally i would like to keep the data untouched.
    If debugging is the process of removing bugs, then programming must be the process of putting them in.

  2. #2

    Thread Starter
    Fanatic Member Megalith's Avatar
    Join Date
    Oct 2006
    Location
    Secret location in the UK
    Posts
    879

    Re: DGV display a column based on logic.

    Ok i have answered most of my question, i have hidden the Name and Plural Name columns and created a new Column Item, how now do i fill the new custom column with the data based on the if statement?
    If debugging is the process of removing bugs, then programming must be the process of putting them in.

  3. #3
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: DGV display a column based on logic.

    You're talking about a custom type (RowObject) but then you mention a DataSet, so I'm not sure what your grid is bound to. If the data source is a DataTable then you can add an extra DataColumn and set its Expression property to calculate the value based on other columns.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  4. #4

    Thread Starter
    Fanatic Member Megalith's Avatar
    Join Date
    Oct 2006
    Location
    Secret location in the UK
    Posts
    879

    Re: DGV display a column based on logic.

    I am confused over this :s what i have done is bound the data to the RowObject in data sources, i then used the DGV form designer to fill the grid using this source. As you know this project takes data from a linq query binds it to a list of this object and then (now) binds this object to a datasource. I feel like i'm in some kind of warped space with all this. what would be the correct way to implement this based on the following criteria

    1/ The data is extracted from xml into a LINQ query.
    2/ This data is used in other methods within the main application (check to see if sufficient quantities exist and wether a product ID exists)
    3/ The data populates a DGV which should be sortable ideally by any column (as it stands i am sorting by either Cost or Quantity using a few radio buttons on the form)
    4/ The data is repopulated when new stock arrives

    I presume that i have gotten myself in a cant see the wood for trees state of mind any pointers would be appreciated to get this done in the simplest way.

    P.S. i dont mind rewriting the entire process if it does what i need
    Last edited by Megalith; Jan 12th, 2010 at 07:51 PM.
    If debugging is the process of removing bugs, then programming must be the process of putting them in.

  5. #5
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: DGV display a column based on logic.

    Part of the confusion is due to terminology. You're using terms that have specific meaning where that meaning doesn't apply and, as such, I don't really know what you mean and you probably don't either. For instance, data isn't extracted INTO a LINQ query. The query does the extracting and then returns the results as an IEnumerable list. Also, your LINQ query doesn't bind anything. The query creates and populates objects and then returns them in a list, which you then bind to your control(s).

    I think you need to explain exactly what you've done, fully and clearly. You say this:
    what i have done is bound the data to the RowObject in data sources
    I don't really know what that means. RowObject is a table in a typed DataSet? A row in a table in a typed DataSet? Something else?
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  6. #6

    Thread Starter
    Fanatic Member Megalith's Avatar
    Join Date
    Oct 2006
    Location
    Secret location in the UK
    Posts
    879

    Re: DGV display a column based on logic.

    Sorry for the confusion ok i'll try again, yes i'm confused

    RowObject is a Class of Fields and Properties that are populated from the LINQ query

    InvList is a List(of RowObject) filled direct from the LINQ query

    i used the Data Surces from the menubar Data>Show Data Sources - Add New Data Source>Object>RowObject

    I then used the Edit Column property box of the DGV to bind to the Data Source RowObject.

    I was then able to add a custom Column from the Edit Column property box (I named it Item) and finally hid and resized the columns.

    Is this the right way to do this? it works anyway, just need to fill the Item Column
    If debugging is the process of removing bugs, then programming must be the process of putting them in.

  7. #7
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: DGV display a column based on logic.

    OK, things are clearer now. Yes, that sounds fine. You certainly can add an unbound column to a bound grid. You'd then handle the appropriate event, probably CellValueChanged, to set the value in the unbound column based on values in the bound columns.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  8. #8

    Thread Starter
    Fanatic Member Megalith's Avatar
    Join Date
    Oct 2006
    Location
    Secret location in the UK
    Posts
    879

    Re: DGV display a column based on logic.

    ah thats good, that makes sense, so each time a row is displayed it calculates the value required thx once again for the help.
    If debugging is the process of removing bugs, then programming must be the process of putting them in.

  9. #9

    Thread Starter
    Fanatic Member Megalith's Avatar
    Join Date
    Oct 2006
    Location
    Secret location in the UK
    Posts
    879

    Re: [RESOLVED] DGV display a column based on logic.

    Ok this is hurting my head and i should be in bed. I dont seem to be able to get how to determine the value. I tried datagridview1.Item("Item", e.RowIndex).Value = "Blah" which i presumed would fill all the values of the Item column with Blah but i got a runtime error...

    An error occurred creating the form. See Exception.InnerException for details. The error is: Index was out of range. Must be non-negative and less than the size of the collection. Parameter name: index

    which is in the line where the DGV is populated with the list, but is obviously within my CellValueChanged event

    Further i was unable to work out how to access the hidden Name and PluralName columns so as to determine the value for Item based on the value of Quantity.

    Google for help with this has been fruitless with most of it referring to Net2003 and 2005 neither of which are the same as in 2008 by all accounts and were misleading to say the least one example said to use row[x].column[y] so i'm going to walk away from it before my head explodes

    Something i guess would work would be to override the paint event but i think that is ott for my needs and would anyway end with the same problem of determining where the data is.
    If debugging is the process of removing bugs, then programming must be the process of putting them in.

  10. #10
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: [RESOLVED] DGV display a column based on logic.

    It's a matter of a bit of reading, experimentation and debugging. I didn't know the exact answer to your problem so I took a look at what events the DGV had available that might be relevant, wrote some code and debugged to see what happened.
    Attached Files Attached Files
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  11. #11

    Thread Starter
    Fanatic Member Megalith's Avatar
    Join Date
    Oct 2006
    Location
    Secret location in the UK
    Posts
    879

    Re: [RESOLVED] DGV display a column based on logic.

    thats great, got it working now
    If debugging is the process of removing bugs, then programming must be the process of putting them in.

Posting Permissions

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



Click Here to Expand Forum to Full Width