
我想替換 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);
uj5u.com熱心網友回復:
閱讀完表格后,您需要列舉表格,執行替換:
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 檔案,它將更改輸出內容。如果這不應該發生,最簡單的方法/方法是使用 DataGridViewComboBox 將“panel1”解碼為“pressure1”。如果需要,我可以發布一個示例
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/373589.html
