XML文件显示、修改、查找

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;
??}
?}

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

haifengat

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值