|
i had a dropdownlist in datagrid. datagrid showing details of a table.
i want dropdownlist list items from other table. where one column in both tables is same. and droplist should update whenever i update the data grid.
code i tried....
<asp:DropDownList ID="DropDownList1" AutoPostBack="true" DataTextField="dept" DataValueField="dept" runat="server" DataSource="<%# ddllist()%>">
</asp:DropDownList>
protected void DataGid1_ItemDataBound(object sender, System.Web.UI.WebControls.DataGridItemEventArgs e)
{
SqlConnection cnn = new SqlConnection("Data Source=NETZOOM\\MADHUK_2005;Integrated Security=SSPI;Initial Catalog=master");
string sql = "select dept from dptmnt";
DataSet ds = new DataSet();
SqlCommand cmd = new SqlCommand(sql, cnn);
cmd.Connection.Open();
SqlDataAdapter ad1 = new SqlDataAdapter(cmd);
ad1.Fill(ds, "dptmnt");
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
{
DropDownList DropDownList1 = (DropDownList)((DataGridItem)e.Item).FindControl(" DropDownList1");
ListItem li = new ListItem();
DropDownList1.Items.Add(li);
DropDownList1.DataSource = ds.Tables["dptmnt"].DefaultView;
DropDownList1.DataValueField = "dept";
DropDownList1.DataTextField = "dept";
DropDownList1.DataBind();
cmd.Connection.Close();
}
}
public DataSet ddllist()
{
string sql = "select dept from dptmnt";
SqlDataAdapter ad1 = new SqlDataAdapter(sql, cnn);
DataSet ds = new DataSet();
ad1.Fill(ds, "dptmnt");
return ds;
}
thanks...
Madhu.k
|