Monday, 17 September 2012

Bind DropDown Under GridView

Bind DropDown Under GridView
lets go from basic...



How to bind dropdown?

Normaly We Bind A Drop Downdown In Asp.net Page Like below....
.aspx

<asp:DropDownList ID="DropDownListGroup" runat="server"
DataTextField="GroupNm" DataValueField="GroupID" Height="18px"
style="margin-bottom: 0px" Width="205px">
</asp:DropDownList>

.cs
        SqlDataAdapter adp2 = new SqlDataAdapter("select * from Group", con);
        DataSet ds2 = new DataSet();
        adp2.Fill(ds2);
        DropDownListGroup.DataSource = ds2;
        DropDownListGroup.DataBind();      
        DropDownListGroup.Items.Insert(0, "---Select---");




but in case of Grid View Thing are bit different..
You need to workout in RowDataBound Click Event

1.Bind DropDown Under GridView In Item Template

 if (e.Row.RowType == DataControlRowType.DataRow)
     {

              DropDownList drdList = (DropDownList)e.Row.FindControl("DropDownListGroup");
             SqlDataAdapter adp = new SqlDataAdapter("select * from MST_Group", con);
             DataSet ds = new DataSet();
             adp.Fill(ds);
             drdList.DataSource = ds;
             drdList.DataBind();
             drdList.Items.Insert(0, "---Select---");

             }



2.Bind DropDown Under GridView In EditItem Template

 if (e.Row.RowType == DataControlRowType.DataRow)
     {
        
if ((e.Row.RowState & DataControlRowState.Edit) > 0)
         {

             DropDownList drdList = (DropDownList)e.Row.FindControl("DropDownListGroup");
             SqlDataAdapter adp = new SqlDataAdapter("select * from MST_Group",con);
             DataSet ds = new DataSet();
             adp.Fill(ds);
             drdList.DataSource = ds;
             drdList.DataBind();
             drdList.Items.Insert(0, "---Select---");
         }
     }




3.Bind DropDown Under GridView In Footer Template

  if (e.Row.RowType == DataControlRowType.Footer)
     {

       ((Label)e.Row.FindControl("lblQty")).Text = totqty.ToString();
       TxtTotal.Text = totqty.ToString();
     
     }

No comments:

Post a Comment

Print Only Grid View in ASP.net

ASP.net How to Print Only GridView < div id ="gridviewDiv" >   < asp:GridView ID ="gridViewToPri...