?
Solved

Populating A GridView Programmatically

Posted on 2010-01-08
1
Medium Priority
?
281 Views
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.
0
Comment
Question by:npl77
1 Comment
 
LVL 3

Accepted Solution

by:
roeib earned 2000 total points
ID: 26214374
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
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

ASP.Net to Oracle Connectivity Recently I had to develop an ASP.NET application connecting to an Oracle database.As I am doing it first time ,I had to solve several problems. This article will help to such developers  to develop an ASP.NET client…
Introduction This article shows how to use the open source plupload control to upload multiple images. The images are resized on the client side before uploading and the upload is done in chunks. Background I had to provide a way for user…
Are you ready to place your question in front of subject-matter experts for more timely responses? With the release of Priority Question, Premium Members, Team Accounts and Qualified Experts can now identify the emergent level of their issue, signal…
Screencast - Getting to Know the Pipeline
Suggested Courses
Course of the Month16 days, left to enroll

850 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