Re: GridView and DDL

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance



In your dropdownlist's tag, simply set the selectedvalue attribute. I know
it doesnt show up in the intellisense, but i actually just did this very
same thing this afternoon and it worked.

<asp:dropdownlist id="lstMyList" runat="server" selectedValue='<%#
Eval("LEASEID") %>' >
... a bunch of items ....
</asp:dropdownlist>

I cant remember for sure though if i used Eval or Bind. But it did work.

HTH.

"Mike Robbins" <robbinsm@xxxxxxxxxxxxxxxxxxxx> wrote in message
news:uBBVp06RGHA.4456@xxxxxxxxxxxxxxxxxxxxxxx
I'm trying to find an article that will help me use a dropdownlist in a
GridView and have the selecteditem set appropriately in the ddl.

<asp:GridView ID="GridView1" runat="server"
AutoGenerateColumns="False" DataSourceID="odsRooms">

<Columns>

<asp:TemplateField HeaderText="LeaseID"
SortExpression="LeaseID">

<EditItemTemplate>

<asp:DropDownList ID="DropDownList1" runat="server"
DataSourceID="odsLeases"

DataTextField="LeaseName"
DataValueField="LeaseID">

</asp:DropDownList>

</EditItemTemplate>

<ItemTemplate>

&nbsp;<asp:DropDownList ID="DropDownList1"
runat="server" DataSourceID="odsLeases"

DataTextField="LeaseName"
DataValueField="LeaseID">

</asp:DropDownList>

</ItemTemplate>

</asp:TemplateField>

<asp:BoundField DataField="RoomID" HeaderText="RoomID"
SortExpression="RoomID" />

<asp:BoundField DataField="RoomName" HeaderText="RoomName"
SortExpression="RoomName" />

<asp:BoundField DataField="RoomOrder"
HeaderText="RoomOrder" SortExpression="RoomOrder" />

<asp:BoundField DataField="Active" HeaderText="Active"
SortExpression="Active" />

</Columns>

</asp:GridView>

<asp:ObjectDataSource ID="odsRooms" runat="server"
SelectMethod="GetList" TypeName="Room">



</asp:ObjectDataSource>

<asp:ObjectDataSource ID="odsLeases" runat="server"
SelectMethod="GetLeases" TypeName="Lease">

</asp:ObjectDataSource>

All of the ddls show as having the first item selected. How do you bind
the ddl's DataValueField to the GridView's LeaseID value for each row?

Thanks.

Mike

public class Room

{

private long m_RoomID;

private long m_LeaseID;

private string m_RoomName;

private int m_RoomOrder;

private int m_Active;



public long RoomID

{

get { return (m_RoomID); }

set {m_RoomID = value;}

}



public long LeaseID

{

get { return (m_LeaseID); }

set { m_LeaseID = value; }

}



public string RoomName

{

get { return (m_RoomName); }

set { m_RoomName = value; }

}



public int RoomOrder

{

get { return (m_RoomOrder); }

set { m_RoomOrder = value; }

}



public int Active

{

get { return (m_Active); }

set { m_Active = value; }

}



public Room()

{

//

// TODO: Add constructor logic here

//

}



public List<Room> GetList(long leaseID)

{

OracleConnection roomConnection = new OracleConnection("user
id=test;password=test;data source=test;persist security info=False");

OracleCommand roomCommand = new OracleCommand("SELECT * FROM
Rooms");

OracleDataReader roomData;



roomCommand.CommandType = CommandType.Text;

roomCommand.Connection = roomConnection;



roomConnection.Open();



roomData = roomCommand.ExecuteReader();



List<Room> RoomList = new List<Room>();



while (roomData.Read())

{

Room thisRoom = new Room();

thisRoom.RoomID = roomData.GetInt32(0);

thisRoom.LeaseID = roomData.GetInt32(1);

thisRoom.RoomName = roomData.GetString(2);

thisRoom.RoomOrder = roomData.GetInt32(3);

thisRoom.Active = roomData.GetInt32(4);



RoomList.Add(thisRoom);

}

roomConnection.Close();



return RoomList;

}

}



public class Lease

{

private long m_LeaseID;

private string m_LeaseName;

private string m_Description;

private int m_Active;



public long LeaseID

{

get { return (m_LeaseID); }

set { m_LeaseID = value; }

}



public string LeaseName

{

get { return (m_LeaseName); }

set { m_LeaseName = value; }

}



public string Description

{

get { return (m_Description); }

set { m_Description = value; }

}



public int Active

{

get { return (m_Active); }

set { m_Active = value; }

}



public Lease()

{

}



public List<Lease> GetLeases()

{

OracleConnection leaseConnection = new OracleConnection("user
id=test;password=test;data source=test;persist security info=False");

OracleCommand leaseCommand = new OracleCommand("SELECT LeaseID,
LeaseName, Description, Active FROM Leases");

OracleDataReader leaseData;



leaseCommand.CommandType = CommandType.Text;

leaseCommand.Connection = leaseConnection;



leaseConnection.Open();



leaseData = leaseCommand.ExecuteReader();



List<Lease> LeaseList = new List<Lease>();



while (leaseData.Read())

{

Lease thisLease = new Lease();

thisLease.LeaseID = leaseData.GetInt32(0);

thisLease.LeaseName = leaseData.GetString(1);

thisLease.Description = leaseData.GetString(2);

thisLease.Active = leaseData.GetInt32(3);



LeaseList.Add(thisLease);

}

leaseConnection.Close();



return LeaseList;

}

}








.



Relevant Pages

  • Re: GridView and DDL
    ... private long m_LeaseID; ... private int m_RoomOrder; ... private string m_LeaseName; ... OracleConnection leaseConnection = new OracleConnection("user ...
    (microsoft.public.dotnet.framework.aspnet.datagridcontrol)
  • GridView and DDL
    ... private long m_LeaseID; ... private int m_RoomOrder; ... private string m_LeaseName; ... OracleConnection leaseConnection = new OracleConnection("user ...
    (microsoft.public.dotnet.framework.aspnet.datagridcontrol)
  • Total newbie array problem
    ... removeModule method to remove it from the array. ... private int[] moduleArray; ... private String studentName; ...
    (comp.lang.java.help)
  • Re: Total newbie array problem
    ... > removeModule method to remove it from the array. ... > private String number; ... > private String moduleCode; ... > private int semester1; ...
    (comp.lang.java.help)
  • Re: IRQ and Serial Ports
    ... private int cont;//informs the bill counter number ... private string nummaq;//keeps the machine number ... public void FechaPorta() ... private void SendBlock ...
    (microsoft.public.dotnet.languages.csharp)