温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

datagridview绑定数据

发布时间:2020-06-12 13:23:36 来源:网络 阅读:503 作者:追逐779 栏目:编程语言

1.直接读取xml并将值赋给datagridview
            xmlDoc.Load("deviceInfo.xml");
            XmlNodeList nodelist = xmlDoc.SelectSingleNode("DEVICE").ChildNodes;
            foreach (XmlNode xn in nodelist)
            {
                XmlElement xe = (XmlElement)xn;
                int index = dataGridView1.Rows.Add();
                DataGridViewRow newrow = dataGridView1.Rows[index];
                newrow.Cells[0].Value = xe.ChildNodes.Item(0).InnerText;
                newrow.Cells[1].Value = xe.ChildNodes.Item(1).InnerText;
                newrow.Cells[2].Value = xe.ChildNodes.Item(2).InnerText;
                newrow.Cells[3].Value = xe.ChildNodes.Item(3).InnerText;
                newrow.Cells[4].Value = xe.ChildNodes.Item(4).InnerText;
                newrow.Cells[5].Value = xe.ChildNodes.Item(5).InnerText;
                newrow.Cells[6].Value = xe.ChildNodes.Item(6).InnerText;
                newrow.Cells[7].Value = xe.ChildNodes.Item(7).InnerText;
                newrow.Cells[8].Value = xe.ChildNodes.Item(8).InnerText;
                newrow.Cells[9].Value = xe.ChildNodes.Item(9).InnerText;
                newrow.Cells[10].Value = xe.ChildNodes.Item(10).InnerText;
                newrow.Cells[11].Value = xe.ChildNodes.Item(11).InnerText;
                newrow.Cells[12].Value = xe.ChildNodes.Item(12).InnerText;
                newrow.Cells[13].Value = xe.ChildNodes.Item(13).InnerText;
                newrow.Cells[14].Value = xe.ChildNodes.Item(14).InnerText;
                newrow.Cells[15].Value = xe.ChildNodes.Item(15).InnerText;
                newrow.Cells[16].Value = xe.ChildNodes.Item(16).InnerText;
                newrow.Cells[17].Value = xe.ChildNodes.Item(17).InnerText;
                newrow.Cells[18].Value = xe.ChildNodes.Item(18).InnerText;
                newrow.Cells[19].Value = xe.ChildNodes.Item(19).InnerText;
        }
2.给类中定义属性变量,读取xml的值,将每个节点的值存入BindingList中,并将其作为gridview的数据源
            string addr, kind, num, mark ;         
            BindingList<Form1> bdlst = new BindingList<Form1>();
            XmlDocument xmlDoc = new XmlDocument();
            xmlDoc.Load("deviceInfo.xml");
            XmlNodeList nodelist = xmlDoc.SelectSingleNode("DEVICE").ChildNodes; 
            foreach (XmlNode xn in nodelist)
            {                
                Form1 frm = new Form1(); 
                frm.addr = xe.ChildNodes.Item(0).ToString();
                frm.kind = xe.ChildNodes.Item(1).ToString();
                frm.num = xe.ChildNodes.Item(2).ToString();
                frm.mark = xe.ChildNodes.Item(3).ToString();
                bdlst.Add(frm);*/
            }
            dataGridView1.DataSource = bdlst; 
3.将xml数据存入dataset,通过dataset直接作为数据源展示
        DataSet ds = new DataSet();
        BindingSource bd = new BindingSource(); 
            ds.ReadXml("deviceInfo.xml");
            dataGridView1.Columns.Clear();
            bd.DataSource = ds.Tables[0].DefaultView;
            dataGridView1.DataSource = bd;
4. 将xml数据存入dataset,通过将dataset中的值一一取出赋给datagridview中的单元格
            ds.ReadXml("deviceInfo.xml");
            dataGridView1.Rows.Clear();
            dataGridView1.Rows.Add(ds.Tables[0].Rows.Count);
            for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
            {
                for (int j = 0; j < ds.Tables[0].Rows.Count; j++)
                {
                    dataGridView1.Rows[i].Cells[j].Value = ds.Tables[0].Rows[i].ItemArray[j];
                }
            }  
5.改变列标题,及列宽
            dataGridView1.Columns[0].HeaderText = "1";
            dataGridView1.Columns[0].Width = 20;
            dataGridView1.Columns[1].HeaderText = "2";
            dataGridView1.Columns[1].Width = 20;
            dataGridView1.Columns[2].HeaderText = "3";
            dataGridView1.Columns[2].Width = 20;

记录下来,希望对自己和需要的人有所帮助! 

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI