Wrox Home  
Search P2P Archive for: Go

  Return to Index  

aspx thread: ASP:Dropdownlist - In a Grid, Databound, how do I set the index???


Message #1 by "Carl Schneider" <cschneider@i...> on Fri, 28 Sep 2001 00:09:58
Whoo who!!!

Finally (3 Days later) I have my Grid and my Template with my Dropdownbox 

in it. Today I got the box to bind to a function that returns the foreign 

table AND sets the DataValueField. I'm so proud :)



Does anybody know a workable approach for setting the selected index so 

the dropdown has the correct value in it?



Here's my code snipits:





<!--DATA GRID CODE -->



<asp:DataGrid id="MyDataGrid" runat="server" 

        AutoGenerateColumns="false"

        OnEditCommand="MyDataGrid_Edit"

        OnCancelCommand="MyDataGrid_Cancel"

        OnUpdateCommand="MyDataGrid_Update"

        DataKeyField="LotID">



<Columns>

<asp:EditCommandColumn EditText="Edit" 

        CancelText="Cancel" UpdateText="Update" />

	

        <asp:BoundColumn HeaderText="LotID" 

                 DataField="LotID" ReadOnly="True" />

        <asp:BoundColumn HeaderText="ProductID" 

                 DataField="ProductID" />

	<asp:BoundColumn HeaderText="Warehouse Code"  

                 DataField="WarehouseCode" />



        <asp:TemplateColumn HeaderText="Warehouse" SortExpression="state">



           <ItemTemplate>

		<asp:Label runat="server" 

                    Text='<%# Container.DataItem("WarehouseID")%>'/>

	    </ItemTemplate>

									

	    <EditItemTemplate>

	        <asp:DropDownList runat="server" id="edit_WarehouseID" 

                    DataSource='<%# GetWarehouses() %>'

                    DataTextField = 'Name'	

                    DatavalueField = 'WarehouseID'>

                </asp:DropDownList>

            </EditItemTemplate>



	</asp:TemplateColumn>

 

        <asp:BoundColumn HeaderText="Quantity" DataField="Quantity" />

	

	                                                            	

	<asp:BoundColumn HeaderText="Quantity Ordered" 

             DataField="QuantityOrdered" />



</Columns>

</asp:Datagrid>



<!--GET WAREHOUSES FUNCTION -->



    '******************************************

    'Retrieves the Forign Table and returns a 

    'DataTable.

    'NOTE: Get Dataset is just my way of avoiding

    'ASP.NET for the moment. It simple creates the

    'connection, sqlDataAdapter and retrieves the

    'date. Lazyness :)

    '******************************************



    Function GetWarehouses()

	Dim MyDataSet as New Dataset

	MyDataset = GetDataSet("Select [Name], WarehouseID From 

Warehouse","Warehouse")

	return MyDataset.Tables("Warehouse")



    End Function



Message #2 by "Eirik Hommeren" <hommeren@h...> on Fri, 28 Sep 2001 10:28:57
I am using another approach using code-behind C#. On Page_Load I binds my 

grid and my dropdownlists whithin it. 





public void BindGrid()

{

	dsReg = (new HoursRegData().getHoursRegistrationForDayEmp

(empNumber, year, month, day));

	if (dsReg.Tables["HoursRegForDayEmp"].Rows.Count == 0)

	{

		// Load users profile, add those registrations for this 

day and bind to dGridWorkingHoursReg

		dsReg = (new HoursRegData().allocateProfileToRegForDay

(empNumber, year, month, day));

		dsReg = (new HoursRegData().getHoursRegistrationForDayEmp

(empNumber, year, month, day));

		dGridWorkingHoursReg.DataSource = dsReg.Tables

["HoursRegForDayEmp"].DefaultView;

		panelBalance.Visible = false;

	}



	else

	{

		dGridWorkingHoursReg.DataSource = dsReg.Tables

["HoursRegForDayEmp"].DefaultView;

		bindHoursBalancePanel();

		panelBalance.Visible = true;

	}	

	

	dsCust = (new TimeRegDotNet.dataAccess.CustomerData

().getAllActiveCustomers("CUSTOMER.cust_CustomerName"));

	dsProj = (new TimeRegDotNet.dataAccess.ProjectData

().getAllActiveProjects("Proj_ProjectName"));

	dsAct = (new TimeRegDotNet.dataAccess.ActivityData

().getAllActiveActivities("Act_ActivityName"));

	dsAcc = (new TimeRegDotNet.dataAccess.AccountPlanData

().getAllActiveAccounts("AP_AccountName"));



	dGridWorkingHoursReg.DataBind();



	// Populates dropdownlists

	for (int i=0; i<dGridWorkingHoursReg.Items.Count; i++) 

	{

		DataGridItem _item = dGridWorkingHoursReg.Items[i];

		PopulateCustomers(_item.Cells[0].Text.ToString(), _item);

		PopulateProjects(_item.Cells[0].Text.ToString(), 

_item.Cells[1].Text.ToString(), _item);

		PopulateActivities(_item.Cells[1].Text.ToString

(),_item.Cells[2].Text.ToString(), _item);

		PopulateAccounts(_item.Cells[3].Text.ToString(), _item);



	}

			

}	



void PopulateAccounts(string AccID, DataGridItem gridItem)

{

	DropDownList ddlAccount = (DropDownList)gridItem.FindControl

("ddlAccount");

	

	DataSet dsAddAcc = new DataSet();

	dsAddAcc = (new AccountData()).getAllActiveAccounts

("AP_AccountName");

	ddlAccount.DataSource = dsAddAcc.Tables

["ActiveAccounts"].DefaultView;

	ddlAccount.DataTextField = "AP_AccountName";

	ddlAccount.DataValueField = "AP_AccountPlanID";

	ddlAccount.DataBind();

	

	foreach (ListItem item in ddlAccount.Items)

	{

		if (item.Value.ToString() == AccID)

		{

			item.Selected = true;

			break;

		}

	}

}				







> Whoo who!!!

> Finally (3 Days later) I have my Grid and my Template with my 

Dropdownbox 

> in it. Today I got the box to bind to a function that returns the 

foreign 

> table AND sets the DataValueField. I'm so proud :)

> 

> Does anybody know a workable approach for setting the selected index so 

> the dropdown has the correct value in it?

> 

> Here's my code snipits:

> 

> 

> <!--DATA GRID CODE -->

> 

> <asp:DataGrid id="MyDataGrid" runat="server" 

>         AutoGenerateColumns="false"

>         OnEditCommand="MyDataGrid_Edit"

>         OnCancelCommand="MyDataGrid_Cancel"

>         OnUpdateCommand="MyDataGrid_Update"

>         DataKeyField="LotID">

> 

> <Columns>

> <asp:EditCommandColumn EditText="Edit" 

>         CancelText="Cancel" UpdateText="Update" />

> 	

>         <asp:BoundColumn HeaderText="LotID" 

>                  DataField="LotID" ReadOnly="True" />

>         <asp:BoundColumn HeaderText="ProductID" 

>                  DataField="ProductID" />

> 	<asp:BoundColumn HeaderText="Warehouse Code"  

>                  DataField="WarehouseCode" />

> 

>         <asp:TemplateColumn HeaderText="Warehouse" 

SortExpression="state">

> 

>            <ItemTemplate>

> 		<asp:Label runat="server" 

>                     Text='<%# Container.DataItem("WarehouseID")%>'/>

> 	    </ItemTemplate>

> 									

> 	    <EditItemTemplate>

> 	        <asp:DropDownList runat="server" id="edit_WarehouseID" 

>                     DataSource='<%# GetWarehouses() %>'

>                     DataTextField = 'Name'	

>                     DatavalueField = 'WarehouseID'>

>                 </asp:DropDownList>

>             </EditItemTemplate>

> 

> 	</asp:TemplateColumn>

>  

>         <asp:BoundColumn HeaderText="Quantity" DataField="Quantity" />

> 	

> 	                                                            	

> 	<asp:BoundColumn HeaderText="Quantity Ordered" 

>              DataField="QuantityOrdered" />

> 

> </Columns>

> </asp:Datagrid>

> 

> <!--GET WAREHOUSES FUNCTION -->

> 

>     '******************************************

>     'Retrieves the Forign Table and returns a 

>     'DataTable.

>     'NOTE: Get Dataset is just my way of avoiding

>     'ASP.NET for the moment. It simple creates the

>     'connection, sqlDataAdapter and retrieves the

>     'date. Lazyness :)

>     '******************************************

> 

>     Function GetWarehouses()

> 	Dim MyDataSet as New Dataset

> 	MyDataset = GetDataSet("Select [Name], WarehouseID From 

> Warehouse","Warehouse")

> 	return MyDataset.Tables("Warehouse")

> 

>     End Function

> 


  Return to Index