views:

57

answers:

3

Hi, I'm trying to add a lookup type column to a gridview. The column will be used as a picklist of values for the user to, well, pick a value from. Sounds fairly straight forward but the problem is I want the picklist column to be a distinct list from a table that is not in the datasource of the gridview. If I join the picklist table into my datasource I may not get the full list of values, if I create a union I suspect I'll get multiple entries in the picklist column. So I think I need to join the picklist column to another datasource or...? Dunno? over to you folks (hopefully) and thanks for any help offered.

WILL

+1  A: 

You can create a template column that contains a dropdownlist. You can then have the dropdownlist be databound by anything including a seperate objectdatasource.

<asp:TemplateField HeaderText="Lookup">
    <itemtemplate>
        <asp:DropDownList runat="server" DataSourceId="SeperateDataSource" SelectedValue='<%# Bind("ValueThatIsSelectedUID") %>' ></asp:DropDownList>
        <asp:ObjectDataSource runat="server" id="SeperateDataSource" selectmethod="SomeSelectMethod"></asp:ObjectDataSource>
    </itemtemplate>
</asp:TemplateField>

The template field also supports edititemtemplate, footertemplate and headertemplate.

Chris
Good man, thanks Chris. I've almost got the drop down list working, I just need to get it to update the row in which it exists with the value that was selected oh and I'll need to set it to the correct value for each row when the grid is instansiated. But anyway not far off, thanks again.
WILL, you don't have to use the rowdatabound event to set the selected value of the dropdownlist. Instead you can databind directly from the datasource in the markup. I revised my example to show how to databind to the selectedvalue attribute.
Chris
+1  A: 

You're going to need to make an edit template that has a dropdown list in it. Then inside your gridview's databound event, you'll need code to fill the ddl:

protected void grdGrid_RowDataBound(object sender, GridViewRowEventArgs e) 
{ 

  if (grdGrid.EditIndex == e.Row.RowIndex) 
  { 
    ddlMine = (DropDownList)e.Row.Cells[0].FindControl("ddlMine"); 
    //bind ddlMine
  }
}
JustLoren
It's C# code in the code behind. Translating it to VB should be trivial.
Jason Berkan
A: 

check the url below

http://asimsajjad.blogspot.com/2009/04/dropdownlist-binding-in-gridview-aspnet.html

hope that will help.

Asim Sajjad