Search code examples
c#winformsdatagridview

Button text in gridview is not changing windows form


i created a gridview via code and i want to Change button text for the clicked row from + to - when this is clicked by the user. I have checked multiple code below code is currently i am using and trying to change the text from + to - and vice versa in below ParentGrid_CellContentClick

private void InitializeParentGrid()
    {
        parentGrid = new DataGridView
        {
            Dock = DockStyle.Fill,
            AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill,
            RowHeadersVisible = false,
            AllowUserToAddRows = false,
            ColumnHeadersVisible = false // Hide column headers
        };

        // Add Expand button column
        var expandColumn = new DataGridViewButtonColumn
        {
            Name = "Expand",
            HeaderText = "",
            Text = "+",
            UseColumnTextForButtonValue = true,
            Width = 30, // Set a small width
            AutoSizeMode = DataGridViewAutoSizeColumnMode.None,

        };
        expandColumn.UseColumnTextForButtonValue = true;
        parentGrid.Columns.Add(expandColumn);

        // Add Year column
        var yearColumn = new DataGridViewTextBoxColumn
        {
            Name = "Year",
            HeaderText = "Year",
            DataPropertyName = "Year"
        };
        parentGrid.Columns.Add(yearColumn);

        // Add Parent Grid to GroupBox1
        groupBox1.Controls.Add(parentGrid);

        // Parent grid events
        parentGrid.CellContentClick += ParentGrid_CellContentClick;
    } 

And here i am changing the grid cell value + to - after click

private void ParentGrid_CellContentClick(object sender, DataGridViewCellEventArgs e)
    {
        if (e.RowIndex < 0) return;  

        if (e.ColumnIndex == 0)  
        {
            var buttonCell = (DataGridViewButtonCell)parentGrid.Rows[e.RowIndex].Cells[0];

            if (expandedRowIndex == e.RowIndex)
            {
                // Collapse if already expanded
                RemoveChildGrid();
                expandedRowIndex = -1;

                // Change the button text back to "+"
                buttonCell.Value = "+"; // Change button text for the clicked row
            }
            else
            {
                // Collapse any existing child grid first
                RemoveChildGrid();

                // Expand new child grid
                expandedRowIndex = e.RowIndex;
                AddChildGrid(e.RowIndex);

                // Change the button text to "-"
                buttonCell.Value = "-"; // Change button text for the clicked row
            }

            // Force the DataGridView to redraw this specific cell
            parentGrid.InvalidateCell(buttonCell);
        }
    }

Solution

  • What if you tried removing those UseColumnTextForButtonValue statements and initialized whenever a new row is added?

    expander column

    private void InitializeParentGrid()
    {
        parentGrid = new DataGridView
        {
            Dock = DockStyle.Fill,
            AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill,
            RowHeadersVisible = false,
            AllowUserToAddRows = false,
            ColumnHeadersVisible = false // Hide column headers
        };
        var expandColumn = new DataGridViewButtonColumn
        {
            Name = "Expand",
            HeaderText = "",
            Width = 30, // Set a small width
            AutoSizeMode = DataGridViewAutoSizeColumnMode.None,
        };
        parentGrid.Columns.Add(expandColumn);
    
        var yearColumn = new DataGridViewTextBoxColumn
        {
            Name = "Year",
            HeaderText = "Year",
            DataPropertyName = "Year"
        };
        parentGrid.Columns.Add(yearColumn);
    
        groupBox1.Controls.Add(parentGrid);
        parentGrid.CellContentClick += ParentGrid_CellContentClick;
        parentGrid.RowsAdded += (sender, e) =>
        {
            var exp = parentGrid.Columns["Expand"].Index;
            if (parentGrid.Rows[e.RowIndex].Cells[exp] is DataGridViewButtonCell button)
            {
                button.Value = "▶";
            }
        };
        parentGrid.Rows.Add();
        parentGrid.Rows.Add();
        parentGrid.Rows.Add();
    }
    
    private void ParentGrid_CellContentClick(object? sender, DataGridViewCellEventArgs e)
    {
        if (e.RowIndex == -1 && e.ColumnIndex == -1 ) return;
        if (parentGrid?[e.ColumnIndex, e.RowIndex] is DataGridViewButtonCell button)
        {
            button.Value = 
                Equals(button.Value, "▶")
                ? "▽"
                : "▶";
        }
    }