CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 1 of 1
  1. #1
    Join Date
    Sep 2008
    Posts
    2

    Trouble converting Vb to C# routine for populating the Treeview Control

    Hey all I'm working on learning more C# and I'm running into some complications converting a program I wrote in VB to C#.

    This is my VB Treeview Populating routine I wrote:

    Code:
      Public Sub configureTreeview()
    
    
            'add letters sort nodes
            For i As Integer = Asc("A") To Asc("Z")
    
                tvRecords.Nodes.Add(Chr(i), Chr(i)) 'set key and text
    
            Next
    
            'add customers
            Dim childnode As TreeNode
            Dim sContactName As String
    
            Dim rowContact As System.Data.DataRow
            For Each rowContact In dtContact.Rows
    
                sContactName = String.Concat(rowContact.Item("LastName1"), ", ", rowContact.Item("FirstName1"))
                If Not IsDBNull(rowContact.Item("MiddleInitial1")) Then
                    sContactName = String.Concat(sContactName, " ", rowContact.Item("MiddleInitial1"), ".")
                End If
                System.Windows.Forms.Application.DoEvents()
                childnode = New TreeNode(sContactName, 0, 1)
                childnode.Name = sContactName 'set text as key
                childnode.Tag = rowContact.Item("ContactID")
    
                Dim sortnode As String = sContactName.Substring(0, 1).ToUpper 'gets alphabet sort node by first letter
                tvRecords.Nodes(sortnode).Nodes.Add(childnode)
    
            Next
    
            'Update TreeView
            tvRecords.Visible = True
            tvRecords.Enabled = True
            tvRecords.ExpandAll()
    
            If dtContact.Rows.Count = 1 Then
                labelRecordCount.Text = dtContact.Rows.Count & " Record"
            Else
                labelRecordCount.Text = dtContact.Rows.Count & " Records"
            End If
    
        End Sub
    Here is what I have for C# minus a few lines I havn't converted yet:

    Code:
           private void PopulateTreeview()
            {
                //add letters sort nodes
                //convert letter to AscII value
                char A = (char)65;
                char Z = (char)90;
                int na = A;
                int nz = Z;
                for (int i = na; i <= nz; i++)
    			{
                    tvRecords.Nodes.Add(Convert.ToChar(i).ToString(), Convert.ToChar(i).ToString()); //set key and text
    			}
                //add customers
                //TreeNode childnode;
                String sContactName;
                DataRowCollection dra = dsContact.Tables["Contact"].Rows;
                foreach (DataRow dr in dra)
                {
                  string sContactName = String.Concat(dr.Item("LastName1"), ", ", dr.Item("FirstName1"));
                    If dr("MiddleInitial1") == System.Convert.IsDBNull;
                    {
                    string sContactName = String.Concat(sContactName, " ", dr.Item("MiddleInitial1"), ".");
                   }
                  System.Windows.Forms.Application.DoEvents();
                  TreeNode childnode = TreeNode(sContactName, 0, 1);
                   childnode.Name = sContactName; //set text as key
                   childnode.Tag = dr.Item("ContactID");
    
                string sortnode = sContactName.Substring(0, 1).ToUpper; //gets alphabet sort node by first letter
                 tvRecords.Nodes(sortnode).Nodes.Add(childnode);
                    
               }
               
            }
    my trouble is in the initial For Each routine, I thought the way I wrote it in C# was pretty equivalent but I guess not can anyone give me some insite as to what I'm doing wrong?

    As you can see dr apparently doesn't have the .item field to work with and I don't know how I should write it to specify those fields from my database.
    Last edited by Troy Davis; September 8th, 2008 at 12:07 AM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured