Saturday, March 6, 2010

How to fill DROPDOWN LIST on row data bound

protected void Page_Load(object sender, EventArgs e)
{
conn.Open();
da = new SqlDataAdapter("Select User_id,Name,EmailId,City_id,Qualification from tbl_user", conn);
da.Fill(ds,"tbl_User");
GridView1.DataSource = ds;
GridView1.DataBind();
GridView1.Columns[3].Visible = false;
conn.Close();
}



protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
try
{
if (e.Row.RowType != DataControlRowType.Header)
{
DropDownList drdp1 = new DropDownList();

drdp1 = (DropDownList)e.Row.Cells[4].FindControl("drdp");

//int user_id =Convert.ToInt32(e.Row.Cells[1].Text);

DataSet ds1 = new DataSet();

SqlDataAdapter da1 = new SqlDataAdapter("Select City_name,City_id from tbl_city", conn);

da1.Fill(ds1, "tbl_drdp");

drdp1.DataSource = ds1;

drdp1.DataTextField = ds1.Tables[0].Columns["City_name"].ColumnName.ToString();

drdp1.DataValueField = ds1.Tables[0].Columns["City_id"].ColumnName.ToString();

drdp1.SelectedIndex = Convert.ToInt32(e.Row.Cells[3].Text);

drdp1.DataBind();
}

}
catch(Exception ex)
{
ex = ex;
}
}

No comments:

Post a Comment