Pages

Saturday, November 27, 2010

DropdownList in ItemTemplate having the item preselected


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


############################################################

<asp:GridView ID="grdData" DataKeyNames="userid,categoryname">


protected void grdData_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
DropDownList _ddlcategory = (DropDownList)e.Row.FindControl("ddlcategory");
if (_ddlcategory != null)
{
_ddlcategory.DataSource = GetCategory();
_ddlcategory.DataTextField = "categoryname";
_ddlcategory.DataValueField = "categoryname";
_ddlcategory.DataBind();

_ddlcategory.SelectedItem.Text = grdData.DataKeys[e.Row.RowIndex].Values[1].ToString();


}

}

}



public DataTable GetCategory()
{
DataTable dt = new DataTable();
SqlConnection con = new SqlConnection();
con.ConnectionString = ConfigurationManager.ConnectionStrings["conPractice"].ToString();

SqlDataAdapter adp = new SqlDataAdapter("GetCategory", con);
adp.Fill(dt);

return dt;

}

############################################


0 comments:

Post a Comment

 

CodeAddict.com | Copyright 2009 Tüm Haklar? Sakl?d?r | Free Blogger Templates by GoogleBoy Download Free Wordpress Templates. Unblock through unblock myspace proxy, Hillsongs by Guitar Song Chords