如何更改 DataGrid 列 C# 中的特定元素名称

How do I change specific Element names in a DataGrid column C#

我想替换 Datagrid 中“名称”列中的某些名称。我不需要更改填充网格的 xml 文件,只需更改显示名称。

例如“panel1”到“pressure1”和“checkBox5”到“Selected”

来源xml

<ChildForm>
  <Control Type="System.Windows.Forms.Panel" Name="panel1">
    <Visible>True</Visible>
    <Control Type="System.Windows.Forms.Button" Name="button11">
      <Visible>True</Visible>
    </Control>
    <Control Type="System.Windows.Forms.Button" Name="button10">
      <Visible>True</Visible>
    </Control>
  </Control>
  <Control Type="System.Windows.Forms.GroupBox" Name="groupBox5">
    <Visible>True</Visible>
    <Control Type="System.Windows.Forms.ComboBox" Name="cmbTBU">
      <Text>Unbalaced</Text>
      <Visible>True</Visible>
    </Control>
    <Control Type="System.Windows.Forms.Button" Name="button8">
      <Visible>True</Visible>
    </Control>
    <Control Type="System.Windows.Forms.Button" Name="button9">
      <Visible>False</Visible>
    </Control>
    <Control Type="System.Windows.Forms.TextBox" Name="shutoff">
      <Text>21</Text>
      <Visible>True</Visible>
    </Control>
    <Control Type="System.Windows.Forms.CheckBox" 

编辑:填充网格的代码

                DataSet dataSet = new DataSet();
                dataSet.ReadXml(dialog.FileName);
                dataGridView1.DataSource = dataSet.Tables[0];


                this.dataGridView1.Columns["Type"].Visible = false;
                this.dataGridView1.Sort(this.dataGridView1.Columns["Visible"], ListSortDirection.Descending);

您需要在阅读后枚举 table,执行替换:

var d = new Dictionary<string, string>(){
  { "panel1" ,"pressure1" },
  { "checkBox5", "Selected"}
};

foreach(DataRow r in dataSet.Tables[0].Rows){
  if(d.TryGetValue(r["Name"].ToString()], out string v))
    r["Name"] = v;  
}

这不会更改 XML 文件,但会更改显示的内容。

请注意,如果您再次将数据集保存到 xml 文件,将会更改输出内容。如果这不应该发生,最简单的 thing/way 是使用 DataGridViewComboBox 将“panel1”解码为“pressure1”。如果需要,我可以post举个例子