DropDownList in DataGrid for Batch Insert
Posted on 2004-09-26
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">
<asp:BoundColumn DataField="UserID" HeaderText="UserID" />
<HeaderStyle CssClass="NormalBold" />
<asp:DropDownList id="ddEmployees" runat="server" NAME="FullName"
DataValuefield="UserID" DataTextField="FullName" DataSource="<%# GetUsersList %>"
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.