Click here to Skip to main content
15,897,273 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello,


Can we assign a dropdownlist to a datatable?

C#
DropDownList drp = new DropDownList();
        drp.Items.Add("Delho");
        drp.Items.Add("Mumbai");

 DataTable dt = new DataTable();
DataRow dr =dt.NewRow();


Is it possible to display the dropdownlist in datatable.

Thanks
Posted
Comments
Member 11099211 25-Sep-14 5:43am    
Hello

Thanks for the reply.The code is displaying the items of the dropdownlist in the datatable but I want dropdownlist to be displayed in the datatable .It is like the way we add a dropdown in a gridview.
Abdul Samad KP 25-Sep-14 7:57am    
What you mean by display, as far as I know datatable is not a control and it cannot be used to display anything but bound to other controls to dispalay its contents.

1 solution

try like below
 var lst=new List<string>();
DropDownList drp = new DropDownList();
        drp.Items.Add("Delho");
        drp.Items.Add("Mumbai");
foreach(object o in drp.items)
{
lst.Add(o.ToString);
}

DataTable table = ConvertListToDataTable(lst);
static DataTable ConvertListToDataTable(List<string[]> list)
	{
	    // New table.
	    DataTable table = new DataTable();

	    // Get max columns.
	    int columns = 0;
	    foreach (var array in list)
	    {
		if (array.Length > columns)
		{
		    columns = array.Length;
		}
	    }

	    // Add columns.
	    for (int i = 0; i < columns; i++)
	    {
		table.Columns.Add();
	    }

	    // Add rows.
	    foreach (var array in list)
	    {
		table.Rows.Add(array);
	    }

	    return table;
	}


Hope this helps
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900