Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

DropDownList in DataGrid for Batch Insert

Posted on 2004-09-26
6
Medium Priority
?
465 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 1000 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 1000 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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 

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

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

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

Lots of people ask this question on how to extend the “MembershipProvider” to make use of custom authentication like using existing database or make use of some other way of authentication. Many blogs show you how to extend the membership provider c…
Problem Hi all,    While many today have fast Internet connection, there are many still who do not, or are connecting through devices with a slower connect, so light web pages and fast load times are still popular.    If your ASP.NET page …
This lesson discusses how to use a Mainform + Subforms in Microsoft Access to find and enter data for payments on orders. The sample data comes from a custom shop that builds and sells movable storage structures that are delivered to your property. …
The Relationships Diagram is a good way to get an overall view of what a database is keeping track of. It is also where relationships are defined. A relationship specifies how two tables connect to each other. As you build tables in Microsoft Ac…

571 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