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. How to change the button's value in Datagrid

How to change the button's value in Datagrid

Scheduled Pinned Locked Moved C#
helptutorialquestion
4 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.
  • D Offline
    D Offline
    dwark106
    wrote on last edited by
    #1

    Hi Everyone, I am using button's obj in datagridview and set the flag as true to show the text value of the button (Name of the Button). If user clicks on the button, its changes the button’s name based upon the criteria. I am doing very simple code here. I set button’s name as “buttonColumn.UseColumnTextForButtonValue = true;” Now If I want to change the value with following settings. It did not work it out. dataGridView1[e.ColumnIndex, e.RowIndex].Value = "America"; It is still taking "Name" Can you please help me on this? private void CreateButtonColumn() { // Initialize the button column. DataGridViewButtonColumn buttonColumn = new DataGridViewButtonColumn(); // Add the button column to the control. buttonColumn = new DataGridViewButtonColumn(); buttonColumn.Name = "Name"; buttonColumn.Text = "Name"; buttonColumn.Width = 110; buttonColumn.UseColumnTextForButtonValue = true; dataGridView1.Columns.Insert(0, buttonColumn); dataGridView1.Rows.Add(2); dataGridView1.CellClick += new DataGridViewCellEventHandler(dataGridView1_CellClick); } void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e) { if (Convert.ToInt32(e.ColumnIndex) == 0) { dataGridView1[e.ColumnIndex, e.RowIndex].Value = "America"; } } Thanks.

    H 1 Reply Last reply
    0
    • D dwark106

      Hi Everyone, I am using button's obj in datagridview and set the flag as true to show the text value of the button (Name of the Button). If user clicks on the button, its changes the button’s name based upon the criteria. I am doing very simple code here. I set button’s name as “buttonColumn.UseColumnTextForButtonValue = true;” Now If I want to change the value with following settings. It did not work it out. dataGridView1[e.ColumnIndex, e.RowIndex].Value = "America"; It is still taking "Name" Can you please help me on this? private void CreateButtonColumn() { // Initialize the button column. DataGridViewButtonColumn buttonColumn = new DataGridViewButtonColumn(); // Add the button column to the control. buttonColumn = new DataGridViewButtonColumn(); buttonColumn.Name = "Name"; buttonColumn.Text = "Name"; buttonColumn.Width = 110; buttonColumn.UseColumnTextForButtonValue = true; dataGridView1.Columns.Insert(0, buttonColumn); dataGridView1.Rows.Add(2); dataGridView1.CellClick += new DataGridViewCellEventHandler(dataGridView1_CellClick); } void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e) { if (Convert.ToInt32(e.ColumnIndex) == 0) { dataGridView1[e.ColumnIndex, e.RowIndex].Value = "America"; } } Thanks.

      H Offline
      H Offline
      Henry Minute
      wrote on last edited by
      #2

      I've not tried this but from your code, an obvious thing to try would seem to be

      void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
      {
      if (Convert.ToInt32(e.ColumnIndex) == 0)
      {
      ((DataGridViewButtonColumn)dataGridView1.Columns[e.ColumnIndex]).UseColumnTextForButtonValue = false;
      dataGridView1[e.ColumnIndex, e.RowIndex].Value = "America";
      }
      }

      Hope this helps. :)

      Henry Minute Do not read medical books! You could die of a misprint. - Mark Twain Girl: (staring) "Why do you need an icy cucumber?" “I want to report a fraud. The government is lying to us all.”

      D 1 Reply Last reply
      0
      • H Henry Minute

        I've not tried this but from your code, an obvious thing to try would seem to be

        void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
        {
        if (Convert.ToInt32(e.ColumnIndex) == 0)
        {
        ((DataGridViewButtonColumn)dataGridView1.Columns[e.ColumnIndex]).UseColumnTextForButtonValue = false;
        dataGridView1[e.ColumnIndex, e.RowIndex].Value = "America";
        }
        }

        Hope this helps. :)

        Henry Minute Do not read medical books! You could die of a misprint. - Mark Twain Girl: (staring) "Why do you need an icy cucumber?" “I want to report a fraud. The government is lying to us all.”

        D Offline
        D Offline
        dwark106
        wrote on last edited by
        #3

        Thanks. This is very good one. Just to mention one thing here. Its changes all the rows with new value "America". I want to updates the same cell where was cliked. Once Again Thanks

        H 1 Reply Last reply
        0
        • D dwark106

          Thanks. This is very good one. Just to mention one thing here. Its changes all the rows with new value "America". I want to updates the same cell where was cliked. Once Again Thanks

          H Offline
          H Offline
          Henry Minute
          wrote on last edited by
          #4

          Ah! :-O Well, it was worth a try. I have searched through my own code for examples of using a DataGridviewButtonColumn and all of the examples I found used the button click to load a data entry form to get the required data. on return from the data entry form they used the following syntax to set the value(s) entered:

          void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
          {
          if (Convert.ToInt32(e.ColumnIndex) == 0)
          {
          dataGridView1.Rows[e.RowIndex].Cells["Name"].Value = "America";
          }
          }

          which is setting the value of the appropriate Cell in the underlying DataGridViewRow.

          Henry Minute Do not read medical books! You could die of a misprint. - Mark Twain Girl: (staring) "Why do you need an icy cucumber?" “I want to report a fraud. The government is lying to us all.”

          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