Home > Forum > ASP.NET > Maintaining Value of drop down list on clicking Edit button

Maintaining Value of drop down list on clicking Edit button

by Dave   on Nov 09, 2013   Category: ASP.NET  |  Views: 3639    |  Points: 5   |  Starter 
  Reply
If user clicks on the edit button then the value displayed in drop down list changes.

Eg Design

<EditItemTemplate>
<asp:DropDownList ID="ddlDonationEntity" runat="server">
<asp:ListItem Value="1">One</asp:ListItem>
<asp:ListItem Value="2">Two</asp:ListItem>
</asp:DropDownList>
</EditItemTemplate>

Eg: Data in Grid
Name Value
Test Two
Two Two
If I click on the first row then in ddl One is displayed inspite of the original value which is Two,same in case when 2nd row is selected.



Bookmark and Share:
 
  User Reply  | Ask a question  |   Reply 
  Re :Maintaining Value of drop down list on clicking Edit button   
by Thamil
on Nov 10 2013 4:14AM
Points : 10
Gold 
Hi Dave,

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.


<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();
}

also refer the below link

http://www.dotnetgallery.com/kb/resource14-Dropdown-checkbox-and-textbox-controls-in-aspnet-editable-Gridview.aspx
  Re :Maintaining Value of drop down list on clicking Edit button   
by Dave
on Nov 10 2013 8:03PM
Points : 10
Starter 
Hi,
I appreciate your answer but in this case the value in the drop down list is not coming from the Database,its manually written in the source as per my example.
The reason the values are not coming from the database is that the ddl has only two values.

Recent Post

Latest Posts