Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Programmatically adding TabPage Contents into DataGrid from XML File 1

Status
Not open for further replies.

annethorne

Programmer
Apr 13, 2005
28
US
Hi,

We are creating a C# Windows application using VS 2005.

It reads in an xml file and creates tab pages that each
have a datagrid that holds the specific information
for that tab's node.

We have been able to programmatically give the tab.text
(the label on the tabs), but when we try to add the
specific data for each tab into a datagrid on the
tab's page, all of the data gets added instead of the
data for just that node.

Perhaps we need to clear the grid first???

Any help would be appreciated.

Thank you in advance,
Anne




Here is a portion of the xml file we are using.
Code:
<?xml version="1.0" encoding="utf-8"?>
<inspection>
    <inspforms>
        <inspform>
            <name>general</name>
            <sections>
                <section>
                    <tab>Heading</tab>
                    <label>
                        <name>id</name>
                        <type>text</type>
                        <value>
                            <single>200621</single>
                         </value>
                    </label>
                    <label>
                        <name>request
                        </name>
                        <type>text
                        </type>
                        <value>
                            <single>30
                            </single>
                            <choices>
                                <choice>
                                </choice>
                            </choices>
                        </value>
                    </label>
 
                </section>
            </sections>
        </inspform>
        <inspform>
            <name>Risk</name>
            <sections>
                <section>
                    <tab>Operations</tab>
                    <label>
                        <name>Business Type</name>
                        <type>radio</type>
                        <value>
                            <choices>
                                <choice>
                                    <choicename>Corporation</choicename>
                                    <choicevalue>
                                    </choicevalue>
                                </choice>
                                <choice>
                                    <choicename>LLC</choicename>
                                    <choicevalue>
                                    </choicevalue>
                                </choice>
                                <choice>
                                    <choicename>Partnership</choicename>
                                    <choicevalue>
                                    </choicevalue>
                                </choice>
                                <choice>
                                    <choicename>Sole Proprietor</choicename>
                                    <choicevalue>
                                    </choicevalue>
                                </choice>
                            </choices>
                        </value>
                    </label>
                    <label>
                        <name>Years in Business</name>
                        <type>text</type>
                        <value>
                            <single>
                            </single>
                        </value>
                    </label>
                 </section>
            </sections>
        </inspform>
    </inspforms>
</inspection>


Here is the pertinent code:
//frmMain.cs
Code:
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Windows.Forms;

using ABC.Components;
using ABC.Globals;

namespace ABC.Forms
{
	public class frmMain : System.Windows.Forms.Form
	{
		public  const string Path = @"C:\ABCXmls\abcBase.xml";
		public  ArrayList ArrayListTabs;

		private ABC.Components.abcTabArea abcTabArea1;

		private System.ComponentModel.Container components = null;

		public frmMain()
		{

			XmlHelper.OpenFile(Path);
			ArrayListTabs = new ArrayList();
			ArrayListTabs = XmlHelper.GetArrayList("tab");
			InitializeComponent();
		}
		#region InitializeComponent()

		private void InitializeComponent()
		{
			...

		}
		#endregion

		[STAThread]
		static void Main() 
		{
			Application.Run(new frmMain());
		}

		private void frmMain_Load(object sender, EventArgs e)
		{
			this.abcTabArea1.CreateTabs(this.ArrayListTabs, Path);
		}

		protected override void Dispose( bool disposing )
		{
			...
		}


	}
	}
}
//xmlHelper.cs
Code:
using System;
using System.Collections;
using System.Data;
using System.IO;
using System.Xml;


namespace ABC.Globals
{
	public class XmlHelper
	{
		private static XmlDocument XmlDocument1;

		public static void OpenFile(string path)
		{
			XmlDocument1 = new XmlDocument();
			XmlDocument1.PreserveWhitespace = false;
			XmlDocument1.Load(path);
		}
		public static ArrayList GetArrayList(string elementName)
		{
			ArrayList arrayList1 = new ArrayList();

			XmlNodeList xmlNodeList1 = XmlDocument1.GetElementsByTagName(elementName);

			for (int idx = 0; idx < xmlNodeList1.Count; idx++) 
			{
				XmlElement xmlElement1 = (XmlElement) xmlNodeList1[idx];
				arrayList1.Add(xmlElement1.InnerText);
			}
	
			return arrayList1;
		}
         }
}
//abcTabArea.cs
Code:
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Windows.Forms;

namespace ABC.Components
{
	public class abcTabArea : System.Windows.Forms.UserControl
	{
		internal TabControl TabControl1;
		internal string Path = "";


		private System.ComponentModel.Container components = null;

		public jmpBottom()
		{
			abcTabArea();
		}

		#region InitializeComponent()
		private void InitializeComponent()
		{
			...
		}
		#endregion

		public void CreateTabs(ArrayList arrayListTabNames1, string path)
		{
			this.Path = path;
			for (int idx = 0; idx < arrayListTabNames1.Count; idx++)
			{
				string tab = "";
				tab = arrayListTabNames1[idx].ToString();

				System.Windows.Forms.TabPage abcTabPage = new TabPage(tab);

				abcPage abcPage = new abcPage(tab,this.Path,idx);

				this.abcTabControl1.Controls.Add(abcTabPage);
				abcTabPage.Controls.Add(abcPage);

			}
		}


		protected override void Dispose( bool disposing )
		{
			...
		}

	}
}
\\abcPage.cs
Code:
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Windows.Forms;
using System.Xml;


namespace ABC.Components
{

	public class abcPage : System.Windows.Forms.UserControl
	{
		internal string NodeName = "";
		internal string Path = "";
		internal int indexNum = 0;
		private System.Windows.Forms.Panel panel1;
		private System.Windows.Forms.DataGrid dataGrid1;
		private System.ComponentModel.Container components = null;

		public jmpPage(string nodeName, string path, int indexNum)
		{
			this.NodeName = nodeName;
			this.Path = path;
			this.indexNum = indexNum;
			InitializeComponent();
            		Populate();
		}

		#region InitializeComponent()

		private void InitializeComponent()
		{
			this.panel1 = new System.Windows.Forms.Panel();
			this.dataGrid1 = new System.Windows.Forms.DataGrid();
			this.panel1.SuspendLayout();
			((System.ComponentModel.ISupportInitialize)(this.dataGrid1)).BeginInit();
			this.SuspendLayout();
			// 
			// panel1
			// 
			this.panel1.Controls.Add(this.dataGrid1);
			this.panel1.Location = new System.Drawing.Point(0, 0);
			this.panel1.Name = "panel1";
			this.panel1.Size = new System.Drawing.Size(800, 528);
			this.panel1.TabIndex = 0;
			// 
			// dataGrid1
			// 
			this.dataGrid1.DataMember = "";
			this.dataGrid1.HeaderForeColor = System.Drawing.SystemColors.ControlText;
			this.dataGrid1.Location = new System.Drawing.Point(0, 0);
			this.dataGrid1.Name = "dataGrid1";
			this.dataGrid1.Size = new System.Drawing.Size(800, 528);
			this.dataGrid1.TabIndex = 0;
			// 
			// jmpGeneral
			// 
			this.Controls.Add(this.panel1);
			this.Size = new System.Drawing.Size(800, 528);
			this.panel1.ResumeLayout(false);
			((System.ComponentModel.ISupportInitialize)(this.dataGrid1)).EndInit();
			this.ResumeLayout(false);

		}
		#endregion

		internal void Populate()
		{
			DataSet dataSet1 = new DataSet();
			XmlDocument XmlDocument1;
			XmlDocument1 = new XmlDocument();
			XmlDocument1.PreserveWhitespace = false;
			XmlDocument1.Load(this.Path);

			XmlNodeList xmlNodeList = XmlDocument1.GetElementsByTagName("section");

			XmlElement xmlElement1 = (XmlElement) xmlNodeList[this.indexNum];
			dataSet1.ReadXml(this.Path);
			dataGrid1.DataSource = dataSet1;
			dataGrid1.DataMember = "section";
			dataGrid1.CaptionText = dataGrid1.DataMember;
		}

		protected override void Dispose( bool disposing )
		{
			...
		}
	}
}


 
Hi Anne,

I've had a quick look at your code, I can see that you are creating the tabpage and then creating the abcPage for the tab page. However within the InitializeComponent of abcPage, there is use of pannel.

this.panel1 = new System.Windows.Forms.Panel();

I would have thought that you would be connecting the datagrid to the tabpage not the pannel of the form. This may mean that instead of getting a data grid for each tab page, You get a single datagrid for your form.

I haven't tried this myself, but if it was my program this is what I would be having a look at.

I hope this helps

Good Luck
MJ
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top