Populating A GridView Programmatically

I have a GridView that I manuall added Bound Columns to the columns are:
Manager Name with Datafield "EMPLNM"
Manager ID with DataField "EMPLID"
Employee Name with DataField "EMPLNM"
Employee ID with DataField "EMPLID"

I have a datatable that is connected to a stored proc call GetEmployees(name) the way I think this should work is
 I get a datatable of all the managers first
Next I iterate through that datatable and call GetEmployees for each manager in the first datatable
Then I want a result of the following...
Manager Name     Manager ID    Employee Name  Employee ID
Nick                       1234               Jack                     12
Nick                       1234               Jerry                    34
Nick                       1234               John                     45
Jeff                       3456               Frank                    56
Jeff                       3456               Larry                    78
Amy                      5678               Curly                    00
Amy                      5678               Moe                      18
Jane                      6789              Sam                       96

Can someone give me some code to accomplish this? I am unsure what to do here. Also is there a better way to go about this (i.e. come up with another stored proc to get all the data at once somehow). I have a dataAccess class so I do not want to use sqldatasource controls. GetEmployee function returns a datatable can someone help me write out this code? Thatnks in advance.
Who is Participating?
roeibConnect With a Mentor Commented:
first i would say i recommend you to use a Repeater Control insted of GridView, its much more convinient in DataBinding and on the Styling of the Design.

about your Issue, i would say you should use the ItemDataBound, then you can control each Item Binding

like this:

private void OnItemDataBound(object sender,
             System.Web.UI.WebControls.DataGridItemEventArgs e)
    if (e.Item.ItemType == ListItemType.Item ||
        e.Item.ItemType == ListItemType.AlternatingItem)
        DataRowView rv = (DataRowView)e.Item.DataItem;
        // Get fourth column value.
        Int32 nUnitsInStock = Convert.ToInt32(rv.Row.ItemArray[4]);
        if (nUnitsInStock < 20)
           e.Item.Cells[4].BackColor = Color.Red;
or use the RowDataBound Event like this:

if (e.Row.RowType == DataControlRowType.DataRow)
    img = e.Row.FindControl("...") as Image;
    img.ImageUrl = "../images/photo" + e.Row.Cells[3].Text;

now mapping this to your question, you need to add 4 Labels and on the RowDataBound use it like this:

if (e.Row.RowType == DataControlRowType.DataRow)
    lblManagerName = e.Row.FindControl("ManagerName") as Label;
    lblManagerID = e.Row.FindControl("ManagerID") as Label;
    lblEmployeeName = e.Row.FindControl(EmployeeName") as Label;
    lblEmployeeID = e.Row.FindControl(EmployeeID") as Label;
   lblManagerName .Text = e.Row.Cells[0].Text;
   lblManagerID .Text = e.Row.Cells[1].Text;
   lblEmployeeName .Text = e.Row.Cells[2].Text;
   lblEmployeeID .Text = e.Row.Cells[3].Text;

hope this helps
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.