GridView and DDL

M

Mike Robbins

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;

}

}
 
A

Arthur Dent

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.
 
M

Mike Robbins

Thanks, Arthur.

I was looking for something like that and when it didn't show up in
Intellisense, I didn't think to research it further. I guess that taught me
a lesson.

Bind() worked fine.

Mike
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,968
Messages
2,570,154
Members
46,702
Latest member
LukasConde

Latest Threads

Top