01.Blogs :
travisowens  

Manually populating a DataGrid

Manually populating a DataGrid

I was coding an WinForms app and had to give a short presentation and needed to fake some functionality of populating a DataGrid of users.  Unfortunetly I didn't have the time to figure out how to query and extract the data from our Active Directory server (aka LDAP server) so I figured for the sake of the presentation it would be better if I just imitated it.  Unfortunetly almost every DataGrid tutorial out there discusses abstract filling via objects or databases and only a handfull actually discuss manually populating a DataGrid manually.

So I took my code and simplied it below in case anybody wanted to see how easy it is to label and populate a DataGrid.

// Create a data table object to put your data in
DataTable dt = new DataTable();

// Create a row object
DataRow dr;

// Add columns, label them and assign a data type
dt.Columns.Add(new DataColumn("UID", typeof(string)));
dt.Columns.Add(new DataColumn("First Name", typeof(string)));
dt.Columns.Add(new DataColumn("Last Name", typeof(string)));
dt.Columns.Add(new DataColumn("Title", typeof(string)));
dt.Columns.Add(new DataColumn("Dept", typeof(string)));
dt.Columns.Add(new DataColumn("Role", typeof(string)));

// Populate an array of strings for the table
string[] users = {"123","John","Doe","Mailman","Office Management","basic user"};

// create a new row
dr = dt.NewRow();

// Loop through the array and push it into the rows
for (int i = 0; i < 7; i++)
{
    dr[i] = users[i];
}

// add the row to the table
dt.Rows.Add(dr);

// create a DataView object and apply the table to it
DataView dv = new DataView(dt);

// take your DataGrid control and push the dataview into it
myDataGrid.DataSource = dv;





































PS: I already have code to query Active Directory so I don't need help there, and I will be posting some example stuff when I'm done with it.

posted on Friday, March 26, 2004 8:48 PM by travisowens


 
03.UPDATE CALENDAR :
<March 2004>
SunMonTueWedThuFriSat
29123456
78910111213
14151617181920
21222324252627
28293031123
45678910

05.MY LINKS :

07.Subscriptions :

Subscriptions


© Copyright 2005 Microsoft Corporation. All Rights Reserved.
Terms of Use | Privacy Statement | Code of Conduct | Hosted by MaximumASP for Microsoft
WHO-BAR