Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. Windows Forms
  4. Problem in ToolStripButton (.Net 2008, Framework 3.5).

Problem in ToolStripButton (.Net 2008, Framework 3.5).

Scheduled Pinned Locked Moved Windows Forms
helpquestioncsharpsalestutorial
9 Posts 3 Posters 2 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • P Offline
    P Offline
    priyamtheone
    wrote on last edited by
    #1

    Hi, I have an editable datagridview that's been databound with dataadapter and datatable, and a normal button on a form. When i edit the dgv's cells and click the save button it fires the datagridview_cellvalidating event and if the cell validation is correct it updates and saves the current record to the datasource. Also, before save it commits change in any cell under editing. But when I use a toolstripbutton to do the same work, though it saves the dgv records but doesn't update the current change i.e. it saves the old records that are being fetched when the dgv was populated first during form load. Also, clicking the toolstripbutton doesn't fire datagridview_cellvalidating, datagridview_rowvalidating etc events. What it seems to be is it's not commiting the change in any cell under editing. Example- Dgv While load: CustomerID Customer Inactive ________________________________________ C01 Max Yes C02 John No C03 Alex Yes Dgv while edit but before clicking toolstrip save button: CustomerID Customer Inactive ________________________________________ C01 Max Yes C02 Tim Yes C03 Alex Yes Dgv after clicking the toolstrip save button: CustomerID Customer Inactive ________________________________________ C01 Max Yes C02 John No C03 Alex Yes Why isn't it happening and how can I make it work? Please Help. Please also let me know how can I fire datagridview_cellvalidating, datagridview_rowvalidating and other events; and commit cell value change by clicking toolstripbutton. Regards.

    M 1 Reply Last reply
    0
    • P priyamtheone

      Hi, I have an editable datagridview that's been databound with dataadapter and datatable, and a normal button on a form. When i edit the dgv's cells and click the save button it fires the datagridview_cellvalidating event and if the cell validation is correct it updates and saves the current record to the datasource. Also, before save it commits change in any cell under editing. But when I use a toolstripbutton to do the same work, though it saves the dgv records but doesn't update the current change i.e. it saves the old records that are being fetched when the dgv was populated first during form load. Also, clicking the toolstripbutton doesn't fire datagridview_cellvalidating, datagridview_rowvalidating etc events. What it seems to be is it's not commiting the change in any cell under editing. Example- Dgv While load: CustomerID Customer Inactive ________________________________________ C01 Max Yes C02 John No C03 Alex Yes Dgv while edit but before clicking toolstrip save button: CustomerID Customer Inactive ________________________________________ C01 Max Yes C02 Tim Yes C03 Alex Yes Dgv after clicking the toolstrip save button: CustomerID Customer Inactive ________________________________________ C01 Max Yes C02 John No C03 Alex Yes Why isn't it happening and how can I make it work? Please Help. Please also let me know how can I fire datagridview_cellvalidating, datagridview_rowvalidating and other events; and commit cell value change by clicking toolstripbutton. Regards.

      M Offline
      M Offline
      musefan
      wrote on last edited by
      #2

      use dataGridView.CommitEdit() or EndEdit() to update the cell value as first thing on button click. These function may also cause your validation events to fire (but I am not 100 % on that)

      Life goes very fast. Tomorrow, today is already yesterday.

      P 1 Reply Last reply
      0
      • M musefan

        use dataGridView.CommitEdit() or EndEdit() to update the cell value as first thing on button click. These function may also cause your validation events to fire (but I am not 100 % on that)

        Life goes very fast. Tomorrow, today is already yesterday.

        P Offline
        P Offline
        priyamtheone
        wrote on last edited by
        #3

        I'v already tried both of them but of no use. I think toolstrip is an external band to the form. That's why any action performed on it is not reflected on the form. I'm still looking for it....

        M 1 Reply Last reply
        0
        • P priyamtheone

          I'v already tried both of them but of no use. I think toolstrip is an external band to the form. That's why any action performed on it is not reflected on the form. I'm still looking for it....

          M Offline
          M Offline
          musefan
          wrote on last edited by
          #4

          I don't think that is your problem. I have manage to use a tool strip button to confirm a table edit and it worked for me. Are you sure you have your tool strip button event set to the correct function?

          Life goes very fast. Tomorrow, today is already yesterday.

          P 1 Reply Last reply
          0
          • M musefan

            I don't think that is your problem. I have manage to use a tool strip button to confirm a table edit and it worked for me. Are you sure you have your tool strip button event set to the correct function?

            Life goes very fast. Tomorrow, today is already yesterday.

            P Offline
            P Offline
            priyamtheone
            wrote on last edited by
            #5

            Here's an interesting thing I found. The toolstripbuttons are notorious to be evaluated first before Datagridview. So, if u edit a cell and commit it by going to a different cell or the next row and then press the toolstripbutton to save, it's working fine and can perform the validations, update, save etc. But when u press the toolstripbutton while editing a cell and not committing it manually, there's where it is failing. A workaround can be, write all the requisite codes in a standard button click event, hide the button beneath any other control, and in the toolstripbutton's click event write- StandardButtonName.PerformClick N.B.- My datagridview updates only after clicking the save button. Please let me know ur opinions on this. Thanks.

            M 1 Reply Last reply
            0
            • P priyamtheone

              Here's an interesting thing I found. The toolstripbuttons are notorious to be evaluated first before Datagridview. So, if u edit a cell and commit it by going to a different cell or the next row and then press the toolstripbutton to save, it's working fine and can perform the validations, update, save etc. But when u press the toolstripbutton while editing a cell and not committing it manually, there's where it is failing. A workaround can be, write all the requisite codes in a standard button click event, hide the button beneath any other control, and in the toolstripbutton's click event write- StandardButtonName.PerformClick N.B.- My datagridview updates only after clicking the save button. Please let me know ur opinions on this. Thanks.

              M Offline
              M Offline
              musefan
              wrote on last edited by
              #6

              OK I have dug out my code for you that works just fine.

              void ToolStripButtonClick(object o, EventArgs e)
              {
              ClickFunction();
              }

              void ClickFunction()
              {
              dataGridViewMain.EndEdit();

              //now getting the value of any cell (including one in selection/edit) returns the actual displayed value
              }

              So... ToolStripButtonClick event fired ClickFunction called EndEdit called Now can get correct value ..if this is not working for you then very strange EDIT: Are you getting your value from the datasource? Maybe that is the issue. Can you force an update of that?

              Life goes very fast. Tomorrow, today is already yesterday.

              P 2 Replies Last reply
              0
              • M musefan

                OK I have dug out my code for you that works just fine.

                void ToolStripButtonClick(object o, EventArgs e)
                {
                ClickFunction();
                }

                void ClickFunction()
                {
                dataGridViewMain.EndEdit();

                //now getting the value of any cell (including one in selection/edit) returns the actual displayed value
                }

                So... ToolStripButtonClick event fired ClickFunction called EndEdit called Now can get correct value ..if this is not working for you then very strange EDIT: Are you getting your value from the datasource? Maybe that is the issue. Can you force an update of that?

                Life goes very fast. Tomorrow, today is already yesterday.

                P Offline
                P Offline
                priyamtheone
                wrote on last edited by
                #7

                Ok I'm gonna cross check my code once again for any bug overlooked and let u know.

                1 Reply Last reply
                0
                • M musefan

                  OK I have dug out my code for you that works just fine.

                  void ToolStripButtonClick(object o, EventArgs e)
                  {
                  ClickFunction();
                  }

                  void ClickFunction()
                  {
                  dataGridViewMain.EndEdit();

                  //now getting the value of any cell (including one in selection/edit) returns the actual displayed value
                  }

                  So... ToolStripButtonClick event fired ClickFunction called EndEdit called Now can get correct value ..if this is not working for you then very strange EDIT: Are you getting your value from the datasource? Maybe that is the issue. Can you force an update of that?

                  Life goes very fast. Tomorrow, today is already yesterday.

                  P Offline
                  P Offline
                  priyamtheone
                  wrote on last edited by
                  #8

                  No way, I'm still in darkness. Here's my entire code. please check where's my mistake.

                  Private strSql As String
                  Private da As SqlDataAdapter
                  Private bs As BindingSource
                  Private dtbl As DataTable

                  Private Sub PopulateDgv(Optional ByVal strQuery As String = Nothing)
                      Dim bs As BindingSource
                      Dim cb As SqlCommandBuilder
                  
                      Try
                          RoomPriceID = New DataGridViewTextBoxColumn
                          PeriodDate = New DataGridViewTextBoxColumn
                          Day = New DataGridViewTextBoxColumn
                          Cost = New DataGridViewTextBoxColumn
                          Price = New DataGridViewTextBoxColumn
                          Allotment = New DataGridViewTextBoxColumn
                          Notes = New DataGridViewTextBoxColumn
                  
                          dgvPriceList.Columns.Clear()
                          dgvPriceList.DataSource = Nothing
                          dgvPriceList.Columns.AddRange(New DataGridViewColumn() {RoomPriceID, PeriodDate, Day, Cost, Price, Allotment, Notes})
                  
                          RoomPriceID.Name = "RoomPriceID"
                          RoomPriceID.DataPropertyName = "RoomPriceID"
                          RoomPriceID.Visible = False
                          PeriodDate.Name = "PeriodDate"
                          PeriodDate.DataPropertyName = "PeriodDate"
                          PeriodDate.ReadOnly = True
                          PeriodDate.HeaderText = "Date"
                          PeriodDate.Width = 100
                          Day.Name = "Day"
                          Day.DataPropertyName = "Day"
                          Day.ReadOnly = True
                          Day.HeaderText = "Day"
                          Day.Width = 100
                          Cost.Name = "Cost"
                          Cost.DataPropertyName = "Cost"
                          Cost.HeaderText = "Cost"
                          Cost.HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleRight
                          Cost.CellTemplate.Style.Alignment = DataGridViewContentAlignment.MiddleRight
                          Cost.Width = 100
                          Price.Name = "Price"
                          Price.DataPropertyName = "Price"
                          Price.HeaderText = "Price"
                          Price.HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleRight
                          Price.CellTemplate.Style.Alignment = DataGridViewContentAlignment.MiddleRight
                          Price.Width = 100
                          Allotment.Name = "Allotment"
                          Allotment.DataPropertyName = "Allotment"
                          Allotment.HeaderText = "Allotment"
                          Allotment.HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleRight
                          Allotment.CellTemplate.Style.Alignment = DataGridViewContentAlignment.MiddleRight
                          Allotment.Width = 100
                          Notes.Name = "Note
                  
                  F 1 Reply Last reply
                  0
                  • P priyamtheone

                    No way, I'm still in darkness. Here's my entire code. please check where's my mistake.

                    Private strSql As String
                    Private da As SqlDataAdapter
                    Private bs As BindingSource
                    Private dtbl As DataTable

                    Private Sub PopulateDgv(Optional ByVal strQuery As String = Nothing)
                        Dim bs As BindingSource
                        Dim cb As SqlCommandBuilder
                    
                        Try
                            RoomPriceID = New DataGridViewTextBoxColumn
                            PeriodDate = New DataGridViewTextBoxColumn
                            Day = New DataGridViewTextBoxColumn
                            Cost = New DataGridViewTextBoxColumn
                            Price = New DataGridViewTextBoxColumn
                            Allotment = New DataGridViewTextBoxColumn
                            Notes = New DataGridViewTextBoxColumn
                    
                            dgvPriceList.Columns.Clear()
                            dgvPriceList.DataSource = Nothing
                            dgvPriceList.Columns.AddRange(New DataGridViewColumn() {RoomPriceID, PeriodDate, Day, Cost, Price, Allotment, Notes})
                    
                            RoomPriceID.Name = "RoomPriceID"
                            RoomPriceID.DataPropertyName = "RoomPriceID"
                            RoomPriceID.Visible = False
                            PeriodDate.Name = "PeriodDate"
                            PeriodDate.DataPropertyName = "PeriodDate"
                            PeriodDate.ReadOnly = True
                            PeriodDate.HeaderText = "Date"
                            PeriodDate.Width = 100
                            Day.Name = "Day"
                            Day.DataPropertyName = "Day"
                            Day.ReadOnly = True
                            Day.HeaderText = "Day"
                            Day.Width = 100
                            Cost.Name = "Cost"
                            Cost.DataPropertyName = "Cost"
                            Cost.HeaderText = "Cost"
                            Cost.HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleRight
                            Cost.CellTemplate.Style.Alignment = DataGridViewContentAlignment.MiddleRight
                            Cost.Width = 100
                            Price.Name = "Price"
                            Price.DataPropertyName = "Price"
                            Price.HeaderText = "Price"
                            Price.HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleRight
                            Price.CellTemplate.Style.Alignment = DataGridViewContentAlignment.MiddleRight
                            Price.Width = 100
                            Allotment.Name = "Allotment"
                            Allotment.DataPropertyName = "Allotment"
                            Allotment.HeaderText = "Allotment"
                            Allotment.HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleRight
                            Allotment.CellTemplate.Style.Alignment = DataGridViewContentAlignment.MiddleRight
                            Allotment.Width = 100
                            Notes.Name = "Note
                    
                    F Offline
                    F Offline
                    fnoppers
                    wrote on last edited by
                    #9

                    had a very similar prb & lost nearly my mind. prb was the datatable. in my case solution was "dtbl.AcceptChanges();" after filling was done.

                    1 Reply Last reply
                    0
                    Reply
                    • Reply as topic
                    Log in to reply
                    • Oldest to Newest
                    • Newest to Oldest
                    • Most Votes


                    • Login

                    • Don't have an account? Register

                    • Login or register to search.
                    • First post
                      Last post
                    0
                    • Categories
                    • Recent
                    • Tags
                    • Popular
                    • World
                    • Users
                    • Groups