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的值,將每個(gè)節(jié)點(diǎn)的值存入BindingList中,并將其作為gridview的數(shù)據(jù)源
string addr, kind, num, mark ;
BindingList
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數(shù)據(jù)存入dataset,通過dataset直接作為數(shù)據(jù)源展示
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數(shù)據(jù)存入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.改變列標(biāo)題,及列寬
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;
記錄下來,希望對自己和需要的人有所幫助!
創(chuàng)新互聯(lián)建站主要從事網(wǎng)站設(shè)計(jì)制作、網(wǎng)站設(shè)計(jì)、網(wǎng)頁設(shè)計(jì)、企業(yè)做網(wǎng)站、公司建網(wǎng)站等業(yè)務(wù)。立足成都服務(wù)龍華,十余年網(wǎng)站建設(shè)經(jīng)驗(yàn),價(jià)格優(yōu)惠、服務(wù)專業(yè),歡迎來電咨詢建站服務(wù):028-86922220