将XML文件保存到DataGridView中

 

 

 1         #region get护理单记录信息XML

 2         //将XML文件保存到DataTable

 3         private DataTable FromXML2DataTable(string XMLStr,string data_h,string data_d)

 4         {

 5             XmlDocument myDoc = new XmlDocument();

 6             myDoc.LoadXml(XMLStr);

 7             if (string.IsNullOrEmpty(XMLStr) || !myDoc.HasChildNodes) return null;

 8             DataTable dt = new DataTable();

 9             DataRow row = null;

10             DataColumn col = null;

11             //填充数据

12             foreach (XmlElement el in myDoc.SelectNodes(data_h + "/" + data_d))

13             {

14                 row = dt.NewRow();

15                 foreach (XmlNode node in el.ChildNodes)

16                 {

17                     if (!dt.Columns.Contains(node.Name))

18                     {

19                         col = new DataColumn();

20                         col.ColumnName = node.Name;

21                         dt.Columns.Add(col);

22                     }

23                     row[node.Name] = node.InnerText;

24                 }

25                 dt.Rows.Add(row);

26             }

27             return dt;

28         }

29         #region Set护理单信息DataTable到GridView

30     //NurseGridList.DataSource=DataTable

31         public void ShowNurseTable(DataTable ds)

32         {

33             if (ds != null && ds.Rows.Count > 0)

34             {

                   //自动设置列
35 NurseGridList.AutoGenerateColumns = false;
//标题内容居中显示位置
36 NurseGridList.ColumnHeadersDefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter;
//行内容显示位置
37 NurseGridList.DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter; 38 NurseGridList.DataSource = ds;
//清除选择
39 NurseGridList.ClearSelection(); 40 } 41 } 42 #endregion

 

你可能感兴趣的:(datagridview)