Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

DropDownList in DataGrid for Batch Insert

Posted on 2004-09-26
6
456 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
Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

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.

 

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

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

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

Sometimes in DotNetNuke module development you want to swap controls within the same module definition.  In doing this DNN (somewhat annoyingly) swaps the Skin and Container definitions to the default admin selections.  To get around this you need t…
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 …
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

808 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