-2

I created a user control which has datagridview. Then I added row and column dynamically from textfile in datagridview.

My problem is I need column which has buttons in each row. In first row, the text of button is 'Test1' and in second row 'Test2' not the same text.

After searched on google I tried this code

var testButton = new DataGridViewButtonColumn();
testButton.Name = "Test";
testButton.HeaderText = "Test";
testButton.UseColumnTextForButtonValue = true;

testButton.Text = "Test1";
this.dataGridView1.Columns.Add(testButton);

But it gives me both button text as 'Test1'. enter image description here

Micha Wiedenmann
  • 19,979
  • 21
  • 92
  • 137
User0804
  • 77
  • 2
  • 12
  • 2
    Possible duplicate of [DatagridViewButtonColumn with different Text and different Functionality for different Rows](https://stackoverflow.com/questions/36354700/datagridviewbuttoncolumn-with-different-text-and-different-functionality-for-dif) – default locale Oct 10 '18 at 08:18
  • 2
    Other similar questions: https://stackoverflow.com/questions/33166226/how-to-add-button-name-and-text-dynamically-to-gridview-button https://stackoverflow.com/questions/48430385/change-the-text-value-of-a-button-in-datagridviewbuttoncolumn – default locale Oct 10 '18 at 08:20
  • I have tried both question's solution. In first question's solution suggested to use CellFormatting and CellContentClick event. I tried both and same is suggested in Q2. But not yet got the result – User0804 Oct 10 '18 at 08:29
  • `CellFormatting` should be enough. (`CellContentClick` is used there to change the button's behavior). It's weird that you didn't get the result. Can you show us an MCVE and explain what exactly doesn't work? – default locale Oct 10 '18 at 08:31
  • 1
    _ But not yet got the result _ That is not a helpful problem description! – TaW Oct 10 '18 at 09:11

2 Answers2

0

Assuming you are in Windows Forms, you need to add a DataGridViewButtonColumn to your DataGridView - Not directly to the DataTable.

This should occur somewhere after you bind the DataTable to the DataGridView.

Something like this should work:

DataGridViewButtonColumn uninstallButtonColumn = new DataGridViewButtonColumn();
uninstallButtonColumn.Name = "uninstall_column";
uninstallButtonColumn.Text = "Uninstall";
int columnIndex = 2;
if (dataGridViewSoftware.Columns["uninstall_column"] == null)
{
    dataGridViewSoftware.Columns.Insert(columnIndex, uninstallButtonColumn);
}

Of course you will have to handle the CellClick event of the grid to do anything with the button.

Add this somewhere in your DataGridView Initialization code

dataGridViewSoftware.CellClick += dataGridViewSoftware_CellClick;

Then create the handler:

private void dataGridViewSoftware_CellClick(object sender, DataGridViewCellEventArgs e)
{
    if (e.ColumnIndex == dataGridViewSoftware.Columns["uninstall_column"].Index)
    {
        //Do something with your button.
    }
}
user229044
  • 232,980
  • 40
  • 330
  • 338
AmirReza-Farahlagha
  • 1,204
  • 14
  • 26
-2
//Change the Button text property on a data-bound event 

    protected void YourDataGridViewId_RowDataBound(object sender, GridViewRowEventArgs e)
    {
       // finding the button control..`enter code here`.

        if (e.Row.RowType == DataControlRowType.DataRow)
        {
           Button YourbtnVariable = (Button)e.Row.FindControl("YourbuttonID");
           YourbtnVariable.Text = "Text"+e.Row.RowIndex+1;
           //e.Row.RowIndex+1 will give u index of row every time incremented with 1
        }

    }
  • 2
    Please provide some explanation of your code so everyone can better understand it. – DAA Oct 10 '18 at 08:38