none
Dropdownlist in gridview RRS feed

  • Question

  • hi all,here am posting my code.please solve my problem.problem was...when i trying to add new row in gridview(add button)the items in the dropdownlist was disappering...y it was happening?
    public partial class New_GeneralComplaints : System.Web.UI.Page
    { 
      BalCallerTypeMaster bctm = new BalCallerTypeMaster();
      BalComplaintMaster bcom = new BalComplaintMaster();
      BalOriginMaster bom = new BalOriginMaster();
      BalDestinationMaster bdsm = new BalDestinationMaster();
      BalPriorityMaster bpm = new BalPriorityMaster();
      BalStatus bs = new BalStatus();
      BalArticle ba = new BalArticle();
      DropDownList dpl1, dpl2, dpl3, dpl4, dpl5;
      protected void Page_Load(object sender, EventArgs e)
      {
        if (!Page.IsPostBack)
        {
          SetInitialRow();
    
          
       
        }
       
        /* // DropDownList dpl1 = (DropDownList)Gridview1.FindControl("Origin_Station");
        DropDownList dpl2 = (DropDownList)Gridview1.FindControl("Delivery_Station");
        DropDownList dpl3 = (DropDownList)Gridview1.FindControl("Article_Type");*/
       
        if (!IsPostBack)
        {
        
    
         
          
          DataSet ds1 = bctm.getCallerTypeMaster();
    
    
          Type_Caller.DataSource = ds1.Tables[0];
          Type_Caller.DataTextField = ds1.Tables[0].Columns["Caller_Type_Desc"].ColumnName.ToString();
          Type_Caller.DataValueField = ds1.Tables[0].Columns["Caller_Type_ID"].ColumnName.ToString();
          Type_Caller.DataBind();
          Type_Caller.Items.Add("Select Caller Type");
          Type_Caller.Items.FindByText("Select Caller Type").Selected = true;
    
    
    
          //Response.Write(Caller_Type_ID);
          
          DataSet ds3 = bcom.getComplaintMaster();
    
          Type_Complaint.DataSource = ds3.Tables[0];
          Type_Complaint.DataValueField = ds3.Tables[0].Columns["Complaint_Type_ID"].ColumnName.ToString();
          Type_Complaint.DataTextField = ds3.Tables[0].Columns["Complaint_Desc"].ColumnName.ToString();
          Type_Complaint.DataBind();
          Type_Complaint.Items.Add("Select Category");
          Type_Complaint.Items.FindByText("Select Category").Selected = true;
    
          
          
          Complaint_Date_Time.Text = DateTime.Now.ToString();
        }
      }
      
      BalComplaints bc = new BalComplaints();
      
      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(int)));
       dt.Columns.Add(new DataColumn("Column3", typeof(string)));
       dt.Columns.Add(new DataColumn("Column4", typeof(string)));
        dt.Columns.Add(new DataColumn("Column5", typeof(string)));
        dt.Columns.Add(new DataColumn("Column6", typeof(string)));
        dt.Columns.Add(new DataColumn("Column7", typeof(string)));
        dt.Columns.Add(new DataColumn("Column8", typeof(string)));
        dt.Columns.Add(new DataColumn("Column9", typeof(string)));
        dt.Columns.Add(new DataColumn("Column10", typeof(string)));
    
    
        dr = dt.NewRow();
        dr["RowNumber"] = 1;
        dr["Column1"] = string.Empty;
        dr["Column2"] = int.MinValue;
        dr["Column3"] = string.Empty;
       dr["Column4"] = string.Empty;
        dr["Column5"] = string.Empty;
        dr["Column6"] = string.Empty;
        dr["Column7"] = string.Empty;
        dr["Column8"] = string.Empty;
        dr["Column9"] = string.Empty;
        dr["Column10"] = string.Empty;
        dt.Rows.Add(dr);
        //dr = dt.NewRow();
    
        //Store the DataTable in ViewState
    
        ViewState["CurrentTable"] = dt;
        Gridview1.DataSource = dt;
        Gridview1.DataBind();
     foreach (GridViewRow grdRow in Gridview1.Rows)
          {
    
       
              dpl1 = (DropDownList)(Gridview1.Rows[grdRow.RowIndex].Cells[1].FindControl("Origin_Station"));
    
              DataSet ds4 = bom.getOrigin();
    
              dpl1.DataSource = ds4.Tables[0];
              dpl1.DataValueField = ds4.Tables[0].Columns["Origin_ID"].ColumnName.ToString();
              dpl1.DataTextField = ds4.Tables[0].Columns["Origin_Desc"].ColumnName.ToString();
              dpl1.DataBind();
              dpl1.Items.Add("Select Origin");
              dpl1.Items.FindByText("Select Origin").Selected = true;
             
    
              dpl2 = (DropDownList)(Gridview1.Rows[grdRow.RowIndex].Cells[1].FindControl("Delivery_Station"));
              DataSet ds5 = bdsm.getDestination();
              dpl2.DataSource = ds5.Tables[0];
              dpl2.DataValueField = ds5.Tables[0].Columns["Destination_ID"].ColumnName.ToString();
              dpl2.DataTextField = ds5.Tables[0].Columns["Destination_Desc"].ColumnName.ToString();
              dpl2.DataBind();
              dpl2.Items.Add("Select Destination");
              dpl2.Items.FindByText("Select Destination").Selected = true;
    
              dpl5 = (DropDownList)(Gridview1.Rows[grdRow.RowIndex].Cells[1].FindControl("Article_Type"));
              DataSet ds6 = ba.getArticleMaster();
              dpl5.DataSource = ds6.Tables[0];
              dpl5.DataValueField = ds6.Tables[0].Columns["Article_ID"].ColumnName.ToString();
              dpl5.DataTextField = ds6.Tables[0].Columns["Article_Desc"].ColumnName.ToString();
             dpl5.DataBind();
              dpl5.Items.Add("Select Article");
              dpl5.Items.FindByText("Select Article").Selected = true;
    
              dpl3 = (DropDownList)(Gridview1.Rows[grdRow.RowIndex].Cells[1].FindControl("Status"));
              DataSet ds = bs.getStatus();
    
              dpl3.DataSource = ds.Tables[0];
              dpl3.DataValueField = ds.Tables[0].Columns["Status_ID"].ColumnName.ToString();
              dpl3.DataTextField = ds.Tables[0].Columns["Status_Desc"].ColumnName.ToString();
             dpl3.DataBind();
              dpl3.Items.Add("Select Status");
              dpl3.Items.FindByText("Select Status").Selected = true;
    
              dpl4 = (DropDownList)(Gridview1.Rows[grdRow.RowIndex].Cells[1].FindControl("Priority"));
              DataSet ds2 = bpm.getPriorityMaster();
    
              dpl4.DataSource = ds2.Tables[0];
              dpl4.DataValueField = ds2.Tables[0].Columns["Priority_ID"].ColumnName.ToString();
              dpl4.DataTextField = ds2.Tables[0].Columns["Priority_Desc"].ColumnName.ToString();
            dpl4.DataBind();
              dpl4.Items.Add("Select Priority");
              dpl4.Items.FindByText("Select Priority").Selected = true;
    }
        
      }
    
    
      
      protected void btn_sub_Click1(object sender, EventArgs e)
      {
        AddNewRowToGrid();
        
     
        
        bc.insertrefund();
        Response.Write("Record added");
     
      }
      protected void btn_res_Click(object sender, EventArgs e)
      {
        ResetFormControlValues(this);
    
      }
      private void ResetFormControlValues(Control parent)
      {
        foreach (Control c in parent.Controls)
        {
          if (c.Controls.Count > 0)
          {
            ResetFormControlValues(c);
          }
          else
          {
            switch (c.GetType().ToString())
            {
              case "System.Web.UI.WebControls.TextBox":
                ((TextBox)c).Text = "";
                break;
    
    
    
            }
          }
        }
      }
      
    public 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("LR");
              TextBox box2 = (TextBox)Gridview1.Rows[rowIndex].Cells[2].FindControl("Number_of_Articles");
             
              TextBox box3 = (TextBox)Gridview1.Rows[rowIndex].Cells[7].FindControl("Consignor");
             
              TextBox box4 = (TextBox)Gridview1.Rows[rowIndex].Cells[9].FindControl("Article_booked_Date");
              TextBox box5 = (TextBox)Gridview1.Rows[rowIndex].Cells[10].FindControl("Desc_Complaint");
              DropDownList dpl1 = (DropDownList)Gridview1.Rows[rowIndex].Cells[3].FindControl("Origin_Station");
              DropDownList dpl2 = (DropDownList)Gridview1.Rows[rowIndex].Cells[4].FindControl("Delivery_Station");
              DropDownList dpl3 = (DropDownList)Gridview1.Rows[rowIndex].Cells[5].FindControl("Status");
              DropDownList dpl4 = (DropDownList)Gridview1.Rows[rowIndex].Cells[6].FindControl("Priority");
              DropDownList dpl5 = (DropDownList)Gridview1.Rows[rowIndex].Cells[8].FindControl("Article_Type");
    
    
    
              drCurrentRow = dtCurrentTable.NewRow();
              drCurrentRow["RowNumber"] = i + 1;
              drCurrentRow["Column1"] = box1.Text;
              drCurrentRow["Column2"] = int.Parse(box2.Text).ToString();
              drCurrentRow["Column3"] = dpl1.Text;
              drCurrentRow["Column4"] = dpl2.Text;
              drCurrentRow["Column5"] = dpl3.Text;
              drCurrentRow["Column6"] = dpl4.Text;
              drCurrentRow["Column7"] = box3.Text;
              drCurrentRow["Column8"] = dpl5.Text;
              drCurrentRow["Column9"] = box4.Text;
              drCurrentRow["Column10"] = box5.Text;
    
              bc.Article_ID1 =int.Parse( dpl5.Text);
              bc.ArticleBDateTime1 = box4.Text;
              bc.CLDesc1 = box5.Text;
              bc.CLPriority_ID1 =int.Parse( dpl4.Text);
              bc.Status_ID1 = int.Parse( dpl3.Text);
              bc.Origin_ID1 = int.Parse( dpl1.Text);
              bc.Destination_ID1 = int.Parse( dpl2.Text);
              bc.LRNo1 = box1.Text;
              bc.No_of_Articles1 = int.Parse( box2.Text);
              bc.Reciever1 = box3.Text;
              bc.Sender1 = Consignee.Text;
              bc.Complaint_Logged_DT1 = Complaint_Date_Time.Text;
              bc.CLFullName1 = Caller_Name.Text;
              bc.CLPhone1 = Phone.Text;
              bc.CLEmail1 = Email.Text;
              bc.Complaint_Type_ID1 = int.Parse(Type_Complaint.Text);
              bc.CLUser_Logged1 = User_Logged.Text;
              bc.Caller_Type_ID1 = int.Parse(Type_Caller.Text);
              bc.insertrefund();
             // Response.Redirect("SendReport_Complaint.aspx?LRNo=" + box1.Text+ "&CLUser_Logged=" + User_Logged.Text);
              rowIndex++;
            }
            //add new row to DataTable
            dtCurrentTable.Rows.Add(drCurrentRow);
            //Store the current data to ViewState
            ViewState["CurrentTable"] = dtCurrentTable;
    
            //Rebind the Grid with the current data
            Gridview1.DataSource = dtCurrentTable;
            Gridview1.DataBind();
           // dpl1.DataSource = dtCurrentTable;
       // dpl1.DataBind();
           // dpl2.DataSource = ds5.Tables[0];
           // dpl2.DataBind();
            // dpl5.DataSource = ds6.Tables[0];
            // dpl5.DataBind();
           // dpl3.DataSource = ds.Tables[0];
            // dpl3.DataBind();
            // dpl4.DataSource = ds2.Tables[0];
           // dpl4.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 = 1; i < dt.Rows.Count; i++)
            {
              TextBox box1 = (TextBox)Gridview1.Rows[rowIndex].Cells[1].FindControl("LR");
              TextBox box2 = (TextBox)Gridview1.Rows[rowIndex].Cells[2].FindControl("Number_of_Articles");
    
              TextBox box3 = (TextBox)Gridview1.Rows[rowIndex].Cells[7].FindControl("Consignor");
    
              TextBox box4 = (TextBox)Gridview1.Rows[rowIndex].Cells[9].FindControl("Article_booked_Date");
              TextBox box5 = (TextBox)Gridview1.Rows[rowIndex].Cells[10].FindControl("Desc_Complaint");
              DropDownList dpl1 = (DropDownList)Gridview1.Rows[rowIndex].Cells[3].FindControl("Origin_Station");
              DropDownList dpl2 = (DropDownList)Gridview1.Rows[rowIndex].Cells[4].FindControl("Delivery_Station");
              DropDownList dpl3 = (DropDownList)Gridview1.Rows[rowIndex].Cells[5].FindControl("Status");
              DropDownList dpl4 = (DropDownList)Gridview1.Rows[rowIndex].Cells[6].FindControl("Priority");
              DropDownList dpl5 = (DropDownList)Gridview1.Rows[rowIndex].Cells[8].FindControl("Article_Type");
              
    
              
              box1.Text = dt.Rows[i]["Column1"].ToString();
              box2.Text = dt.Rows[i]["Column2"].ToString();
              dpl1.Text = dt.Rows[i]["Column3"].ToString();
              dpl2.Text = dt.Rows[i]["Column4"].ToString();
              dpl3.Text = dt.Rows[i]["Column5"].ToString();
              dpl4.Text = dt.Rows[i]["Column6"].ToString();
              box3.Text = dt.Rows[i]["Column7"].ToString();
              dpl5.Text = dt.Rows[i]["Column8"].ToString();
              box4.Text = dt.Rows[i]["Column9"].ToString();
              box5.Text = dt.Rows[i]["Column10"].ToString();
              
    
              rowIndex++;
    
    
            }
          }
        }
      }
    
    
    
    
      protected void ButtonAdd_Click1(object sender, EventArgs e)
      {
     
        AddNewRowToGrid();
      
      }
    }
    • Moved by edhickeyModerator Tuesday, September 7, 2010 2:59 PM (From:.NET 3.0/3.5 Windows Workflow Foundation)
    Saturday, September 4, 2010 12:13 PM

Answers

  • Hi,

    This forum is for WF3/3.5 only, you should post your question in ASP.NET forum: http://forums.asp.net

    Thanks
    This posting is provided "AS IS" with no warranties, and confers no rights. Microsoft Online Community Support. My Blog:http://xhinker.com
    Tuesday, September 7, 2010 7:20 AM