Winform中实现读取xml配置文件并动态配置DevExpress的RadioGroup的选项

场景

Winform中对DevExpress的RadioGroup进行数据源绑定,即通过代码添加选项:

https://blog.csdn.net/BADAO_LIUMANG_QIZHI/article/details/100540152

Winform中自定义xml配置文件后对节点进行读取与写入:

https://blog.csdn.net/BADAO_LIUMANG_QIZHI/article/details/100532137

结合上面两种效果实现打开一个新的窗体后,此窗体上的RadioGroup的选项是根据配置文件

中的配置自动生成的。

关注公众号
霸道的程序猿
获取编程相关电子书、教程推送与免费下载。
大量编程视频教程:https://space.bilibili.com/164396311

配置文件代码如下:

<?xml version="1.0" encoding="utf-8"?>
<Configure>
  <!--Y轴集合-->
  <YAxis>
    <!--第一条Y轴-->
    <YAxi>
      <no>1</no>
      <title>霸道</title>
      <color>black</color>
      <min>-1500</min>
      <max>1500</max>
    </YAxi>
    <!--第二条Y轴-->
    <YAxi>
      <no>2</no>
      <title>电压</title>
      <color>black</color>
      <min>-1500</min>
      <max>1500</max>
    </YAxi>
    <YAxi>
      <no>3</no>
      <title>badao</title>
      <color>red</color>
      <min>-1600</min>
      <max>1600</max>
    </YAxi>
  </YAxis>
</Configure>

实现

新建一个窗体并拖拽一个RadioGroup控件。

 

双击窗体进入其加载完之后的事件中

 

 private void EditY_Load(object sender, EventArgs e)
        {
            List<YAxisModel> nodeYList = new List<YAxisModel>();
            //获取可执行文件的路径-即bin目录下的debug或者release目录
            string context = System.Windows.Forms.Application.StartupPath;
            string path = String.Concat(context,@"\config\YAxisSet.xml");
            XmlDocument xml = new XmlDocument();
            //打开一个xml
            try
            {
                xml.Load(path);
                //读取节点数据
                XmlNodeList nodeList = xml.SelectNodes("Configure/YAxis/YAxi");
                foreach (XmlNode n in nodeList)
                {
                   YAxisModel ya = new YAxisModel();
                   ya.No = int.Parse(n.SelectSingleNode("no").InnerText);
                   ya.Title = n.SelectSingleNode("title").InnerText;
                   ya.Color = n.SelectSingleNode("color").InnerText;
                   ya.Min = double.Parse(n.SelectSingleNode("min").InnerText);
                   ya.Max = double.Parse(n.SelectSingleNode("max").InnerText);
                   nodeYList.Add(ya);
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
           
            //数据绑定
            foreach (YAxisModel s in nodeYList)
            {
                //每一个单元按钮对应的选线item
                RadioGroupItem item = new RadioGroupItem();
                //设置选项的value值
                item.Value = s.No;
                //设置选项的描述值 即 要显示的值
                item.Description = s.Title;
                //使选项启用
                item.Enabled = true;
                //将新增的选项添加到radiogroup的Items中
                this.radioGroup1.Properties.Items.Add(item);
            }
           //默认选中value为1的项
            radioGroup1.EditValue = 1;
         }

在此之前要新建一个对象用来存取读取的配置文件的YAxi节点的属性。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ZedGraphTest.model
{
    class YAxisModel
    {
        private int no;

        public int No
        {
            get { return no; }
            set { no = value; }
        }
        private string title;

        public string Title
        {
            get { return title; }
            set { title = value; }
        }
        private string color;

        public string Color
        {
            get { return color; }
            set { color = value; }
        }
        private double min;

        public double Min
        {
            get { return min; }
            set { min = value; }
        }
        private double max;

        public double Max
        {
            get { return max; }
            set { max = value; }
        }
    }
}

效果

 

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
WinForm操作DataGridView读写XML文件可以通过以下步骤实现: 1. 首先,创建一个WinForm应用程序,并向窗体添加一个DataGridView控件。 2. 在窗体的Load事件读取XML文件内容,并将其加载到DataGridView。可以使用XmlDocument类来读取XML文件,使用其Load方法加载文件,并使用SelectNodes方法获取需要的节点数据。 3. 将XML文件的数据添加到DataGridView,可以通过遍历获取到的节点数据,将其添加到DataGridView的行。 4. 编写保存按钮的Click事件处理方法,在该方法将DataGridView的数据保存为XML文件。可以使用XmlDocument类来创建XML文档对象,并使用其CreateElement、CreateAttribute等方法创建XML节点和属性,并将DataGridView的数据逐一添加XML文件,最后使用Save方法保存XML文件。 5. 在保存XML文件之后,可以通过重新加载XML文件的方式更新DataGridView的数据显示。 示例代码如下: private void Form1_Load(object sender, EventArgs e) { // 读取XML文件 XmlDocument xmlDoc = new XmlDocument(); xmlDoc.Load("data.xml"); // 获取需要的节点数据 XmlNodeList nodes = xmlDoc.SelectNodes("/root/item"); // 添加节点数据到DataGridView foreach (XmlNode node in nodes) { // 获取节点数据,并添加到DataGridView的行 string name = node.SelectSingleNode("name").InnerText; string age = node.SelectSingleNode("age").InnerText; dataGridView1.Rows.Add(name, age); } } private void btnSave_Click(object sender, EventArgs e) { // 创建XML文档对象 XmlDocument xmlDoc = new XmlDocument(); // 创建根节点 XmlElement root = xmlDoc.CreateElement("root"); xmlDoc.AppendChild(root); // 遍历DataGridView的行 foreach (DataGridViewRow row in dataGridView1.Rows) { // 创建item节点 XmlElement item = xmlDoc.CreateElement("item"); // 创建name属性 XmlAttribute nameAttr = xmlDoc.CreateAttribute("name"); nameAttr.Value = row.Cells[0].Value.ToString(); item.Attributes.Append(nameAttr); // 创建age属性 XmlAttribute ageAttr = xmlDoc.CreateAttribute("age"); ageAttr.Value = row.Cells[1].Value.ToString(); item.Attributes.Append(ageAttr); // 添加item节点到根节点 root.AppendChild(item); } // 保存XML文件 xmlDoc.Save("data.xml"); // 重新加载XML文件,更新DataGridView数据 Form1_Load(null, null); } 这样,通过以上步骤,就可以在WinForm应用程序通过DataGridView操作读写XML文件了。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

霸道流氓气质

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

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

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

打赏作者

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

抵扣说明:

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

余额充值