Solved

DropDownList in DataGrid for Batch Insert

Posted on 2004-09-26
6
449 Views
Last Modified: 2006-11-17
I'm creating a batch insert.  I have two DropDownLists - the first one is a list of departments (which is populated in the codebehind):

<asp:dropdownlist id="ddDepartments" runat="server" CssClass="NormalTextBox" DataValueField="DepartmentID" DataTextField="DepartmentName" Width="150px" AutoPostBack="True"></asp:dropdownlist>

The user chooses a department and then what I want to do is create a datagrid with one row for every employee in that department.  The first cell of each row is a DropDownList (ddEmployees) with a list of all of the employees in the department the user has choose.  The default selection in ddEmployees in the first row is set to the first employee, then the next and so on until the last emplyee is the default in the last row.  I wnat do this so that the user can actually change the employee for that row - in case they need to make 2 entries for one employee and 0 for another.

Here's what I have so far for the Datagrid:

<ASP:DATAGRID id="GrdBatchEmp" runat="server" AutoGenerateColumns="False" EnableViewState="True" ShowFooter="False">
      <Columns>
            <asp:BoundColumn DataField="UserID" HeaderText="UserID" />
            <asp:TemplateColumn HeaderText="Name">
                  <HeaderStyle CssClass="NormalBold" />
                  <ItemTemplate>
                        <asp:DropDownList id="ddEmployees" runat="server" NAME="FullName"
                              DataValuefield="UserID" DataTextField="FullName" DataSource="<%# GetUsersList %>"
                        />
                  </ItemTemplate>
            </asp:TemplateColumn>
      </Columns>
</ASP:DATAGRID>

So, the questions are (code samples would be most appreciated):

  1. What is the best way to populate ddEmployees? Since the list is exactly the same, but is repeated for every row in the datagrid, it would make sense not to go back and query the database over and over for every row (the departments and employees are stored in sql server).  Can I store them in a <?? What should I use??> and then provide that as the datasource?

  2. What is the best way to set the default selection in ddEmployees?  Currently I'm, thinking of using the value in Cell 0 (which should hold the UserID) in the GrdBatchEmp_ItemDataBound.

  3. How do you access the users selection in ddEmployees in GrdBatchEmp?

Again, code samples would be most appreciated.
0
Comment
Question by:jsurfage
  • 3
  • 2
6 Comments
 
LVL 28

Accepted Solution

by:
mmarinov earned 250 total points
ID: 12156228
Hi jsurfage,

when you bind your combo you can save your datasource ( datatable for example ) in a session variable
so after a submit you have it and you don't have to recall the database to get the data

for populating ddlEmployees you have to use ItemDataBound event of the datagrid
sample:

 void Item_Bound(Object sender, DataListItemEventArgs e)
      {

         if (e.Item.ItemType == ListItemType.Item ||
             e.Item.ItemType == ListItemType.AlternatingItem)
         {

           DataTable dt = (DataTable)Session["DataSetForddDepartments"];
           DropDownList ddl = (DropDownList )e.Item.FindControl("ddEmployees");
           ddl.DataSource = dt;
           ddl.DataBind()
         }

      }
so you have to remove
 DataSource="<%# GetUsersList %>"
from your html code

for 2. i think your way is pretty good and you don't have to search for other except if this will not work like you want.
3. for accessing the selection of ddlEmployees you have to use

DropDownList ddl = (DropDownList)DataGrid.Item["the row you wanted to check"].FindControl("ddlEmployees")

Hope this helps
Regards
B..M
0
 
LVL 8

Assisted Solution

by:boulder_bum
boulder_bum earned 250 total points
ID: 12163496
1. Since you're using information that does not change for each user, I'd consider using caching over session. Basically do something like what mmarinov suggests, but change:

DataTable dt = (DataTable)Session["DataSetForddDepartments"];

  -to-

DataTable dt = GetTable( departmentID );

  -and add this function-

private DataTable GetTable( int departmentID)
{
     DataTable dt = Cache["Employees" + departmentID.ToString()];
   
     if( dt == null )
     {
          dt = //get data
          Cache["Employees" + departmentID.ToString()] = dt;
     }

     return dt;
}

You can do a few optimizations with the caching but that gives you the basic idea.

2. Instead of looking to a cell value, it's probably for fitting to look at ((DataRowView)e.Item.DataItem) within the ItemDataBound event.

3. mmarinov pretty much sussed it:

foreach (DataGridItem dgi in myGrid.Items) //you may not wish to loop here, it depends on the implementation
{
       DropDownList ddl = ((DropDownList)dgi.Cells[index1].Controls[index2]);
       int employeeID = ddl.SelectedValue;

       //do stuff
}
0
 
LVL 8

Expert Comment

by:boulder_bum
ID: 12163508
*more fitting - correction to # 2 in the list.
0
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

 

Author Comment

by:jsurfage
ID: 12175802
Thanks to both.

Since ddDepartments and GrdBatchEmp will be populated at the same time.  I suspect all I need to so is store the results in a dataSet or datatable and then use it as suggested.  Do you have an example of how to do that if the data is coming from a datareader?
0
 
LVL 8

Expert Comment

by:boulder_bum
ID: 12176801
If you want to populate a DataSet or DataTable, you'll want to use a DataAdapter and its Fill() method. Here's an example and more info:

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfsystemdatasqlclientsqldataadapterclasstopic.asp

If you ended up using a DataReader, you'd have to re-read the results (which ends up being a bit inefficient).
0
 

Author Comment

by:jsurfage
ID: 12222616
Thanks guys.
0

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

Join & Write a Comment

Today is the age of broadband.  More and more people are going this route determined to experience the web and it’s multitude of services as quickly and painlessly as possible. Coupled with the move to broadband, people are experiencing the web via …
A quick way to get a menu to work on our website, is using the Menu control and assign it to a web.sitemap using SiteMapDataSource. Example of web.sitemap file: (CODE) Sample code to add to the page menu: (CODE) Running the application, we wi…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

707 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

17 Experts available now in Live!

Get 1:1 Help Now