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. Set DataGridView cell value and add a new row

Set DataGridView cell value and add a new row

Scheduled Pinned Locked Moved Windows Forms
question
1 Posts 1 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.
  • U Offline
    U Offline
    Uros Calakovic
    wrote on last edited by
    #1

    I have an unbound DataGridView control with two columns. The first one is a DataGridViewButtonColumn and the second DataGridViewTextBoxColumn. When my form is first shown the DGW is empty and the only row is the one with the * in the row header (new/empty row) When I click a cell in the first column an OpenFileDialog gets shown and when I select a file the file name is written in the second cell in the same row. This works, but the row is still marked with * and a new row isn't added. If I edit the second cell manually, the row header is marked with the pencil sign and a new/empty row is added below. Is there a way to achieve this when I edit a cell programatically? Here is my current code:

        private void dataGridView1\_CellContentClick(
            object sender, DataGridViewCellEventArgs e)
        {
            if (e.ColumnIndex == 0)
            {
                SelectFile(e.RowIndex);
            }
        }
    
        private void SelectFile(int rowIndex)
        {
            if (openFileDialog.ShowDialog() == DialogResult.OK)
            {
                dataGridView1.Rows\[rowIndex\].Cells\[1\].Value =
                    openFileDialog.FileName;
                dataGridView1.CurrentCell = dataGridView1.Rows\[rowIndex\].Cells\[1\];
            }
        }
    

    The bearing of a child takes nine months, no matter how many women are assigned.

    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