Before doing this create datasource DataSources["options"]. with XML
private void BindCustomerDropDown()
{
try
{
DataTable dtCustomer = // Load datatable
if (dtCustomer != null && dtCustomer.Rows.Count > 0)
{
string name = string.Empty;
string id = string.Empty;
RemoveFirstItem();
foreach (DataRow row in dtCustomer.Rows)
{
if (row["CustomerName"] != null)
name = row["CustomerName"].ToString();
if (row["CustomerId"] != null)
id = row["CustomerId"].ToString();
AddItem(id, name);
}
RemoveFirstItem();
}
}
catch (System.Exception ex)
{
ShowMessage(ex.Message);
}
}
private void RemoveFirstItem()
{
XPathNavigator DOM = DataSources["options"].CreateNavigator();
if (DOM == null)
return;
XPathNavigator group1 = DOM.SelectSingleNode("//options", NamespaceManager);
if (group1 == null)
return;
XPathNavigator field1 = DOM.SelectSingleNode("//options/option", NamespaceManager);
if (field1 == null)
return;
field1.DeleteSelf();
group1.InnerXml = group1.InnerXml.Replace("\r", " ");
group1.InnerXml = group1.InnerXml.Replace("\n", " ");
}
private void AddItem(string itemId, string itemName)
{
//RemoveFirstItem();
XPathNavigator DOM = DataSources["options"].CreateNavigator();
if (DOM == null)
return;
XPathNavigator group1 = DOM.SelectSingleNode("//options", NamespaceManager);
if (group1 == null)
return;
XPathNavigator field1 = DOM.SelectSingleNode("//options/option", NamespaceManager);
if (field1 == null)
return;
XPathNavigator newNode = field1.Clone();
newNode.SelectSingleNode("value").SetValue(itemId);
newNode.SelectSingleNode("displayname").SetValue(itemName);
group1.AppendChild(newNode);
}
you can refer these links for more details
http://www.bizsupportonline.net/infopath2007/programmatically-fill-populate-drop-down-list-box-infopath-2007.htm
http://blogs.catapultsystems.com/sboldt/archive/2009/08/07/populating-a-list-box-in-infopath-2007-form-services-from-a-sql-stored-procedure.aspx