Populating A GridView Programmatically

Posted on 2010-01-08
Last Modified: 2012-06-27
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.
Question by:npl77
    1 Comment
    LVL 3

    Accepted Solution

    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

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    How your wiki can always stay up-to-date

    Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
    - Increase transparency
    - Onboard new hires faster
    - Access from mobile/offline

    Microsoft Reports are based on a report definition, which is an XML file that describes data and layout for the report, with a different extension. You can create a client-side report definition language (*.rdlc) file with Visual Studio, and build g…
    The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (…
    Sending a Secure fax is easy with eFax Corporate ( First, Just open a new email message.  In the To field, type your recipient's fax number You can even send a secure international fax — just include t…
    This video discusses moving either the default database or any database to a new volume.

    779 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    18 Experts available now in Live!

    Get 1:1 Help Now