Click here to Skip to main content
15,886,518 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi Friends.
My problem is i have three drop down list in gridview i,e Main category,subcategory,product.when i am selected main category item onely that releated items shows in subcategory list ,and products also shows subcategory related information....the following are the code for asp.net
C#
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using MySql.Data.MySqlClient;

public partial class Default16 : System.Web.UI.Page
{
    private void SetInitialRow()
    {
        DataTable dt = new DataTable();
        DataRow dr = null;
        dt.Columns.Add(new DataColumn("RowNumber", typeof(string)));
        dt.Columns.Add(new DataColumn("Column1", typeof(string)));
        dt.Columns.Add(new DataColumn("Column2", typeof(string)));
        dt.Columns.Add(new DataColumn("Column3", typeof(string)));
        dt.Columns.Add(new DataColumn("Column4", typeof(string)));
        dt.Columns.Add(new DataColumn("Column5", typeof(string)));
        dr = dt.NewRow();
        dr["RowNumber"] = 1;
        dr["Column1"] = string.Empty;
        dr["Column2"] = string.Empty;
        dr["Column3"] = string.Empty;
        dr["Column4"] = string.Empty;
        dr["Column5"] = string.Empty;
  

        dt.Rows.Add(dr);

        //Store the DataTable in ViewState
        ViewState["CurrentTable"] = dt;

        Gridview1.DataSource = dt;
        Gridview1.DataBind();
    }
    private void AddNewRowToGrid()
    {
        int rowIndex = 0;

        if (ViewState["CurrentTable"] != null)
        {
            DataTable dtCurrentTable = (DataTable)ViewState["CurrentTable"];
            DataRow drCurrentRow = null;
            if (dtCurrentTable.Rows.Count > 0)
            {
                for (int i = 1; i <= dtCurrentTable.Rows.Count; i++)
                {
                    //extract the TextBox values
                   // TextBox box1 = (TextBox)Gridview1.Rows[rowIndex].Cells[1].FindControl("TextBox1");
                    DropDownList DropDownList1 = (DropDownList)Gridview1.Rows[rowIndex].Cells[1].FindControl("DropDownList1");
                    DropDownList DropDownList2 = (DropDownList)Gridview1.Rows[rowIndex].Cells[2].FindControl("DropDownList2");
                    DropDownList DropDownList3 = (DropDownList)Gridview1.Rows[rowIndex].Cells[3].FindControl("DropDownList3");
                    TextBox box2 = (TextBox)Gridview1.Rows[rowIndex].Cells[4].FindControl("TextBox2");
                    TextBox box3 = (TextBox)Gridview1.Rows[rowIndex].Cells[5].FindControl("TextBox3");
                    
                    drCurrentRow = dtCurrentTable.NewRow();
                    drCurrentRow["RowNumber"] = i + 1;
                    dtCurrentTable.Rows[i - 1]["Column1"] = DropDownList1.SelectedItem.Text;
                    dtCurrentTable.Rows[i - 1]["Column2"] = DropDownList2.SelectedItem.Text;
                    dtCurrentTable.Rows[i - 1]["Column3"] = DropDownList3.SelectedItem.Text;
                
                    //dtCurrentTable.Rows[i - 1]["Column1"] = box1.Text;
                    dtCurrentTable.Rows[i - 1]["Column4"] = box2.Text;
                    dtCurrentTable.Rows[i - 1]["Column5"] = box3.Text;
                    


                    rowIndex++;
                }
                dtCurrentTable.Rows.Add(drCurrentRow);
                ViewState["CurrentTable"] = dtCurrentTable;

                Gridview1.DataSource = dtCurrentTable;
                Gridview1.DataBind();
            }
        }
        else
        {
            Response.Write("ViewState is null");
        }

        //Set Previous Data on Postbacks
        SetPreviousData();
    }
    private void SetPreviousData()
    {
        int rowIndex = 0;
        if (ViewState["CurrentTable"] != null)
        {
            DataTable dt = (DataTable)ViewState["CurrentTable"];
            if (dt.Rows.Count > 0)
            {
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    //TextBox box1 = (TextBox)Gridview1.Rows[rowIndex].Cells[1].FindControl("TextBox1");
                    DropDownList DropDownList1 = (DropDownList)Gridview1.Rows[rowIndex].Cells[1].FindControl("DropDownList1");
                   DropDownList DropDownList2 = (DropDownList)Gridview1.Rows[rowIndex].Cells[2].FindControl("DropDownList2");
                    DropDownList DropDownList3 = (DropDownList)Gridview1.Rows[rowIndex].Cells[3].FindControl("DropDownList3");
                    TextBox box2 = (TextBox)Gridview1.Rows[rowIndex].Cells[4].FindControl("TextBox2");
                    TextBox box3 = (TextBox)Gridview1.Rows[rowIndex].Cells[5].FindControl("TextBox3");

                    DropDownList1.SelectedItem.Text = dt.Rows[i]["Column1"].ToString();
                    DropDownList2.SelectedItem.Text = dt.Rows[i]["Column2"].ToString();
                    DropDownList3.SelectedItem.Text = dt.Rows[i]["Column3"].ToString();

                  //  box1.Text = dt.Rows[i]["Column1"].ToString();
                    box2.Text = dt.Rows[i]["Column4"].ToString();
                    box3.Text = dt.Rows[i]["Column5"].ToString();
                    

                    rowIndex++;
                }
            }
        }
    }
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            SetInitialRow();
            
        }
    }
    protected void ButtonAdd_Click(object sender, EventArgs e)
    {
        AddNewRowToGrid();
    }
    protected void ButtonInsert_Click(object sender, EventArgs e)
    {
        int rowIndex = 0;
        foreach (GridViewRow g1 in Gridview1.Rows)
        {
            DropDownList DropDownList3 = (DropDownList)Gridview1.Rows[rowIndex].Cells[3].FindControl("DropDownList3");
           string value= DropDownList3.SelectedValue.ToString();

           TextBox box2 = (TextBox)Gridview1.Rows[rowIndex].Cells[4].FindControl("TextBox2");
           TextBox box3 = (TextBox)Gridview1.Rows[rowIndex].Cells[5].FindControl("TextBox3");
           
            string  text1value= box2.Text.ToString();
            string text2val = box3.Text.ToString();
           

           DateTime StockUpDateedDate = DateTime.Now;
           string FormatedStockUpDateedDate = string.Format("{0:yyyy-MM-dd HH:mm:ss}", StockUpDateedDate);
           string ConnectionString = Convert.ToString(System.Configuration.ConfigurationManager.ConnectionStrings["Db_Connection"]);
           MySqlConnection connection = new MySqlConnection(ConnectionString);


           string querry = " insert into stock_detail (Product_Id,Stock_Status,Quantity,Damaged_Quantity,Stock_UpdateDate,Stock_UpdatedBy)values('" + value + "','0','" +text1value  + "','" + text2val  + "','0','0' )";
            connection.Open();
            MySqlCommand cmd = new MySqlCommand(querry, connection);
            cmd.ExecuteNonQuery();
            connection.Close();

        }
        rowIndex++;
    }
   
   
    protected void DropDownList1_OnSelectedIndexChanged(object sender, EventArgs e)
    {
        DropDownList ddl = sender as DropDownList;
        string MyValue = ddl.SelectedValue.ToString();
        foreach (GridViewRow row in Gridview1.Rows)
        {
            //Finding Dropdown control  
            Control ctr2 = row.FindControl("DropDownList2") as DropDownList;

            if (ctr2 != null)
            {
                string ConnectionString = Convert.ToString(System.Configuration.ConfigurationManager.ConnectionStrings["Db_Connection"]);
                MySqlConnection con = new MySqlConnection(ConnectionString);
                string querry = "select SubCategoryId ,SubCategoryName from ravishankar_ivrs_subcategory";
                MySqlCommand cmd = new MySqlCommand(querry, con);
                con.Open();

                MySqlDataAdapter ad = new MySqlDataAdapter(cmd);
                con.Close();

                DataSet dt = new DataSet();

                ad.Fill(dt);
               // DropDownList ddl1 = (DropDownList)ctr2;

            }
        }
    }
    protected void Gridview1_RowDataBound(object sender, GridViewRowEventArgs e)
    {

        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            Control ctrl = e.Row.FindControl("DropDownList1");
            Control ctrl1 = e.Row.FindControl("DropDownList2");
            Control ctrl2 = e.Row.FindControl("DropDownList3");

            if (ctrl != null)
            {
                DropDownList dd = ctrl as DropDownList;
                dd.DataTextField = "CategoryName";
                dd.DataValueField = "CategoryId";
                dd.DataSource = getdataset();
                dd.DataBind();


            }
            if (ctrl1 != null)
            {
                DropDownList dd1 = ctrl1 as DropDownList;

                dd1.DataTextField = "SubCategoryName";
                dd1.DataValueField = "SubCategoryId";
                dd1.DataSource = subcategory();
                dd1.DataBind(); 
 
            }
            if (ctrl2 != null)
            {
                DropDownList dd12 = ctrl2 as DropDownList;

                dd12.DataTextField = "ProductName";
                dd12.DataValueField = "ProductId";
                dd12.DataSource = product();
                dd12.DataBind();


            }
        }


    }

    private DataSet getdataset()
    {
        string ConnectionString = Convert.ToString(System.Configuration.ConfigurationManager.ConnectionStrings["Db_Connection"]);
        MySqlConnection con = new MySqlConnection(ConnectionString);
        string querry = "select CategoryId ,CategoryName from ravishankar_ivrs_interface_category";
        MySqlCommand cmd = new MySqlCommand(querry, con);
        con.Open();

        MySqlDataAdapter ad = new MySqlDataAdapter(cmd);
        con.Close();

        DataSet dt = new DataSet();

        ad.Fill(dt);

        return dt;
    }
    private DataSet subcategory()
    {
        string ConnectionString = Convert.ToString(System.Configuration.ConfigurationManager.ConnectionStrings["Db_Connection"]);
        MySqlConnection con = new MySqlConnection(ConnectionString);
        string querry = "select SubCategoryId ,SubCategoryName from ravishankar_ivrs_subcategory";
        MySqlCommand cmd = new MySqlCommand(querry, con);
        con.Open();

        MySqlDataAdapter ad = new MySqlDataAdapter(cmd);
        con.Close();

        DataSet dt = new DataSet();

        ad.Fill(dt);

        return dt;

    }
    private DataSet product()
    {
        string ConnectionString = Convert.ToString(System.Configuration.ConfigurationManager.ConnectionStrings["Db_Connection"]);
        MySqlConnection con = new MySqlConnection(ConnectionString);
        string querry = "select ProductId ,ProductName from tbl_product_details";
        MySqlCommand cmd = new MySqlCommand(querry, con);
        con.Open();

        MySqlDataAdapter ad = new MySqlDataAdapter(cmd);
        con.Close();

        DataSet dt = new DataSet();

        ad.Fill(dt);

        return dt;

    }
    protected void Gridview1_SelectedIndexChanged(object sender, EventArgs e)
    {

    }
}
Posted
Updated 11-Oct-12 20:37pm
v2
Comments
I.explore.code 12-Oct-12 2:46am    
So where is the question?

for the sub SubCategory u have to pass CategoryID as argument and pass this args to your sqlString

C#
private DataSet subcategory(string Cat)
    {
        string ConnectionString = Convert.ToString(System.Configuration.ConfigurationManager.ConnectionStrings["Db_Connection"]);
        MySqlConnection con = new MySqlConnection(ConnectionString);
        string querry = "select SubCategoryId ,SubCategoryName from ravishankar_ivrs_subcategory where CategoryId='"+Cat+"'" +;
        MySqlCommand cmd = new MySqlCommand(querry, con);
        con.Open();
 
        MySqlDataAdapter ad = new MySqlDataAdapter(cmd);
        con.Close();
 
        DataSet dt = new DataSet();


 
        ad.Fill(dt);
 
        return dt;
 
    }



and the same ForProduct Realated To SubCat.


You can Checkout This Also.
http://forums.asp.net/t/1333162.aspx/1[^]

http://www.aspnettutorials.com/tutorials/controls/dropdownlist-gridview-csharp.aspx[^]

http://stackoverflow.com/questions/2422140/making-gridview-respond-to-2-drop-down-lists[^]
 
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