hi seema,
I write some codes:
In Html Page:
<asp:gridview runat="server" id="GridView1" autogeneratecolumns="false" cellpadding="3" cellspacing="0" xmlns:asp="#unknown">
<columns>
<asp:templatefield headertext="Column1">
<itemtemplate>
<asp:dropdownlist runat="server" id="DropDownList1" width="150px/">
<asp:hiddenfield runat="server" id="HiddenField1" value="<%#Bind("Column1") %"> />
</asp:hiddenfield></asp:dropdownlist></itemtemplate>
</asp:templatefield>
<asp:boundfield datafield="Column2" headertext="Column2" />
<asp:boundfield datafield="Column3" headertext="Column3" />
</columns>
</asp:gridview>
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
BindGridView();
}
}
private void BindGridView()
{
DataTable datatable = new DataTable();
datatable.Columns.Add("Column1", typeof(System.String));
datatable.Columns.Add("Column2", typeof(System.String));
datatable.Columns.Add("Column3", typeof(System.String));
datatable.Rows.Add("1", "2", "3");
datatable.Rows.Add("4", "5", "6");
this.GridView1.DataSource = datatable;
this.GridView1.DataBind();
foreach (GridViewRow GridViewRow1 in this.GridView1.Rows)
{
//find hiddenField and DropDownList by id
HiddenField hiddenField1 = (HiddenField)GridViewRow1.FindControl("HiddenField1");
DropDownList dropDownList1 = (DropDownList)GridViewRow1.FindControl("DropDownList1");
if (hiddenField1 != null && dropDownList1 != null)
{
dropDownList1.Items.Clear;
//bind dropDownList1
//.....
dropDownList1.SelectedValue = hiddenField1.Value;
}
}
}
Hope make you understand.