This is simple and easy, Please follow below steps to solve your problem. i have created sample gridview with dropdown list and loaded sample employee data from datatable.
1. Load the data in gridview from dataTable 2. I assume that employee dep have the value with dropdownlist. 3. when you click on Edit button, OnRowDataBound event will be fired and get the specific row in DataRowView as like below DataRowView drview = e.Row.DataItem as DataRowView; 4. Then load the Emp_dep dropdown from GetEmpDept() method which will return departments datatable
5. After loaded the deprt detail in dropdown then select orginal value of the cell from DataRowView, thats it.
Balaji A47 pointsPosted Jul 14, 2015, 1:12 AM EDT
1. Load the data in gridview from dataTable
2. I assume that employee dep have the value with dropdownlist.
3. when you click on Edit button, OnRowDataBound event will be fired and get the specific row in DataRowView as like below
DataRowView drview = e.Row.DataItem as DataRowView;
4. Then load the Emp_dep dropdown from GetEmpDept() method which will return departments datatable
<asp:GridView ID="GridViewSample" runat="server" AutoGenerateColumns="False" Font-Names="Verdana" AllowPaging="true" PageSize="5" Width="75%" DataKeyNames="Emp_id" BorderColor="#CCCCCC" BorderStyle="Solid" OnRowCancelingEdit="GridViewSample_RowCancelingEdit" OnRowEditing="GridViewSample_RowEditing" OnRowUpdating="GridViewSample_RowUpdating" BorderWidth="1px" OnRowDataBound="GridViewSample_RowDataBound"> <AlternatingRowStyle BackColor="#FFD4BA" /> <FooterStyle BorderColor="#CCCCCC" BorderStyle="Solid" BorderWidth="1px" /> <PagerStyle BorderColor="#CCCCCC" BorderStyle="Solid" BorderWidth="1px" /> <HeaderStyle Height="30px" BackColor="#BC3670" Font-Size="15px" BorderColor="#CCCCCC" BorderStyle="Solid" BorderWidth="1px" /> <RowStyle Height="20px" Font-Size="13px" BorderColor="#CCCCCC" BorderStyle="Solid" BorderWidth="1px" /> <Columns> <asp:TemplateField HeaderText="Employee Name" HeaderStyle-Width="10%"> <ItemTemplate> <asp:Label ID="lblEmpName" runat="server" Text='<%#Eval("Emp_Name") %>'></asp:Label> </ItemTemplate> <EditItemTemplate> <asp:TextBox ID="txtEmpName" runat="server" Text='<%#Eval("Emp_Name") %>'></asp:TextBox> </EditItemTemplate> <HeaderStyle Width="15%"></HeaderStyle> </asp:TemplateField> <asp:TemplateField HeaderText="Job" HeaderStyle-Width="15%"> <ItemTemplate> <asp:Label ID="lblJob" runat="server" Text='<%#Eval("Emp_job") %>'></asp:Label> </ItemTemplate> <EditItemTemplate> <asp:TextBox ID="txtEmpJob" runat="server" Text='<%#Eval("Emp_job") %>'></asp:TextBox> </EditItemTemplate> <HeaderStyle Width="15%"></HeaderStyle> </asp:TemplateField> <asp:TemplateField HeaderText="Department" HeaderStyle-Width="15%"> <ItemTemplate> <asp:Label ID="lblEmpDep" runat="server" Text='<%#Eval("Emp_Dep") %>'></asp:Label> </ItemTemplate> <EditItemTemplate> <asp:DropDownList ID="DrpDwnEmpDept" runat="server" Width="150px"> </asp:DropDownList> </EditItemTemplate> <HeaderStyle Width="15%"></HeaderStyle> </asp:TemplateField> <asp:TemplateField HeaderText="Edit" HeaderStyle-Width="15%"> <ItemTemplate> <asp:LinkButton ID="btnEdit" Text="Edit" runat="server" CommandName="Edit" /> </ItemTemplate> <EditItemTemplate> <asp:LinkButton ID="btnUpdate" Text="Update" runat="server" CommandName="Update" /> <asp:LinkButton ID="btnCancel" Text="Cancel" runat="server" CommandName="Cancel" /> </EditItemTemplate> <HeaderStyle Width="15%"></HeaderStyle> </asp:TemplateField> </Columns> </asp:GridView>protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { LoadData(); } } private void LoadData() { DataTable dt = new DataTable(); dt.Columns.Add("Emp_id"); dt.Columns.Add("Emp_Name"); dt.Columns.Add("Emp_job"); dt.Columns.Add("Emp_Dep"); DataRow rw3 = dt.NewRow(); rw3["Emp_id"] = "2700"; rw3["Emp_Name"] = "Muthu kumar"; rw3["Emp_job"] = "Support Enginner"; rw3["Emp_Dep"] = "IT"; dt.Rows.Add(rw3); DataRow rw4 = dt.NewRow(); rw4["Emp_id"] = "4678"; rw4["Emp_Name"] = "Arvind"; rw4["Emp_job"] = "Sr Software Engineer"; rw4["Emp_Dep"] = "Finance"; dt.Rows.Add(rw4); DataRow rw5 = dt.NewRow(); rw5["Emp_id"] = "2300"; rw5["Emp_Name"] = "Raja ram"; rw5["Emp_job"] = "Test Engineer"; rw5["Emp_Dep"] = "Security"; dt.Rows.Add(rw5); GridViewSample.DataSource = dt; GridViewSample.DataBind(); } protected void GridViewSample_RowDataBound(object sender, GridViewRowEventArgs e) { DataRowView drview = e.Row.DataItem as DataRowView; if (e.Row.RowType == DataControlRowType.DataRow) { if ((e.Row.RowState & DataControlRowState.Edit) > 0) { DropDownList dpEmpdept = (DropDownList)e.Row.FindControl("DrpDwnEmpDept"); DataTable dt = GetEmpDept(); dpEmpdept.DataSource = GetEmpDept(); dpEmpdept.DataTextField = "DepName"; dpEmpdept.DataValueField = "DepName"; dpEmpdept.DataBind(); dpEmpdept.SelectedValue = drview[3].ToString(); } } } private DataTable GetEmpDept() { //Get Employee department DataTable dt = new DataTable(); dt.Columns.Add("DepName"); DataRow rw1 = dt.NewRow(); rw1[0] = "IT"; dt.Rows.Add(rw1); DataRow rw2 = dt.NewRow(); rw2[0] = "Finance"; dt.Rows.Add(rw2); DataRow rw3 = dt.NewRow(); rw3[0] = "Security"; dt.Rows.Add(rw3); return dt; } protected void GridViewSample_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e) { GridViewSample.EditIndex = -1; LoadData(); } protected void GridViewSample_RowEditing(object sender, GridViewEditEventArgs e) { GridViewSample.EditIndex = e.NewEditIndex; LoadData(); } protected void GridViewSample_RowUpdating(object sender, GridViewUpdateEventArgs e) { GridViewSample.EditIndex = -1; LoadData(); }