简体   繁体   English

如何从作者 datagridview 单元格更改 datagridview header 文本

[英]How to change datagridview header text from an author datagridview cell

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.我想从datagridview2中的单元格更改datagridview1中的标题文本,这可能吗,我知道如何从文本框但从datagridview中的单元格没有。 Any help, I don't have any idea.任何帮助,我不知道。

Example:例子: 在此处输入图像描述

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在这种情况下,datagridview 1 将从 datagridview 2 的第 1 列第 1 行用于变量 X 和第 2 列第 2 行用于变量 Y

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.如果您知道如何设置网格列的 header 文本,那么无论文本来自何处,您都知道该怎么做。 Write a method that accepts the text as an argument and sets the header.编写一个接受文本作为参数并设置 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.现在调用返回文本的方法并将其传递给另一个方法。

According to your description, you want to change headers text in datagridview1 from cell in datagridview2.根据您的描述,您想从 datagridview2 中的单元格更改 datagridview1 中的标题文本。 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();
                }
            }
        }

@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. @Jack J Jun 感谢您的回答,这正是我想要的,我更改了您的代码以使 sam 成为我问题上的图片。

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

    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使用此代码,我得到变量工作列

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM