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 SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Extra Column In DataGridView From Active Directory

Status
Not open for further replies.

gcrane

Programmer
Aug 4, 2014
12
0
0
US
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.

Thanks!
GCRANE

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")
IDType.Open()
Dim ResultsCommand As OleDbCommand = Nothing
Dim ResultsAdapter As New OleDbDataAdapter
Dim ResultsTable As New DataTable
Try
'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
ResultsAdapter.Fill(ResultsTable)
'bind grid view to data table
dgvIDType.DataSource = ResultsTable
'lblRecords.Text = ResultsTable.Rows.Count.ToString

'ADD USER NAME COLUMN
Dim strUserID As String
dgvIDType.Columns.Add("username", "User_Name")

'FOR EACH LOOP?

Try
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
ResultsCommand.Dispose()
ResultsAdapter.Dispose()
ResultsTable.Dispose()
End Sub
 

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

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

ResultsTable.Columns.Add(dcUsername)

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

ResultsTable.AcceptChanges()

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!
 
IT WORKED! THANK YOU! This will help us with future datagridviews.

gcrane
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top