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. C#
  4. DataGrid Cell Edit

DataGrid Cell Edit

Scheduled Pinned Locked Moved C#
comdesignhostingquestionannouncement
6 Posts 2 Posters 0 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.
  • K Offline
    K Offline
    Kayess Tech
    wrote on last edited by
    #1

    Hi guys and gals, I have a windows form with a DateTimePicker and a DataGrid. What I would like to do is double-click a cell in the DataGrid and have that cell update to show the date that is selected by the DateTimePicker. Any suggestions as to how I could do this? TIA Steve Web design and hosting http://www.kayess.com.au

    P 1 Reply Last reply
    0
    • K Kayess Tech

      Hi guys and gals, I have a windows form with a DateTimePicker and a DataGrid. What I would like to do is double-click a cell in the DataGrid and have that cell update to show the date that is selected by the DateTimePicker. Any suggestions as to how I could do this? TIA Steve Web design and hosting http://www.kayess.com.au

      P Offline
      P Offline
      Polis Pilavas
      wrote on last edited by
      #2

      Perhaps you could even embed the DateTimePicker in the DataColumn, so that whenever you click on a cell it drops down so that you can select a date and automatically updates itself. Have a look here[^]. Regards, Polis Can you practice what you teach?

      K 1 Reply Last reply
      0
      • P Polis Pilavas

        Perhaps you could even embed the DateTimePicker in the DataColumn, so that whenever you click on a cell it drops down so that you can select a date and automatically updates itself. Have a look here[^]. Regards, Polis Can you practice what you teach?

        K Offline
        K Offline
        Kayess Tech
        wrote on last edited by
        #3

        Thanks Polis, but that's not what the boss wants I'm afraid. Web design and hosting http://www.kayess.com.au

        P 1 Reply Last reply
        0
        • K Kayess Tech

          Thanks Polis, but that's not what the boss wants I'm afraid. Web design and hosting http://www.kayess.com.au

          P Offline
          P Offline
          Polis Pilavas
          wrote on last edited by
          #4

          In that case, here's what I think. 1. First of all, in order for you to be able to handle the cell you click you have to get the current row by writing something like:

          //Get current row
          CurrencyManager xCM= (CurrencyManager)this.dataGrid2.BindingContext[this.dataGrid2.DataSource, this.dataGrid2.DataMember];
          DataRowView xDRV= (DataRowView)xCM.Current;
          DataRow currRow= xDRV.Row;

          2. Then you move on by specifying that you wish to respond only to mouse-clicks that fall between a cell's range:

          System.Drawing.Point point= new Point(e.X, e.Y);
          DataGrid.HitTestInfo info= this.dataGrid2.HitTest(point);
          if(info.Type == DataGrid.HitTestType.Cell)
          {
          // Do your thing... e.g.
          currRow[myDataTable.Columns["Date"]] = this.dateTimePicker1.Value.ToShortDateString();
          }

          A piece of note here. In order to be able to get the your new point from e.X and e.Y, you will have to write the code in a relevant event (such as your dataGrid's mouseUp event) Something important: The actual change will not take part on the dataGrid itself but on the DataRow of the table that the dataGrid is bound to. By changing the column of your bound table, the value on the dataGrid's cell will automatically change Hope this pleases the boss ;) Regards, Polis Can you practice what you teach?

          K 1 Reply Last reply
          0
          • P Polis Pilavas

            In that case, here's what I think. 1. First of all, in order for you to be able to handle the cell you click you have to get the current row by writing something like:

            //Get current row
            CurrencyManager xCM= (CurrencyManager)this.dataGrid2.BindingContext[this.dataGrid2.DataSource, this.dataGrid2.DataMember];
            DataRowView xDRV= (DataRowView)xCM.Current;
            DataRow currRow= xDRV.Row;

            2. Then you move on by specifying that you wish to respond only to mouse-clicks that fall between a cell's range:

            System.Drawing.Point point= new Point(e.X, e.Y);
            DataGrid.HitTestInfo info= this.dataGrid2.HitTest(point);
            if(info.Type == DataGrid.HitTestType.Cell)
            {
            // Do your thing... e.g.
            currRow[myDataTable.Columns["Date"]] = this.dateTimePicker1.Value.ToShortDateString();
            }

            A piece of note here. In order to be able to get the your new point from e.X and e.Y, you will have to write the code in a relevant event (such as your dataGrid's mouseUp event) Something important: The actual change will not take part on the dataGrid itself but on the DataRow of the table that the dataGrid is bound to. By changing the column of your bound table, the value on the dataGrid's cell will automatically change Hope this pleases the boss ;) Regards, Polis Can you practice what you teach?

            K Offline
            K Offline
            Kayess Tech
            wrote on last edited by
            #5

            Thank you Polis, you are my hero :-D now if I do a if(e.Clicks == 2) in the MouseUp Event that will handle the double-click. Cheers Steve Web design and hosting http://www.kayess.com.au

            P 1 Reply Last reply
            0
            • K Kayess Tech

              Thank you Polis, you are my hero :-D now if I do a if(e.Clicks == 2) in the MouseUp Event that will handle the double-click. Cheers Steve Web design and hosting http://www.kayess.com.au

              P Offline
              P Offline
              Polis Pilavas
              wrote on last edited by
              #6

              Glad I could help Steve Regards, Polis Can you practice what you teach?

              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