Get and set the value in dropdownlist of GridView ItemTemplate and EditItemTemplate.
In this article we are going to see how we can Get and Set the value in dropdownlist of GridView ItemTemplate and EditItemTemplate.
We have 2 tables, Emp Table with columns EmpId,Emp Name,DeptId and Dept table with columns DeptId,DeptName. We will display EmpId,EmpName and Dept Name of each employee in the GridView .
Get and set the value in dropdownlist of GridView ItemTemplate and EditItemTemplate.
We have 2 tables, Emp Table with columns EmpId,Emp Name,DeptId and Dept table with columns DeptId,DeptName. We will display EmpId,EmpName and Dept Name of each employee in the GridView . Here the focus is on How we are displaying the Dept Name of the Employee in the Dropdownlist and How are able to edit and save back the selected DeptName for each Employee in the database using dropdownlist of EditItemTemplate.
Step1: In the Default.aspx page, write the below code which displays the EmpId,Name and Dept Name in the DropDownList.
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" OnRowEditing="GridView1_RowEditing" OnRowUpdating="GridView1_RowUpdating">
<Columns>
<asp:TemplateField HeaderText="Emp Id">
<EditItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%# Bind("EmpId") %>'></asp:Label>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%# Bind("EmpId") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Emp Name">
<EditItemTemplate>
<asp:TextBox ID="TextBox2" runat="server" Text='<%# Bind("Name") %>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label2" runat="server" Text='<%# Bind("Name") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="DeptName">
<ItemTemplate>
<asp:DropDownList runat="server" ID="ddlDeptId" Enabled="false" DataSourceID="sdsDept" SelectedValue='<%#Bind("DeptId") %>' DataTextField="DeptName" DataValueField="DeptId"></asp:DropDownList>
</ItemTemplate>
<EditItemTemplate>
<asp:DropDownList runat="server" ID="ddlDeptId" Enabled="true" DataSourceID="sdsDept" DataTextField="DeptName" DataValueField="DeptId"></asp:DropDownList>
</EditItemTemplate>
</asp:TemplateField>
<asp:CommandField ShowEditButton="true" ShowSelectButton="true" />
</Columns>
</asp:GridView>
<asp:SqlDataSource ID="sdsDept" runat="server" ConnectionString="<%$ ConnectionStrings:CompanyDBConnectionString %>" SelectCommand="SELECT [DeptId], [DeptName] FROM [Dept]"></asp:SqlDataSource>
Step 2:In the Default.aspx.cs file, In the Page_Load event get the Emp Details and display it in the Grid View.
SqlConnection con;
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
UpdateGrid();
}
}
protected void UpdateGrid()
{
con = new SqlConnection(@"Data Source=.\SQLEXPRESS;Initial Catalog=CompanyDB;Integrated Security=True;Pooling=False");
SqlDataAdapter daUsers = new SqlDataAdapter("select EmpId,Name,DeptId from Emp", con);
DataSet ds = new DataSet();
daUsers.Fill(ds, "Emp");
GridView1.DataSource = ds.Tables["Emp"];
GridView1.DataBind();
con.Close();
}
Step 3:When a particular Emp Details are edited and updated. Below events are fired. GridView1_RowEditing event is fired when we click on Edit button of the GridView. GridView1_RowUpdating event is fired when we update the Emp Details. In the below code, e.NewValues is a collection of all the new and updated values of a particular Emp. To get the selected DeptId we are using Findcontrol method of the row which is in Edit Mode and then getting the SelectedValue of the DropDownList(DeptId).
protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
{
}
protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
con = new SqlConnection(@"Data Source=.\SQLEXPRESS;Initial Catalog=CompanyDB;Integrated Security=True;Pooling=False");
string val = ((DropDownList)(GridView1.Rows[e.RowIndex].FindControl("ddlDeptId"))).SelectedItem.Value;
SqlCommand cmd = new SqlCommand("Update Emp Set DeptId="+val + " , Name='"+e.NewValues["Name"]+"' where EmpId=" +e.NewValues["EmpId"] , con);
con.Open();
cmd.ExecuteNonQuery();
con.Close();
GridView1.EditIndex = -1;
UpdateGrid();
}
Below is how the data is updated in the GridView.