Identifying the Current Row

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Christopher Weaver

    Identifying the Current Row

    I want to set a value in a specific field in the current row of a DataSet.
    This seems like the most basic thing to do but I can't find the syntax for
    identifying the current row.

    IOW, I can do this:

    SomeRow["fieldName"] = 'value';

    But how do I set SomeRow to the row that the user is currently viewing?

    I tried this:

    DataRowView drv = (DataRowView) cMgr.Current; //Where cMgr is a
    CurrencyManager
    drv["SubCategor y"] = Value;

    But it doesn't stick. When the user moves off the record and back again,
    the previous value is restored.


  • Christopher Weaver

    #2
    Re: Identifying the Current Row

    >> ... mightn't it be better to get them all into one
    dataset and set up a DataRelation between them? <<

    This is exactly what I needed to do. I'm still not used to this concept of
    multiple tables residing in the same DataSet. ( I come from an environment
    where DataSet and table were synonymous. )

    Here's the idea. Tasks is a table with a Foreign Key constraint in
    SubCategory. Since the user can only include items from the finite set of
    choices within the referenced table, I am providing a ComboBox from which
    they are constrained to make their choice. Furthermore, the items in that
    ComboBox are constrained by the choice made in another field, also controled
    by a ComboBox.

    I've placed a TextBox on the form and bound its Text property to
    "Tasks.SubCateg ory" for the purposes of debugging this situation.

    I've brought all of the tables within the same DataSet and created three
    relationships (1 : m in each case): Task : Activities, Category :
    SubCategory, and Category : Task. The dropdown behavior now results in the
    underlying table receiving the user's selection. It did require one line of
    code in the
    SelectionChange Committed event to help it along:

    dsTaskActivitie s.Tables["Tasks"].Rows[cMgr.Position]["SubCategor y"] =
    Value;

    But I still have a problem. As the user moves through the records of the
    Task table, the ComboBox does not always show the correct value for the
    field that it is bound to. Sometimes it does, but other times it just
    reveals a blank. What's really odd is that it will show a blank on a
    specific row when approached from one direction, say the previous row, but
    when approached from the following row it shows the correct value.

    So, I've made some progress, but I still need to figure out why the ComboBox
    is not consistant. If you have any thoughts on this I would really
    appreciate it.

    Also, I must implement the same behavior in a DataGrid. One of the columns
    needs to have a ComboBox in it. In other words, it needs to function like
    the property editor pane when editing a property like BorderStyle. Do you
    know how to do this?

    Thanks for your help.





    "Ian Griffiths [C# MVP]" <ian-interact-sw@nospam.nospa m> wrote in message
    news:O$V0jmiZFH A.2768@tk2msftn gp13.phx.gbl...[color=blue]
    > Sorry - I'm used to using strongly-typed datasets. With those, the
    > DataSet exposes a property for each table, and the table provides an
    > indexer that means you don't need to use Rows. I have a habit of
    > forgetting that when going back to normal datasets...
    >
    > As for your combobox, if I understand correctly, you've bound the Text
    > property to one table, but you've got the DataSource of the same combobox
    > bound to a different table. I'm not sure if that's a supported scenario
    > to be honest.
    >
    > cbSubCategoryLo okUp.DataBindin gs.Add("Text", dsTaskActivitie s,
    > "Tasks.SubCateg ory");
    >
    > and
    >
    > cbSubCategoryLo okUp.DataSource = dsCatSubCats.Ta bles["Category"];
    >
    > I'm not sure what the expected behaviour is when you've got two different
    > currency managers trying to control the same value. (Which is ultimately
    > what's going on here. The combobox will want to set the Text property to
    > be whatever the currently selected item is, which will be an item from the
    > dsCatSubCats.Ta bles["Category"] table, but you've also got a binding
    > trying to set it to something from a different table altogether.)
    >
    > At least that's how it looks to me. On the other hand it's late here, so
    > I could have misread it. :)
    >
    > What are you trying to achieve here? Is there some relation between the
    > Category table in dsCatSubCats and the SubCategory table in
    > dsTaskActivitie s? If so, mightn't it be better to get them all into one
    > dataset and set up a DataRelation between them? But I'm not quite clear
    > on what your goal is, so I'm not sure how best to solve the problem.
    >
    > --
    > Ian Griffiths - http://www.interact-sw.co.uk/iangblog/
    >
    > "Christophe r Weaver" <we.aver@verizo n.net> wrote in message
    > news:eKzxihhZFH A.796@TK2MSFTNG P10.phx.gbl...[color=green]
    >> Thanks for the reply.
    >>
    >> I've tried a couple of things before and since your post. Here's my
    >> attempt to implement one of your suggestions:
    >>
    >> Your suggestion was:
    >> myDataSet.MyTab le[currencyMgr.Pos ition]["field"] = someValue;
    >>
    >> and my implementation was:
    >> dsTaskActivitie s.Tables["Tasks"][cMgr.Position]["SubCategor y"] =
    >> Value;
    >>
    >> The error message received was:
    >> Cannot apply indexing with [] to an expression of type
    >> 'System.Data.Da taTable'
    >>
    >> So I changed it to this:
    >> dsTaskActivitie s.Tables["Tasks"].Rows[cMgr.Position]["SubCategor y"] =
    >> Value;
    >>
    >> And added this:
    >> dsTaskActivitie s.Tables["Tasks"].Rows[cMgr.Position].EndEdit();
    >>
    >> Now here's the real problem. I've realized through some experimentation
    >> that the problem lies in the specific control that I'm using. I'm using
    >> a ComboBox to allow the user to select a value from a foreign key table.
    >> When a selection is made, the selected value is revealed in a TextBox
    >> that I added for debugging purposes; so I know that the value is being
    >> set properly. When I leave the editted row and return, the new value is
    >> still in the TextBox but the ComboBox is showing another value.
    >> Sometimes it's the previous value, sometimes it's something else
    >> entirely. While I thought that I was having trouble getting the value
    >> selected by the user to stick, it appears that I'm really having trouble
    >> getting the ComboBox to show the current value. I've used the following:
    >>
    >> cbSubCategoryLo okUp.DataBindin gs.Add("Text", dsTaskActivitie s,
    >> "Tasks.SubCateg ory");
    >> textBox2.DataBi ndings.Add("Tex t", dsTaskActivitie s, "Tasks.SubCateg ory");
    >>
    >> And yet they don't always show the same value even though the ComboBox is
    >> bound to the table that holds the only values allowed in SubCategory and
    >> the only values that show up in the TextBox are selected from the
    >> ComboBox dropdown list. This is how the ComboBox is bound:
    >>
    >> cbSubCategoryLo okUp.DataSource = dsCatSubCats.Ta bles["Category"];
    >> cbSubCategoryLo okUp.DisplayMem ber = "CatSubCats.Sub Category";
    >>
    >> Do you have any idea what I'm missing here?
    >>
    >> Chris.
    >>
    >>
    >>
    >>
    >> "Ian Griffiths [C# MVP]" <ian-interact-sw@nospam.nospa m> wrote in message
    >> news:OcWhpggZFH A.3732@TK2MSFTN GP10.phx.gbl...[color=darkred]
    >>> Christopher Weaver wrote:
    >>>>I want to set a value in a specific field in the current row of a
    >>>>DataSet.
    >>>
    >>> So the first thing to know is that the DataSet doesn't have any notion
    >>> of a current row.
    >>>
    >>> In fact strictly speaking, the DataSet doesn't have any notion of a row.
    >>> It just contains a bunch of DataTables. And it's the DataTables that
    >>> contain the rows. But a DataTable doesn't have any notion of a current
    >>> row either, no more than a table in a relational database has a notion
    >>> of a 'current' row.
    >>>
    >>> Currency (in the sense of what is currently selected) is entirely a UI
    >>> thing. It's managed by the currency manager in a Windows Forms
    >>> applciation.
    >>>
    >>>
    >>>> SomeRow["fieldName"] = 'value';
    >>>>
    >>>> But how do I set SomeRow to the row that the user is currently viewing?
    >>>>
    >>>> I tried this:
    >>>>
    >>>> DataRowView drv = (DataRowView) cMgr.Current; //Where cMgr is a
    >>>> CurrencyManager
    >>>> drv["SubCategor y"] = Value;
    >>>>
    >>>> But it doesn't stick. When the user moves off the record and back
    >>>> again, the previous value is restored.
    >>>
    >>> You mean that your changes aren't being stored in the DataTable?
    >>>
    >>> In which case you've misdiagnosed the problem. You *are* successfully
    >>> getting hold of the current row. The reason the changes aren't sticking
    >>> is because you probably need to call EndEdit on the DataRowView.
    >>>
    >>> In a Windows Forms app, edits are provisional until they are applied.
    >>> There are a number of reasons. One is that it may only make sense to
    >>> check constraints once all of the fields for a row have been entered, in
    >>> which case there has to be some point at which you say 'done now!' to
    >>> try and apply the changes.
    >>>
    >>> Also, by convention, in most grid editing UIs in Windows, you can
    >>> usually cancel out of your current row edit.
    >>>
    >>> So the changes you make through the view are probably not sticking
    >>> because the edit is being regarded as provisional, and you never try to
    >>> finish the edit.
    >>>
    >>> At least that's one possibility - try calling EndEdit on the DataRowView
    >>> and see if that helps.
    >>>
    >>>
    >>> Alternatively, just get the Position from the CurrencyManager , and go
    >>> straight to the DataTable object rather than going via the view.
    >>> Something like:
    >>>
    >>> myDataSet.MyTab le[currencyMgr.Pos ition]["field"] = someValue;
    >>>
    >>>
    >>> --
    >>> Ian Griffiths - http://www.interact-sw.co.uk/iangblog/
    >>>[/color]
    >>
    >>[/color]
    >
    >[/color]


    Comment

    • Ian Griffiths [C# MVP]

      #3
      Re: Identifying the Current Row

      Christopher Weaver wrote:[color=blue]
      > Here's the idea. Tasks is a table with a Foreign Key constraint in
      > SubCategory.[/color]

      So a task has to belong to a subcategory? Or a subcategory has to belong to
      a task. I'm trying to get my head around where the primary and foreign keys
      are.

      If I've understood correctly, one of the columns in Tasks is SubCategoryID
      (or something like that), and a FK constraint requires this to correspond to
      a row in the SubCategory table. So SubCategory : Tasks is a 1:Many
      relation. Have I understood, or did I get it backwards?

      I'm not sure because I don't see that one listed here:
      [color=blue]
      > I've brought all of the tables within the same DataSet and created three
      > relationships (1 : m in each case): Task : Activities, Category :
      > SubCategory, and Category : Task.[/color]

      That implies that Tasks has an FK to Category, and that SubCategory also has
      an FK to Category. I don't know what the relationship between Tasks and
      SubCategory is though.


      [color=blue]
      > Since the user can only include items from the finite set of choices
      > within the referenced table, I am providing a ComboBox from which they are
      > constrained to make their choice. Furthermore, the items in that ComboBox
      > are constrained by the choice made in another field, also controled by a
      > ComboBox.[/color]

      Does the combobox list tasks, subcategories, categories, or something else?

      You've got two sets of criteria both constraining what appears in the
      ComboBox then? How are you making that work? Are you setting a filter on
      the view?


      [color=blue]
      > But I still have a problem. As the user moves through the records of the
      > Task table, the ComboBox does not always show the correct value for the
      > field that it is bound to. Sometimes it does, but other times it just
      > reveals a blank. What's really odd is that it will show a blank on a
      > specific row when approached from one direction, say the previous row, but
      > when approached from the following row it shows the correct value.[/color]

      That's really strange. I have to admit I've not seen that happen before.
      I'm afraid I can't offer any suggestions as to what the problem might be
      based on prior experience.

      [color=blue]
      > Also, I must implement the same behavior in a DataGrid. One of the
      > columns needs to have a ComboBox in it. In other words, it needs to
      > function like the property editor pane when editing a property like
      > BorderStyle. Do you know how to do this?[/color]

      I don't use the built-in DataGrid much, so I'll have to leave that for
      someone else...


      --
      Ian Griffiths - http://www.interact-sw.co.uk/iangblog/


      Comment

      • Christopher Weaver

        #4
        Re: Identifying the Current Row

        Thanks for wading through my previous effort. This one should do it.
        Here's the structure, (and with apologies, I didn't design it and would love
        to change it)

        tblTasks has five Foreign Keys: Category, SubCategory, AssignBy, AssignTo,
        and Status. I would like the user to be able to select these values from
        ComboBoxes because I don't want them to have the option of inputting a value
        that's not legal. There is also a relationship between Category and
        SubCategory in that tblSubCategory has a Foreign Key, Category, to
        tblCategory. AssignBy and AssignTo are both linked to tblNames.

        So, each task, tblTask row, must have a Category, SubCategory, AssignBy,
        AssignTo, and Status value that are found in their respective tables.

        Would you recommend that all of these tables be included in one DataSet?
        Presently I have four included: tblTask, tblActivity, tblCategory, and
        tblSubCategory. tblActivity has a Foreign Key to tblTask, as their names
        would imply. The activities for each task are displayed nicely in the
        DataGrid that I've placed on the same form.

        Regarding the DataGrid. You mentioned that you don't work with the built-in
        DataGrid much; what are my other options? Are there other DataGrids
        available or must we create our own?

        I don't know what to make of the inconsistent behavior of the ComboBox Text
        property as I move through the rows of the Task table. If you were to be
        given an assignment like this, what approach would you take? Would you use
        ComboBox?

        Thanks again.

        BTW, are you in England? My mother was born there.


        "Ian Griffiths [C# MVP]" <ian-interact-sw@nospam.nospa m> wrote in message
        news:uE6YzkoZFH A.2128@TK2MSFTN GP15.phx.gbl...[color=blue]
        > Christopher Weaver wrote:[color=green]
        >> Here's the idea. Tasks is a table with a Foreign Key constraint in
        >> SubCategory.[/color]
        >
        > So a task has to belong to a subcategory? Or a subcategory has to belong
        > to a task. I'm trying to get my head around where the primary and foreign
        > keys are.
        >
        > If I've understood correctly, one of the columns in Tasks is SubCategoryID
        > (or something like that), and a FK constraint requires this to correspond
        > to a row in the SubCategory table. So SubCategory : Tasks is a 1:Many
        > relation. Have I understood, or did I get it backwards?
        >
        > I'm not sure because I don't see that one listed here:
        >[color=green]
        >> I've brought all of the tables within the same DataSet and created three
        >> relationships (1 : m in each case): Task : Activities, Category :
        >> SubCategory, and Category : Task.[/color]
        >
        > That implies that Tasks has an FK to Category, and that SubCategory also
        > has an FK to Category. I don't know what the relationship between Tasks
        > and SubCategory is though.
        >
        >
        >[color=green]
        >> Since the user can only include items from the finite set of choices
        >> within the referenced table, I am providing a ComboBox from which they
        >> are constrained to make their choice. Furthermore, the items in that
        >> ComboBox are constrained by the choice made in another field, also
        >> controled by a ComboBox.[/color]
        >
        > Does the combobox list tasks, subcategories, categories, or something
        > else?
        >
        > You've got two sets of criteria both constraining what appears in the
        > ComboBox then? How are you making that work? Are you setting a filter on
        > the view?
        >
        >
        >[color=green]
        >> But I still have a problem. As the user moves through the records of the
        >> Task table, the ComboBox does not always show the correct value for the
        >> field that it is bound to. Sometimes it does, but other times it just
        >> reveals a blank. What's really odd is that it will show a blank on a
        >> specific row when approached from one direction, say the previous row,
        >> but when approached from the following row it shows the correct value.[/color]
        >
        > That's really strange. I have to admit I've not seen that happen before.
        > I'm afraid I can't offer any suggestions as to what the problem might be
        > based on prior experience.
        >
        >[color=green]
        >> Also, I must implement the same behavior in a DataGrid. One of the
        >> columns needs to have a ComboBox in it. In other words, it needs to
        >> function like the property editor pane when editing a property like
        >> BorderStyle. Do you know how to do this?[/color]
        >
        > I don't use the built-in DataGrid much, so I'll have to leave that for
        > someone else...
        >
        >
        > --
        > Ian Griffiths - http://www.interact-sw.co.uk/iangblog/
        >[/color]


        Comment

        Working...