XML文件显示、修改、查找

类别:.NET开发 点击:0 评论:0 推荐:

public class Form1 : System.Windows.Forms.Form
?{
??private System.Windows.Forms.DataGrid dataGrid1;
??private System.Windows.Forms.OpenFileDialog openFileDialog1;
??private System.Windows.Forms.TabControl tabControl1;
??private System.Windows.Forms.TabPage tabPageNormal;
??private System.Windows.Forms.TabPage tabPageSearch;
??private System.Windows.Forms.Panel panel1;
??private System.Windows.Forms.Button btnSearch;
??private System.Windows.Forms.TextBox tbValue;
??private System.Windows.Forms.ComboBox cmbField;
??private System.Windows.Forms.DataGrid dataGrid2;
??private System.Windows.Forms.Button btnClose;
??private System.Windows.Forms.Button btnReadXml;
??private System.Windows.Forms.Button btnUpdate;
??///


??/// 必需的设计器变量。
??///

??private System.ComponentModel.Container components = null;

??public Form1()
??{
???//
???// Windows 窗体设计器支持所必需的
???//
???InitializeComponent();

???//
???// TODO: 在 InitializeComponent 调用后添加任何构造函数代码
???//
??}

??///


??/// 清理所有正在使用的资源。
??///

??protected override void Dispose( bool disposing )
??{
???if( disposing )
???{
????if (components != null)
????{
?????components.Dispose();
????}
???}
???base.Dispose( disposing );
??}

???DataSet
???ds = new DataSet();

??///


??/// 应用程序的主入口点。
??///

??[STAThread]
??static void Main()
??{
???Application.Run(new Form1());
??}

??///


??/// 更新XML文件
??///

??///
??///
??private void btnUpdate_Click(object sender, System.EventArgs e)
??{
???ds.WriteXml(this.openFileDialog1.FileName);
???MessageBox.Show("更新完成!");
???this.dataGrid1.Focus();
??}

??///


??/// 读取XML文件
??///

??///
??///
??private void btnReadXml_Click(object sender, System.EventArgs e)
??{
???this.openFileDialog1.Filter = "数据文件|*.xml";
???if (this.openFileDialog1.ShowDialog() == DialogResult.OK)
???{
????// 将读入的XML文件作为数据源绑定到DataGrid上
????ds.Reset();
????ds.ReadXml(this.openFileDialog1.FileName);
????this.dataGrid1.DataSource = ds;
????this.dataGrid1.Focus();
???}
??}

??///


??/// 选择某个子表时修改‘列表’
??///

??///
??///
??private void dataGrid1_DataSourceChanged(object sender, System.EventArgs e)
??{???
???// 如果子表名不存在
???if (this.dataGrid1.DataMember == "")
???{
????this.btnSearch.Enabled = false;
????this.tbValue.Clear();
????this.tbValue.Enabled = false;
????this.cmbField.Items.Clear();
???}?// 子表存在,将所有字段名赋值给ComboBox
???else
???{
????this.cmbField.Items.Clear();
????this.cmbField.Items.Add("请选择字段");
????foreach (DataColumn dc in ds.Tables[this.dataGrid1.DataMember].Columns)
????{
?????this.cmbField.Items.Add(dc.ColumnName);
????}
????this.cmbField.SelectedIndex = 0;
???}
??}

??///


??/// 用户选择了一个列名
??///

??///
??///
??private void cmbField_SelectedIndexChanged(object sender, System.EventArgs e)
??{
???if (this.cmbField.SelectedIndex != 0)
????this.tbValue.Enabled = true;
???else
????this.tbValue.Enabled = false;
??}

??///


??/// 用户输入了要搜索的内容
??///

??///
??///
??private void tbValue_TextChanged(object sender, System.EventArgs e)
??{
???if (this.tbValue.Text.Trim().Length > 0)
????this.btnSearch.Enabled = true;
???else
????this.btnSearch.Enabled = false;
??}

??///


??/// 用户点击了“搜索”按钮
??///

??///
??///
??private void btnSearch_Click(object sender, System.EventArgs e)
??{
???// 将搜索结果存入一个DataView
???DataView
????dvSearch = new DataView();
???dvSearch.Table = ds.Tables[this.dataGrid1.DataMember];
???dvSearch.RowFilter = this.cmbField.Text + "='" + this.tbValue.Text.Trim() + "'";
???// 显示 DataView 的内容
???this.dataGrid2.DataSource = dvSearch;
???this.tabControl1.SelectedTab = this.tabPageSearch;
???this.tbValue.Clear();
??}

??///


??/// 关闭--查询结果页
??///

??///
??///
??private void btnClose_Click(object sender, System.EventArgs e)
??{
???this.tabControl1.SelectedTab = this.tabPageNormal;
??}
?}

本文地址:http://com.8s8s.com/it/it44341.htm