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. Setting a default value for a datagridviewtextbox column

Setting a default value for a datagridviewtextbox column

Scheduled Pinned Locked Moved C#
question
3 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.
  • B Offline
    B Offline
    bluewavestrider
    wrote on last edited by
    #1

    I have a datagridview and would like one of it's txtBox columns to contain the Current date by default. Which of the datagridview's events should I use to be able to assign to it's EdittedFormatedValue or something?

    Live in fragments no longer. Only connect.

    L 1 Reply Last reply
    0
    • B bluewavestrider

      I have a datagridview and would like one of it's txtBox columns to contain the Current date by default. Which of the datagridview's events should I use to be able to assign to it's EdittedFormatedValue or something?

      Live in fragments no longer. Only connect.

      L Offline
      L Offline
      Lisa Jorgensen
      wrote on last edited by
      #2

      There's more than one way to approach this. One way is to set the DataGridViewTextBoxColumn's DefaultValue property to the current date. This means that all rows with an unspecified value in that column will get the current date. One problem is that the date will remain the same even if the date changes while the application is still running (i.e. new rows will still get the no-longer-current date). Another approach is to use the DataGridView's DefaultValuesNeeded event to set the current date, e.g. (if the target DataGridViewTextBoxColumn is named "DateColumn"):

      private void dataGridView1_DefaultValuesNeeded(object sender, DataGridViewRowEventArgs e)
      {
      e.Row.Cells["DateColumn"].Value = DateTime.Now.ToShortDateString();
      }

      This affects only new rows; existing rows will not be updated. If you do both, then existing rows with an unspecified value will get the date on which the application began running and new rows will get the current date.

      B 1 Reply Last reply
      0
      • L Lisa Jorgensen

        There's more than one way to approach this. One way is to set the DataGridViewTextBoxColumn's DefaultValue property to the current date. This means that all rows with an unspecified value in that column will get the current date. One problem is that the date will remain the same even if the date changes while the application is still running (i.e. new rows will still get the no-longer-current date). Another approach is to use the DataGridView's DefaultValuesNeeded event to set the current date, e.g. (if the target DataGridViewTextBoxColumn is named "DateColumn"):

        private void dataGridView1_DefaultValuesNeeded(object sender, DataGridViewRowEventArgs e)
        {
        e.Row.Cells["DateColumn"].Value = DateTime.Now.ToShortDateString();
        }

        This affects only new rows; existing rows will not be updated. If you do both, then existing rows with an unspecified value will get the date on which the application began running and new rows will get the current date.

        B Offline
        B Offline
        bluewavestrider
        wrote on last edited by
        #3

        Thanks Lisa

        Live in fragments no longer. Only connect.

        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