Wednesday, April 6, 2011

Populate Dropdown lists in GridView in asp.net-II

other than doing in the previous post, we can do it in the following way





protected void GridView1_RowDataBound(object sender,GridViewRowEventArgs e)
{
DropDownList ddl = new DropDownList();
ddl = (DropDownList)e.Row.Cells[Cell No].FindControl("Template DropDownId");
//Code to bind the dropdownlist...
}

Populate Dropdown lists in GridView in asp.net

How to do it is :


Write a method which will get the values from the database and it returns a dataset:

--------------------------------------------------------------------------
// This method populates the DropDownList

public DataSet PopulateControls()
{
SqlConnection myConnection = new SqlConnection(GetConnectionString());
SqlDataAdapter ad = new SqlDataAdapter("SELECT [Name] FROM tblPerson",
myConnection);
DataSet ds = new DataSet();
ad.Fill(ds, "tblPerson");
return ds;
}
--------------------------------------------------------------------------


Now inside the itemtemplate tag do the following




Thats it........Done.

Sunday, April 3, 2011

Dictionary as a datasource to a dropdown

Wondering how to provide the dictionary object as the datasource to the dropdown:

CODE:
--------------------------------------------------------------
//Setting the dictionary item list

dctLists.Add("hyderabad", 500020);
dctLists.Add("sec-bad", 500009);
dctLists.Add("chikkadpally", 500020);

//poplting the second ddl
ddlListWithValues.DataSource = dctLists;
ddlListWithValues.DataTextField = "Key";
ddlListWithValues.DataValueField = "Value";
ddlListWithValues.DataBind();

--------------------------------------------------------------

Saturday, April 2, 2011

Display more than 1 display values in a dropdown with ',' seperated

Are you looking for displaying more than 12 values in a dropdown with ',' seperated.
It is possible using the generics DICTIONARY class:
CODE::

---------------------------------------------------------------------------------
Dictionary dctLists = null;
//Setting the dictionary item list
dctLists = new Dictionary();
dctLists.Add("hyderabad", 500020);
dctLists.Add("sec-bad", 500009);
dctLists.Add("chikkadpally", 500020);

//Display more than 1 value in the dropdown
ddlMultipleValsDisplaye.DataSource = dctLists;
ddlMultipleValsDisplaye.DataBind();

---------------------------------------------------------------------------------

if you run the above code, then you will see that both the valuees from the dictionary are displayed in the dropdownlist.