How do convince a DataGridView it's been edited?

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

    #1

    How do convince a DataGridView it's been edited?

    The situation:

    * I have a ButtonColumn in a DataGridView.
    * When the user preses one of the buttons, a dialog appears.
    * Based on what the user selects in the dialog, data is entered
    programmaticall y into the the underlying cell (i.e., I'm setting Value
    property of the cell based on user input, but the user is not directly
    entering any data).

    The problem:

    The DataGridView doesn't seem believe the user has edited the row, so
    if the user clicks or tabs somewhere else (which causes the
    DataGridView to lose focus), all the changes to the row get
    cleared/canceled.

    Other information:

    Looking at the RowHeaderCell for the row as it's being edited, it
    changes from a large asterisk[*] to a right arrow + asterisk [>*].
    This is different than if the user directly edits a cell by typing (in
    that case, you'd get a pencil icon while the cell is in edit mode
    followed by a large right arrow after the change is committed).

    My question:

    So, how do I programmaticall y tell the DataGridView that the user
    really has touched/edited this row and that the data should stick even
    if the DataGridView loses focus.

    Thanks!

    -Dan

  • =?Utf-8?B?Q2lhcmFuIE8nJ0Rvbm5lbGw=?=

    #2
    RE: How do convince a DataGridView it's been edited?

    If the grid is bound, edit the underlying data source. I dont ever add rows
    and columns to a grid with out binding it. I make a DataTable first and let
    that be the container for the data. Then I can bind the grid to it and update
    the DataTable when needs be.

    --
    Ciaran O''Donnell



    "DanThMan" wrote:
    The situation:
    >
    * I have a ButtonColumn in a DataGridView.
    * When the user preses one of the buttons, a dialog appears.
    * Based on what the user selects in the dialog, data is entered
    programmaticall y into the the underlying cell (i.e., I'm setting Value
    property of the cell based on user input, but the user is not directly
    entering any data).
    >
    The problem:
    >
    The DataGridView doesn't seem believe the user has edited the row, so
    if the user clicks or tabs somewhere else (which causes the
    DataGridView to lose focus), all the changes to the row get
    cleared/canceled.
    >
    Other information:
    >
    Looking at the RowHeaderCell for the row as it's being edited, it
    changes from a large asterisk[*] to a right arrow + asterisk [>*].
    This is different than if the user directly edits a cell by typing (in
    that case, you'd get a pencil icon while the cell is in edit mode
    followed by a large right arrow after the change is committed).
    >
    My question:
    >
    So, how do I programmaticall y tell the DataGridView that the user
    really has touched/edited this row and that the data should stick even
    if the DataGridView loses focus.
    >
    Thanks!
    >
    -Dan
    >
    >

    Comment

    • DanThMan

      #3
      Re: How do convince a DataGridView it's been edited?

      Thanks to both of you for your replies.

      The problem is that I'm not creating a new row programmaticall y then
      filling it with values. Rather, the user is interacting with the "row
      for new records" and I'm programmaticall y entering values into that row
      based on what the user does.

      If I were just trying to add a new record entirely through code, I
      could easily see how this would be done using the underlying DataTable.
      But, in this case, if I were to add a new row to the DataTable when the
      user interacts with row for new records, It seems like I'd just end up
      with two unsync'd versions of the same row.

      I like the SendKeys idea, but it only works for tables where there is a
      visible DataGridViewTex tBoxCell available to send input to. Sending
      keys to a DataGridViewBut tonCell, for example, doesn't trick the
      DataGridView into thinking the row has been edited.

      Is there really no way to simply set the status of a row to
      edited/changed?

      There seems to be a way to set the DirtyState of a cell to True
      (NotifyCurrentC ellDirty), but this still isn't enough to prevent the
      problem of all values disappearing when the row loses focus. I also
      can't seem to programmaticall y change the CurrentCell to any other type
      of cell besides a DataGridViewTex tBoxCell.

      Right now, the only solution I can think of is to create a dummy
      DataGridViewTex tBoxColumn in a every table just so that I can use
      SendKeys with it. Someone please tell me I don't have to resort to
      something that kludgey!

      Thanks again,

      -Dan

      On Jan 25, 2:41 am, "ClayB" <c...@syncfusio n.comwrote:
      One other comment in addition to what Ciaran suggested is that the row
      with the * next to it is the AddNew row so it really does not exist in
      your DataSource. So, if you want to use Ciaran's suggestion of
      interacting directly with the DataSource, then you would have to
      actually add a new item (DataRow) in some manner, maybe calling
      DataTable.NewRo w, change the values in this new row, and then call
      DataTable.Rows. Add to add the new row into the DataTable.
      >
      If you are only changing a single value in this new row, and want to do
      it through the UI (and not directly work with the DataTable), then you
      can try using SendKeys to simiulate keystrokes into the grid to set a
      value, but there may be consequences of doing it this way. Manipulating
      the data in the DataSource is probably the most robust way to do this.
      >
      //set the current cell and set it editing
      this.dataGridVi ew1.CurrentCell = this.dataGridVi ew1[2, 10];
      this.dataGridVi ew1.BeginEdit(t rue);
      //send the keystrokes and end the editing
      SendKeys.Send(S omeValue.ToStri ng());
      this.dataGridVi ew1.EndEdit();
      >
      =============== ==========
      Clay Burch
      Syncfusion, Inc.

      Comment

      • RobinS

        #4
        Re: How do convince a DataGridView it's been edited?

        Why can't you create a new row for your datatable, and put the user's
        entries into it, then add it to the DataGridView?

        What are you binding your datagridview to? Is it by chance a list of
        objects? Or is it a datatable or dataset?

        Robin S.
        ---------------------------------
        "DanThMan" <danthman@cox.n etwrote in message
        news:1169758960 .811106.19440@v 45g2000cwv.goog legroups.com...
        Thanks to both of you for your replies.
        >
        The problem is that I'm not creating a new row programmaticall y then
        filling it with values. Rather, the user is interacting with the "row
        for new records" and I'm programmaticall y entering values into that
        row
        based on what the user does.
        >
        If I were just trying to add a new record entirely through code, I
        could easily see how this would be done using the underlying
        DataTable.
        But, in this case, if I were to add a new row to the DataTable when
        the
        user interacts with row for new records, It seems like I'd just end up
        with two unsync'd versions of the same row.
        >
        I like the SendKeys idea, but it only works for tables where there is
        a
        visible DataGridViewTex tBoxCell available to send input to. Sending
        keys to a DataGridViewBut tonCell, for example, doesn't trick the
        DataGridView into thinking the row has been edited.
        >
        Is there really no way to simply set the status of a row to
        edited/changed?
        >
        There seems to be a way to set the DirtyState of a cell to True
        (NotifyCurrentC ellDirty), but this still isn't enough to prevent the
        problem of all values disappearing when the row loses focus. I also
        can't seem to programmaticall y change the CurrentCell to any other
        type
        of cell besides a DataGridViewTex tBoxCell.
        >
        Right now, the only solution I can think of is to create a dummy
        DataGridViewTex tBoxColumn in a every table just so that I can use
        SendKeys with it. Someone please tell me I don't have to resort to
        something that kludgey!
        >
        Thanks again,
        >
        -Dan
        >
        On Jan 25, 2:41 am, "ClayB" <c...@syncfusio n.comwrote:
        >One other comment in addition to what Ciaran suggested is that the
        >row
        >with the * next to it is the AddNew row so it really does not exist
        >in
        >your DataSource. So, if you want to use Ciaran's suggestion of
        >interacting directly with the DataSource, then you would have to
        >actually add a new item (DataRow) in some manner, maybe calling
        >DataTable.NewR ow, change the values in this new row, and then call
        >DataTable.Rows .Add to add the new row into the DataTable.
        >>
        >If you are only changing a single value in this new row, and want to
        >do
        >it through the UI (and not directly work with the DataTable), then
        >you
        >can try using SendKeys to simiulate keystrokes into the grid to set a
        >value, but there may be consequences of doing it this way.
        >Manipulating
        >the data in the DataSource is probably the most robust way to do
        >this.
        >>
        > //set the current cell and set it editing
        > this.dataGridVi ew1.CurrentCell = this.dataGridVi ew1[2,
        >10];
        > this.dataGridVi ew1.BeginEdit(t rue);
        > //send the keystrokes and end the editing
        > SendKeys.Send(S omeValue.ToStri ng());
        > this.dataGridVi ew1.EndEdit();
        >>
        >============== ===========
        >Clay Burch
        >Syncfusion, Inc.
        >

        Comment

        • DanThMan

          #5
          Re: How do convince a DataGridView it's been edited?

          It looks like the solution was to place the following code in the
          DataGridView'sR owLeave event handler:

          'make sure row is not empty
          'binding = reference to the binding for the DataGridView
          binding.Suspend Binding()
          binding.ResumeB inding()

          I'd feel better about this if I knew why it worked. I just sort of
          stumbled upon it after playing with several different solutions,
          including using SendKeys and adding a row to the DataTable.

          Thanks for all the advice,

          -Dan

          On Jan 25, 2:09 pm, "RobinS" <Rob...@NoSpam. yah.nonewrote:
          Why can't you create a new row for your datatable, and put the user's
          entries into it, then add it to the DataGridView?
          >
          What are you binding your datagridview to? Is it by chance a list of
          objects? Or is it a datatable or dataset?
          >
          Robin S.
          ---------------------------------"DanThMan" <danth...@cox.n etwrote in messagenews:116 9758960.811106. 19440@v45g2000c wv.googlegroups .com...
          >
          Thanks to both of you for your replies.
          >
          The problem is that I'm not creating a new row programmaticall y then
          filling it with values. Rather, the user is interacting with the "row
          for new records" and I'm programmaticall y entering values into that
          row
          based on what the user does.
          >
          If I were just trying to add a new record entirely through code, I
          could easily see how this would be done using the underlying
          DataTable.
          But, in this case, if I were to add a new row to the DataTable when
          the
          user interacts with row for new records, It seems like I'd just end up
          with two unsync'd versions of the same row.
          >
          I like the SendKeys idea, but it only works for tables where there is
          a
          visible DataGridViewTex tBoxCell available to send input to. Sending
          keys to a DataGridViewBut tonCell, for example, doesn't trick the
          DataGridView into thinking the row has been edited.
          >
          Is there really no way to simply set the status of a row to
          edited/changed?
          >
          There seems to be a way to set the DirtyState of a cell to True
          (NotifyCurrentC ellDirty), but this still isn't enough to prevent the
          problem of all values disappearing when the row loses focus. I also
          can't seem to programmaticall y change the CurrentCell to any other
          type
          of cell besides a DataGridViewTex tBoxCell.
          >
          Right now, the only solution I can think of is to create a dummy
          DataGridViewTex tBoxColumn in a every table just so that I can use
          SendKeys with it. Someone please tell me I don't have to resort to
          something that kludgey!
          >
          Thanks again,
          >
          -Dan
          >
          On Jan 25, 2:41 am, "ClayB" <c...@syncfusio n.comwrote:
          One other comment in addition to what Ciaran suggested is that the
          row
          with the * next to it is the AddNew row so it really does not exist
          in
          your DataSource. So, if you want to use Ciaran's suggestion of
          interacting directly with the DataSource, then you would have to
          actually add a new item (DataRow) in some manner, maybe calling
          DataTable.NewRo w, change the values in this new row, and then call
          DataTable.Rows. Add to add the new row into the DataTable.
          >
          If you are only changing a single value in this new row, and want to
          do
          it through the UI (and not directly work with the DataTable), then
          you
          can try using SendKeys to simiulate keystrokes into the grid to set a
          value, but there may be consequences of doing it this way.
          Manipulating
          the data in the DataSource is probably the most robust way to do
          this.
          >
          //set the current cell and set it editing
          this.dataGridVi ew1.CurrentCell = this.dataGridVi ew1[2,
          10];
          this.dataGridVi ew1.BeginEdit(t rue);
          //send the keystrokes and end the editing
          SendKeys.Send(S omeValue.ToStri ng());
          this.dataGridVi ew1.EndEdit();
          >
          =============== ==========
          Clay Burch
          Syncfusion, Inc.

          Comment

          • William LaMartin

            #6
            Re: How do convince a DataGridView it's been edited?

            I have found that if I want to be sure that changes to a cell in a
            datagridview are made that I need to click on any other row before doing the
            update command for the dataadapter.

            I am sure there is another way.


            "DanThMan" <danthman@cox.n etwrote in message
            news:1169794909 .710251.255870@ p10g2000cwp.goo glegroups.com.. .
            It looks like the solution was to place the following code in the
            DataGridView'sR owLeave event handler:
            >
            'make sure row is not empty
            'binding = reference to the binding for the DataGridView
            binding.Suspend Binding()
            binding.ResumeB inding()
            >
            I'd feel better about this if I knew why it worked. I just sort of
            stumbled upon it after playing with several different solutions,
            including using SendKeys and adding a row to the DataTable.
            >
            Thanks for all the advice,
            >
            -Dan
            >
            On Jan 25, 2:09 pm, "RobinS" <Rob...@NoSpam. yah.nonewrote:
            >Why can't you create a new row for your datatable, and put the user's
            >entries into it, then add it to the DataGridView?
            >>
            >What are you binding your datagridview to? Is it by chance a list of
            >objects? Or is it a datatable or dataset?
            >>
            >Robin S.
            >---------------------------------"DanThMan" <danth...@cox.n etwrote in
            >messagenews:11 69758960.811106 .19440@v45g2000 cwv.googlegroup s.com...
            >>
            Thanks to both of you for your replies.
            >>
            The problem is that I'm not creating a new row programmaticall y then
            filling it with values. Rather, the user is interacting with the "row
            for new records" and I'm programmaticall y entering values into that
            row
            based on what the user does.
            >>
            If I were just trying to add a new record entirely through code, I
            could easily see how this would be done using the underlying
            DataTable.
            But, in this case, if I were to add a new row to the DataTable when
            the
            user interacts with row for new records, It seems like I'd just end up
            with two unsync'd versions of the same row.
            >>
            I like the SendKeys idea, but it only works for tables where there is
            a
            visible DataGridViewTex tBoxCell available to send input to. Sending
            keys to a DataGridViewBut tonCell, for example, doesn't trick the
            DataGridView into thinking the row has been edited.
            >>
            Is there really no way to simply set the status of a row to
            edited/changed?
            >>
            There seems to be a way to set the DirtyState of a cell to True
            (NotifyCurrentC ellDirty), but this still isn't enough to prevent the
            problem of all values disappearing when the row loses focus. I also
            can't seem to programmaticall y change the CurrentCell to any other
            type
            of cell besides a DataGridViewTex tBoxCell.
            >>
            Right now, the only solution I can think of is to create a dummy
            DataGridViewTex tBoxColumn in a every table just so that I can use
            SendKeys with it. Someone please tell me I don't have to resort to
            something that kludgey!
            >>
            Thanks again,
            >>
            -Dan
            >>
            On Jan 25, 2:41 am, "ClayB" <c...@syncfusio n.comwrote:
            >One other comment in addition to what Ciaran suggested is that the
            >row
            >with the * next to it is the AddNew row so it really does not exist
            >in
            >your DataSource. So, if you want to use Ciaran's suggestion of
            >interacting directly with the DataSource, then you would have to
            >actually add a new item (DataRow) in some manner, maybe calling
            >DataTable.NewR ow, change the values in this new row, and then call
            >DataTable.Rows .Add to add the new row into the DataTable.
            >>
            >If you are only changing a single value in this new row, and want to
            >do
            >it through the UI (and not directly work with the DataTable), then
            >you
            >can try using SendKeys to simiulate keystrokes into the grid to set a
            >value, but there may be consequences of doing it this way.
            >Manipulating
            >the data in the DataSource is probably the most robust way to do
            >this.
            >>
            > //set the current cell and set it editing
            > this.dataGridVi ew1.CurrentCell = this.dataGridVi ew1[2,
            >10];
            > this.dataGridVi ew1.BeginEdit(t rue);
            > //send the keystrokes and end the editing
            > SendKeys.Send(S omeValue.ToStri ng());
            > this.dataGridVi ew1.EndEdit();
            >>
            >============== ===========
            >Clay Burch
            >Syncfusion, Inc.
            >

            Comment

            Working...