Click here to Skip to main content
15,885,546 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
i have a drop down list which have three entry.when i select every entry of dropdownlist then the data show into the gridview...my code behind page is


C#
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
using System.Net.Mail;
using System.IO;
using System.ComponentModel;
using System.Drawing;
using System.Web.SessionState;

namespace AccountManagement
{
    public partial class Account : System.Web.UI.Page
    {
        SqlConnection con;
       SqlDataAdapter da;
        SqlDataReader dr;
        //DataSet ds1;
       // String Sql;
        //int Count;
        DataTable dt;
        SqlCommand cmd;
        protected void Page_Load(object sender, EventArgs e)
        {
            string s = ConfigurationManager.ConnectionStrings["conn"].ConnectionString;
            con = new SqlConnection(s);
            //if (!IsPostBack)
            //{
            //    bindgrid();
            //    bindlst();
            //}
            con.Open();
        }

        //protected void Button1_Click(object sender, EventArgs e)
        //{
        //    bindgrid();
        //    GridView1.Visible = true;
            
        //}
        public void bindgrid()
        {
            cmd=new SqlCommand ("select distinct Fname,Lname from Client_registration_tbl",con);
            //ds1 = new DataSet();
            con.Open();
            dr = cmd.ExecuteReader();
            //da.Fill(ds1);
            GridView1.DataSource = dr;
            GridView1.DataBind();
            GridView1.Visible = true;
            con.Close();

        }

        public void bindlst()
        {
            da = new SqlDataAdapter("select distinct Fname,Lname from Client_registration_tbl", con);
            dt = new DataTable();
            da.Fill(dt);
            DropDownList1.DataSource = dt;
            DropDownList1.DataBind();
        }
        protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
        {

            da = new SqlDataAdapter("select distinct Fname,Lname from Client_registration_tbl" + DropDownList1.SelectedItem.Text + "", con);
            dt = new DataTable();
            da.Fill(dt);
            GridView1.DataSource = dt;
            GridView1.DataBind();
           
            bindgrid();
            bindlst();
            //GridView1.Visible = true;
        }

      


    }
}

thanksss in advance
Posted
Updated 13-Sep-12 22:20pm
v2
Comments
cagdas0606 14-Sep-12 4:42am    
and check your !postback situation, actually it should not run on formar way, you can use this and improve it day by day, by yourself

protected void Page_Load(object sender, EventArgs e)
{
if (Session["name"] == null)
{
Response.Redirect("Login.aspx");
}
else
{

if (Page.IsPostBack)
{
gridwhateverelse.RowCommand +=gridwhateverelse_RowCommand;
return;
}
}
}

1 solution

you can do this via RowDataBound event, take a look at the RowDataBound events, do a research.i can explain something for you: click on your gridview and then click to the properties(which is formally on the right bottom of your project page), after click the thunder image(events) and find RowCommand,rowDeleting,rowdatabound fields and learn how these fields trigger your project. You can also search this: HiddenField operations on asp.net,c#

protected void gridView_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
DropDownList ddl = (DropDownList)e.Row.FindControl("DropDownListPermission");
ddl.DataSource = new DB().FillDataTable(" select bla bla "),surname) as name ORDER BY 2");
ddl.DataBind();
HiddenField authorized = (HiddenField)e.Row.FindControl("HiddenFieldauthorized");
ddl.SelectedValue = authorized.Value;
}
}

as well as this code is your answer
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900