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.