All pastes #259134 Raw Edit

PopulateTreeListView

public text v1 · immutable
#259134 ·published 2006-11-27 03:33 UTC
rendered paste body
/// <summary>
/// Populate Tree List Views.
/// The Procedure uses 
/// </summary>
/// <param name="gbcc">Number of Group Columns, the categories</param>
/// <param name="sql">Source SQL for the control, First X(gbcc) are for grouping</param>
/// <param name="tlvDestination">TreeListView to populate</param>
		public static void populateTreeListView (int gbcc, string sql, TreeListView tlvDestination)
		{
			bool ComparePaths =false;
			string ActiveRecordPath ="";
			string[] CurrentNodePath;
			//Clear the Destination List
			tlvDestination.Clear();
			
			// Open a connection to the Database and get the data
			OdbcConnection  DB=new OdbcConnection(Core.ODBCConnectionString);			
			TreeListViewItem WorkingNode=null;
			TreeListViewItem CurrentItemNode =null;
			OdbcCommand getData =new OdbcCommand (sql,DB);
			OdbcDataReader treeData=null;

			try
			{
				DB.Open();
				treeData= getData.ExecuteReader();				
				while (treeData.Read())
				{
					//Determin if the Current Item Node is the correct for the Current Data record
					//New Node
					if (CurrentItemNode ==null)
					{
						//Build Coloumns?
						
						for( int i=gbcc;i<(treeData.FieldCount-1);i++)
						{
							int ColWidth = 0;
							if (treeData.GetName(i).EndsWith("id"))
							{
								ColWidth=0;
							}	
							else
							{
								ColWidth=150;
							}
							tlvDestination.Columns.Add(treeData.GetName(i).ToString(),ColWidth,HorizontalAlignment.Left );
							
						}
						//Build TRee List
						
						for( int i=0;i<(gbcc);i++)
						{
							WorkingNode = new TreeListViewItem(treeData[i].ToString());
							if (CurrentItemNode==null ) 
							{
								CurrentItemNode =WorkingNode;
								tlvDestination.Items.Add(WorkingNode);
							}
							else
							{
								CurrentItemNode.Items.Add(WorkingNode);
								CurrentItemNode =WorkingNode;
							}
						}						
					}
					else
					{
						//Build Current Record Path
						ActiveRecordPath = "";
						for( int i=0;i<(gbcc);i++)
						{
							ActiveRecordPath += treeData[i].ToString() + tlvDestination.PathSeparator.ToString();
						}
						ActiveRecordPath=ActiveRecordPath.TrimEnd(tlvDestination.PathSeparator.ToCharArray());
						if (CurrentItemNode.FullPath!=ActiveRecordPath)
						{
							//split Current Item Path
							CurrentNodePath = CurrentItemNode.FullPath.Split(tlvDestination.PathSeparator.ToCharArray());

							//Locate difference.
							ComparePaths=true;
							for( int i=0;i<(gbcc);i++)
							{
								if (ComparePaths==false)
								{
									/*
									WorkingNode = new TreeListViewItem(treeData[i].ToString());
									CurrentItemNode.Items.Add(WorkingNode);
									CurrentItemNode =WorkingNode;
									*/
									WorkingNode = new TreeListViewItem(treeData[i].ToString());

									if (i==0)
									{
										tlvDestination.Items.Add(WorkingNode);	

									}
									else
									{
										CurrentItemNode.Items.Add(WorkingNode);												
									}
									CurrentItemNode =WorkingNode;

								}
								else
								{
									if (CurrentNodePath[i]!=treeData[i].ToString())
									{
										//Build new Current Item
										for(int j=gbcc;j>i;j--)
										{
											CurrentItemNode = CurrentItemNode.Parent;
										}			
										ComparePaths=false;
										if((i+1)>=(gbcc))
										{
											WorkingNode = new TreeListViewItem(treeData[i].ToString());

											if (i==0)
											{
												tlvDestination.Items.Add(WorkingNode);	

											}
											else
											{
												CurrentItemNode.Items.Add(WorkingNode);												
											}
											CurrentItemNode =WorkingNode;
										}
										else if (i==0)
										{
											WorkingNode = new TreeListViewItem(treeData[i].ToString());
											tlvDestination.Items.Add(WorkingNode);												
											CurrentItemNode =WorkingNode;
										}
										
									}
								}
								
							}

						}
						//If 
					}
					//Populate Data
					WorkingNode=new TreeListViewItem(treeData[gbcc].ToString());
					for (int i =gbcc+1;i<treeData.FieldCount-1;i++)
					{
						WorkingNode.SubItems.Add(treeData[i].ToString());

					}
					CurrentItemNode.Items.Add(WorkingNode);

    
				}
			}
			catch (System.Exception  ex)
			{
				MessageBox.Show(ex.ToString());
			}
			finally
			{

			}


		}