0

I have datagridview with some data, and I add a button (column) to this grid do do sth with it later.

//------------------- definitions
DataTable datatable = new DataTable("Points");
this.DATAGRID.DataSource = datatable; //connect data to DATAGRID set in designer

//adding button column
if (DATAGRID.Columns.Contains("Button_column") == false) //I want to add button column only once
{ 
    DataGridViewButtonColumn button_column = new DataGridViewButtonColumn();
    button_column.HeaderText = "ON/OFF";
    button_column.Text = "Click";
    button_column.Name = "Button_column";
    button_column.UseColumnTextForButtonValue = true;
    DATAGRID.Columns.Add(button_column);
}    

//add next columns
datatable.Columns.Add("id", typeof(int));
datatable.Columns.Add("Date", typeof(string));
datatable.Columns.Add("Point", typeof(string));
datatable.Columns.Add("Status", typeof(string));

//set order for the user
DATAGRID.Columns["id"].DisplayIndex = 0; //will need id later
DATAGRID.Columns["id"].Visible = false; //but I hide it from user
DATAGRID.Columns["Date"].DisplayIndex = 1;
DATAGRID.Columns["Point"].DisplayIndex = 2;
DATAGRID.Columns["Status"].DisplayIndex = 3;
DATAGRID.Columns["Button_column"].DisplayIndex = 4;

//------------------- data
int i = 0;
while (r_dane_kontroli.Read())
{

    //I add data here
    datatable.Rows.Add(1, "Date", "Point"); //adding value to the button here won't work (error "to many columns")

    //I TRY TO CHANGE BUTTON TEXT HERE - IN THE LOOP
    //this doesn't work no matter if I adress the row or cell via index or name (tried other indexes too...)
    if(status == "ON")
        DATAGRID.Rows[i].Cells[0].Value = "OFF";
    if(status == "OFF")
        DATAGRID.Rows[i].Cells[0].Value = "ON";

    i++;
}

There is no error. It simply is not working. I don't want to create additional loops after this one.

It does not matter if I add button column programaticly like above, or via designer. Effect is the same. Any solutions?

4
  • When you run the program, does the button show you the blank text? or what shows you? Commented Mar 20, 2018 at 15:00
  • You are assigning the name directly to the cell DATAGRID.Rows[i].Cells[0].Value = "OFF"; must be assigned to the button in this way: string status = DATAGRID.Rows[e.RowIndex].Cells["Status"].Value.ToString(); if (status.Equals("ON") { button_collumn.Text = "OFF"; } else {button_collumn.Text = "ON"} Commented Mar 20, 2018 at 15:06
  • Program (when run) shows Click from button_collumn.Text = "Click"; that I have in definition Commented Mar 20, 2018 at 15:25
  • If I understand correctly you try first to read value of status (should be i instead of e I think) and then assign button to text this way: button_collumn.Text = "OFF" - unfortunatelly I tried and it doesn't work, or I don't understand sth (error: button_collumn does not exist in current (loop) context) Commented Mar 20, 2018 at 15:31

1 Answer 1

1

Your code is not very clear but I will try to give you a solution according to what I have understood of your question.

If you need to set different text for the buttons, you can use the CellFormatting event of the DataGridView and Set the value of those cells:

private void DATAGRID_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
{ 
    //// If this is a new header row or row, do nothing
    if (e.RowIndex < 0 || e.RowIndex == this.DATAGRID.NewRowIndex)
        return;

    //If your column type button is 0, you must validate this
    if (e.ColumnIndex == 0)
    {
        string status = DATAGRID.Rows[e.RowIndex].Cells["Status"].Value.ToString();

        if (status.Equals("ON"))  
            e.Value = "OFF"; 
        else 
            e.Value = "ON";
    }
}

You must assign this driver to the CellFormatting event:

this.DATAGRID.CellFormatting += DATAGRID_CellFormatting;
Sign up to request clarification or add additional context in comments.

6 Comments

this line: button_collumn.Text = "OFF"; gives error: The name 'button_collumn' does not exist in the current context. Also please tell me what can I do to make my code more clear.
You must replace the name button_collumn by the real name of your button type column
accually this works fine: if (e.ColumnIndex == 0) {e.Value = "new value" } insead off button_collumn (that is a real name I used in project) - could you please update your answear so I could accept it? (index is 0 becouse button collumns is first one added in code)
Program will compile but crash with this exception error: Object reference not set to an instance of an object. - but there is really no need I can acces the text in the way mentioned above and it works fine.
yes it works fine. Full code: if (e.ColumnIndex == 0) { if (status == "ON") e.Value = "OFF"; else e.Value = "ON"; }. Thanx for your help. I need to go home now, so I'll accept your answear now, and if you could update it my way (it works so others can use it) then it would be cool.
|

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.