Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!
  • Students Click Here

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here


Extra Column In DataGridView From Active Directory

Extra Column In DataGridView From Active Directory

Extra Column In DataGridView From Active Directory

Currently, I have a DataGridView (DGV) that brings in three columns (Employee_ID, User_ID, ID_Type) from two different MS Access tables. However, I would like to add a fourth column (User_Name) from Active Directory (AD) by looking up the User's name from AD using the Employee_ID. In the below code, do I use a for each loop? Any assistance would be greatly appreciated.


Here is the code I have so far:
Private Sub MainView_Load(sender As Object, e As EventArgs) Handles MyBase.Load
IDType = New OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0; Data Source = Drive:\Path\Filename.accdb")
Dim ResultsCommand As OleDbCommand = Nothing
Dim ResultsAdapter As New OleDbDataAdapter
Dim ResultsTable As New DataTable
'establish command object and data adapter
ResultsCommand = New OleDbCommand("SELECT UserIDs.EmpID AS Employee_ID, UserIDs.IDType AS User_ID, IDDesc.[Desc] AS ID_Type FROM UserIDs INNER JOIN IDDesc ON UserIDs.IDDesc = IDDesc.ID ORDER BY UserIDs.EmpID", IDType)
ResultsAdapter.SelectCommand = ResultsCommand
'bind grid view to data table
dgvIDType.DataSource = ResultsTable
'lblRecords.Text = ResultsTable.Rows.Count.ToString

Dim strUserID As String
dgvIDType.Columns.Add("username", "User_Name")


dgvIDType.User_Name = GetUserName(domainDN, strUserID)
Catch ex As Exception
MsgBox(Err.Description, MsgBoxStyle.Information, "SYSTEM MESSAGE")
End Try

Catch ex As Exception
MessageBox.Show(ex.Message, "Error in Processing SQL", MessageBoxButtons.OK, MessageBoxIcon.Error)
End Try
End Sub

RE: Extra Column In DataGridView From Active Directory

Yes, but you should add the additional column to the datatable, not the grid:


Dim dcUsername As DataColumn
dcUsername = New DataColumn("User_Name")


For Each drUser As DataRow in ResultsTable.Rows
        drUser.Item("User_Name") = GetUserName(domainDN, drUser.Item("User_ID"))
    Catch ex As Exception
        MsgBox(Err.Description, MsgBoxStyle.Information, "SYSTEM MESSAGE")
    End Try


dgvIDType.DataSource = ResultsTable 

Note: add the column to the datatable before you bind it to the grid.

I used to rock and roll every night and party every day. Then it was every other day. Now I'm lucky if I can find 30 minutes a week in which to get funky. - Homer Simpson

Arrrr, mateys! Ye needs ta be preparin' yerselves fer Talk Like a Pirate Day!

RE: Extra Column In DataGridView From Active Directory

IT WORKED! THANK YOU! This will help us with future datagridviews.


Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members!

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close