0
votes

I want to change headers text in datagridview1 from cell in datagridview2, is that possible, I know how to do it from textbox but from cells in datagridview no. Any help, I don't have any idea.

Example: enter image description here

In this case datagridview 1 will take the change from datagridview 2 column 1 row 1 for variable X and column 2 row 2 for variable Y

3

3 Answers

0
votes

According to your description, you want to change headers text in datagridview1 from cell in datagridview2. You can try the following code to solve it:

private void dataGridView2_CellValueChanged(object sender, DataGridViewCellEventArgs e)
        {
            if (e.RowIndex == 0) 
            {
                if (e.ColumnIndex==0) 
                {
                    dataGridView1.Columns[0].HeaderText = (dataGridView2.Rows[0].Cells[0].Value).ToString();
                }
                if (e.ColumnIndex==1) 
                {
                    dataGridView1.Columns[1].HeaderText = (dataGridView2.Rows[0].Cells[1].Value).ToString();
                }
            }
        }
0
votes

Text is text. It doesn't matter where it comes from or goes to. It's still just text. If you know how to set the header text of a grid column then you know how to do it, regardless of where the text comes from. Write a method that accepts the text as an argument and sets the header. You can then call that method with any text, regardless of the source. Now all you have to do is get the text from a grid cell. I would hope that you already know how to do that but, if you don't, it's easy to find out. Put that code into a method that returns the text. Now call the method that returns the text and pass that to the other method.

0
votes

@Jack J Jun Thank you for your answer, that exactly what I want, I change in your code to make sam as the picture on my question.

Dgv.rows.add(nothing,nothing) Dgv.rows.add(nothing,nothing)

    If e.ColumnIndex = 0 Then
        dataGridView1.Columns(0).HeaderText = (dataGridView2.Rows(0).Cells(0).Value).ToString()
    End If

    If e.ColumnIndex = 0 Then
        dataGridView1.Columns(1).HeaderText = (dataGridView2.Rows(1).Cells(0).Value).ToString()
    End If

With this code I get the column of variable work