2

I am reading xml file in winform and displaying data in DataGridView DataGridView read in xml has a button, and when the button is clicked, the button's text is changed.

DataGridViewButtonCell btnStart = null;
btnStart = (DataGridViewButtonCell)(DataGridView.Rows[e.RowIndex].Cells[0]);
var btnType = btnStart.Value.ToString();
btnStart.UseColumnTextForButtonValue = false;

In this result I try to re-read the XML and append the ROW

DataGridView.DataSource = dataSet.Tables[0];

Is there a way to add ADD to the DataGridView without changing the text of the button in the DataGridView that is shown?

5
  • As John V’s answer notes, replacing the “whole” table in the grids data source is going to clear the values in the button column. In addition, where in the posted code does it try and “change” the buttons text value? The first code snippet grabs the grid’s button cell btnStart… then sets the button string value to a variable called btnType then does nothing with that value…? … In other words, … I do not see where the btnStart.Value = SomeValue is set…? … Commented Apr 12, 2022 at 4:34
  • The default value of btnStart in the code is start Commented Apr 12, 2022 at 5:21
  • if (e.ColumnIndex == 0) { if(btnType == "start"){ this.DataGridView.Rows[e.RowIndex].Cells[e.ColumnIndex].Value = "stop"; }} Commented Apr 12, 2022 at 5:23
  • I am changing the btnType vlaue in this way Commented Apr 12, 2022 at 5:23
  • I am not sure why you did not post that code to begin with as it is pertinent. However, as noted already, even if the button value is set… it will be cleared when the grids "WHOLE" data source is changed. Adding a row to the grids data source will work. Commented Apr 12, 2022 at 5:43

1 Answer 1

1

This line of code: DataGridView.DataSource = dataSet.Tables[0];

Doesn't read a row, it resets the entire DataGridView, which is why the button changes. If you want to append a row, then add a row to the Table the DataGridView is displaying, don't replace the whole thing.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.