DropdownList SelectedIndexChanged in Gridview in Asp.net C#


Dropdownlist SelectedndexChanged Event in Gridview

Dropdownlist SelectedndexChanged Event in Gridview

hello friends,
In this articles i'm going to show you how to add Dropdownlist to gridview control. In this gridview control we edit particular gridview row data through this DropdownList selectedIndexChanged event. If i changed the value of dropdownlist then its immediately execute selectedIndexChanged event and updated the sql server database table.

HTML Markup : Add Gridview to HTML Page


 <div style="background:#ffffff; margin-top:-15px; height:500px; padding:10px 25px 50px 25px;">
  <div style="padding:10px;" align="center">
   
   <table class="table_BS">
    <tr>
        <td class="td_1">Book ID :</td>
        <td class="td_2"><asp:TextBox ID="txt_bookid" CssClass="txtbox" runat="server" 
                AutoPostBack="True" ontextchanged="txt_bookid_TextChanged"></asp:TextBox></td>
        <td class="td_1">Student ID :</td>
        <td class="td_2"><asp:TextBox ID="TextBox1" CssClass="txtbox" runat="server"></asp:TextBox></td>
   </tr>
   </table>
  </div>
  <br />
  <div align="center">  
   <asp:GridView ID="Grd_Pending_Status" AutoGenerateColumns="False" 
          HeaderStyle-CssClass="headerstyle" runat="server" BackColor="White" 
          BorderColor="White" BorderStyle="Ridge" BorderWidth="2px" CellPadding="3" 
          CellSpacing="1" GridLines="None" DataKeyNames="bookid,studentid" 
          onrowdatabound="Grd_Pending_Status_RowDataBound" >
    <Columns>
       <asp:TemplateField HeaderText="Book ID">
         <ItemTemplate>
         <asp:Label ID="lblbookid" runat="server" Text='<%# Evalundefined"bookid") %>' />
         </ItemTemplate>
        </asp:TemplateField>
         <asp:TemplateField HeaderText="Student ID">
         <ItemTemplate>
         <asp:Label ID="lblstudentid" runat="server" Text='<%# Evalundefined"studentid") %>'/>
         </ItemTemplate>
        </asp:TemplateField>
         <asp:TemplateField HeaderText="Start Date">
         <ItemTemplate>
         <asp:Label ID="lblstartdate" runat="server" Text='<%# Evalundefined"startdate") %>'/>
         </ItemTemplate>
        </asp:TemplateField>
         <asp:TemplateField HeaderText="End Date">
         <ItemTemplate>
         <asp:Label ID="lblenddate" runat="server" Text='<%# Evalundefined"enddate") %>'/>
         </ItemTemplate>
        </asp:TemplateField>
         <asp:TemplateField HeaderText="Due Days">
         <ItemTemplate>
         <asp:Label ID="lblduedays" runat="server" Text='<%# Evalundefined"duedays") %>'/>
         </ItemTemplate>
        </asp:TemplateField>
         <asp:TemplateField HeaderText="Penality">
         <ItemTemplate>
         <asp:Label ID="lblpenality" runat="server" Text='<%# Evalundefined"penality") %>' />
         </ItemTemplate>
        </asp:TemplateField>
       <asp:TemplateField HeaderText="Status">
         <ItemTemplate>
           <asp:Label ID="lblstatus" runat="server" Text='<%# Evalundefined"status") %>' Visible = "false" />
          <asp:DropDownList ID="ddl_status" Width="120px" runat="server" AutoPostBack="True" onselectedindexchanged="ddl_status_SelectedIndexChanged"></asp:DropDownList>
         </ItemTemplate>
        </asp:TemplateField>
    </Columns>
       <FooterStyle BackColor="#C6C3C6" ForeColor="Black" />

<HeaderStyle CssClass="header_th"></HeaderStyle>
       <PagerStyle BackColor="#C6C3C6" ForeColor="Black" HorizontalAlign="Right" />
       <RowStyle BackColor="#DEDFDE" ForeColor="Black" CssClass="row_td" />
       <SelectedRowStyle BackColor="#9471DE" Font-Bold="True" ForeColor="White" />
       <SortedAscendingCellStyle BackColor="#F1F1F1" />
       <SortedAscendingHeaderStyle BackColor="#594B9C" />
       <SortedDescendingCellStyle BackColor="#CAC9C9" />
       <SortedDescendingHeaderStyle BackColor="#33276A" />
</asp:GridView> 
</div>
<div align="center">
<br />
 <asp:Label ID="lblresult" runat="server"></asp:Label>
</div>
 </div>

C# Coding : Bind Gridview through TextBox Values


using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Configuration;
using System.Data.SqlClient;

public partial class Pending : System.Web.UI.Page
{
    SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["ms"].ConnectionString);
    SqlDataAdapter da;
    DataSet ds = new DataSet();
    protected void Page_Load(object sender, EventArgs e)
    {
        
    }
    protected void txt_bookid_TextChanged(object sender, EventArgs e)
    {
        try
        {
            da = new SqlDataAdapter("select * from librarysampledata where studentid='" + txt_bookid.Text.Trim() + "'", con);
            con.Open();
            da.Fill(ds);
            if (ds.Tables[0].Rows.Count > 0)
            {
                Grd_Pending_Status.DataSource = ds;
                Grd_Pending_Status.DataBind();
            }
        }
        catch
        {
            con.Close();
        }
    }
    protected void Grd_Pending_Status_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        try
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                da = new SqlDataAdapter("select * from librarysampledata where studentid='" + txt_bookid.Text.Trim() + "'", con);
                //Find the DropDownList in the Row
                DropDownList ddl_status = (e.Row.FindControl("ddl_status") as DropDownList);
                //Select the Country of Customer in DropDownList                
                da.Fill(ds);
                if (ds.Tables[0].Rows.Count > 0)
                {
                    SqlDataAdapter da1 = new SqlDataAdapter("select statusid, statusname from statusdetails", con);
                    DataSet ds1 = new DataSet();
                    da1.Fill(ds1);
                    if (ds1.Tables[0].Rows.Count > 0)
                    {
                        ddl_status.DataSource = ds1;
                        ddl_status.DataTextField = "statusname";
                        ddl_status.DataBind();
                        string status = (e.Row.FindControl("lblstatus") as Label).Text;
                        ddl_status.Items.FindByValue(status).Selected = true;
                        //Add Default Item in the DropDownList
                        ddl_status.Items.Insert(0, new ListItem("Please select"));
                    }
                }
            }
        }
        catch
        {
            con.Close();
        }
    }    
}

C# Coding : DropdownList SelectedIndexChanged Event in Gridview


    protected void ddl_status_SelectedIndexChanged(object sender, EventArgs e)
    {
        try
        {
            DropDownList ddl_status = (DropDownList)sender;
            GridViewRow row = (GridViewRow)ddl_status.Parent.Parent;
            int idx = row.RowIndex;

            
            //Retrieve bookid and studentid from Gridview and status(dropdownlist)
            String lblbookid = ((Label)row.Cells[0].FindControl("lblbookid")).Text;
            String lblstudentid = ((Label)row.Cells[0].FindControl("lblstudentid")).Text;
            DropDownList ddl = (DropDownList)row.Cells[0].FindControl("ddl_status");

            
            //Update Status            
            string query = "Update librarysampledata set status='" + ddl.Text.ToString() + "' where bookid='" + lblbookid + "'";
            SqlCommand cmd = new SqlCommand(query, con);
            con.Open();
            cmd.ExecuteNonQuery();

            
            //cmd.ExecuteNonQuery();
            lblresult.Text = "Status is : " + ddl.Text.ToString() + " Updated Successfully";
        }
        catch
        {
            con.Close();

        }
    }

1 Komentar untuk "DropdownList SelectedIndexChanged in Gridview in Asp.net C#"

Back To Top