Take the 2-minute tour ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

I have created template field with a ddl. I have instantiated an SPGridview and the ddl shows up but only with the default choice values. How do I bind to the actual values of the List item record?

Here is my code:

    public class ItemTemplateDDL:ITemplate
{
    private string _NAME;
    private string _DATAVALUEFIELD;
    private string _DATATEXTFIELD;
    private SPWeb _SITE;
    private string _LISTNAME;

    public ItemTemplateDDL(string ddlName, string DataValueField, string DataTextField, SPWeb site, string list)
    {
        this._DATATEXTFIELD = DataTextField;
        this._DATAVALUEFIELD = DataTextField;
        this._LISTNAME = list;
        this._SITE = site;
        this._NAME = DataTextField;

    }


    public void InstantiateIn(Control container)
    {
        DropDownList ddl = new DropDownList();
        ddl.DataBinding += new EventHandler(ddl_Databinding);
        ddl.SelectedIndexChanged +=new EventHandler(ddl_SelectedIndexChanged);

        container.Controls.Add(ddl);
    }

    private void ddl_Databinding(object sender, EventArgs e)
    {
        DropDownList ddl = (DropDownList)sender;
          SPList dataList = _SITE.Lists[_LISTNAME];


          SPFieldChoice field = (SPFieldChoice)dataList.Fields[_DATAVALUEFIELD];


          ddl.DataSource = field.Choices;

    }

    private void ddl_SelectedIndexChanged(object sender, EventArgs e)
    {
        string h = "";
    }
}
share|improve this question

1 Answer 1

up vote 0 down vote accepted

I was able to answer my own question:

private void ddl_Databinding(object sender, EventArgs e)
    {
        DropDownList ddl = (DropDownList)sender;
          SPList dataList = _SITE.Lists[_LISTNAME];


          SPFieldChoice field = (SPFieldChoice)dataList.Fields[_DATAVALUEFIELD];
          ddl.DataSource = field.Choices;

          SPGridViewRow container = (SPGridViewRow)ddl.NamingContainer;
          ddl.Text = DataBinder.Eval(container.DataItem, _NAME).ToString(); 




    }
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.