Click here to Skip to main content
15,867,568 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have Gridview DropDownList, which has DataTextField and DataValueField.

Dropdownlist---> CMBITem

When I Select the Item, The Rate will be display on Gridview TextBox. But everytime the I select the Item the Dropdown list is refresh, Already Selected Item is not display.

protected void CMBItem_SelectedIndexChanged(object sender, EventArgs e)
   {
       DropDownList lb = (DropDownList)sender;
       GridViewRow gvRow = (GridViewRow)lb.NamingContainer;
     int rowID = gvRow.RowIndex;
       if (ViewState["CurrentTable"] != null)
       {
           DataTable dt = (DataTable)ViewState["CurrentTable"];

           connect.cn();
           SqlCommand sqlcmd = new SqlCommand("Get_Item_PurchasePrice", connect.cn());
           sqlcmd.Parameters.AddWithValue("@IMSNO", lb.SelectedValue);
          sqlcmd.CommandType = CommandType.StoredProcedure;
          SqlDataAdapter da = new SqlDataAdapter(sqlcmd);
           DataTable dt1 = new DataTable();
           da.Fill(dt1);
           if (dt1.Rows.Count > 0)
           {
               dt.Rows[rowID][4] = dt1.Rows[0][0].ToString();
               dt.Rows[rowID][5] = dt1.Rows[0][1].ToString();

         }
           ViewState["CurrentTable"] = dt;
           //Re bind the GridView for the updated data
           Gridview2.DataSource = dt;
           Gridview2.DataBind();
       }
     SetPreviousData();
   }


 private void SetPreviousData()
   {

       int rowIndex = 0;

       if (ViewState["CurrentTable"] != null)
       {

           DataTable dt = (DataTable)ViewState["CurrentTable"];

           if (dt.Rows.Count > 0)
           {
               AddGridcombo();
               LoadItemList();

               for (int i = 0; i < dt.Rows.Count; i++)
               {
                  DropDownList box4 = (DropDownList)Gridview2.Rows[rowIndex].Cells[1].FindControl("CMBGodown");
                  DropDownList box7 = (DropDownList)Gridview2.Rows[rowIndex].Cells[1].FindControl("CMBItem");

                   TextBox box1 = (TextBox)Gridview2.Rows[rowIndex].Cells[2].FindControl("txtqty");
                   TextBox box2 = (TextBox)Gridview2.Rows[rowIndex].Cells[3].FindControl("txtrate");
                   TextBox box3 = (TextBox)Gridview2.Rows[rowIndex].Cells[4].FindControl("txttaxpercent");
                   TextBox box5 = (TextBox)Gridview2.Rows[rowIndex].Cells[5].FindControl("txttaxvalue");
                   TextBox box6 = (TextBox)Gridview2.Rows[rowIndex].Cells[6].FindControl("txtamt");
                   box4.Text = dt.Rows[i]["Column1"].ToString();
                   box1.Text = dt.Rows[i]["Column3"].ToString();
                   box2.Text = dt.Rows[i]["Column4"].ToString();
                   box3.Text = dt.Rows[i]["Column5"].ToString();
                   box5.Text = dt.Rows[i]["Column6"].ToString();
                   box6.Text = dt.Rows[i]["Column7"].ToString();
                   box7.Text = dt.Rows[i]["Column2"].ToString();

                   rowIndex++;


              }

             // dtCurrentTable.Rows.Add(drCurrentRow);
             ViewState["CurrentTable"] = dt;
            //  Gridview2.DataSource = dt;
              //Gridview2.DataBind();
             // AddGridcombo();

           }
       }

private void LoadItemList()
   {
       try
       {
            connect.cnopen();
       foreach (GridViewRow grdRow in Gridview2.Rows)
       {
          DropDownList drdlist = (DropDownList)(Gridview2.Rows[grdRow.RowIndex].Cells[2].FindControl("CMBItem"));

        //   BeginTransaction("LoadItemList");
          // SqlCommand sqlcmd = new SqlCommand("FETCH_PURCHASE_DTL_SP",connect.cn());
           SqlCommand sqlcmd = new SqlCommand("FETCH_ITEM_LIST_SP", connect.cn());
                sqlcmd.CommandType=CommandType.StoredProcedure;
         SqlDataAdapter da=new SqlDataAdapter(sqlcmd);
           DataTable dt = new DataTable();
           da.Fill(dt);
               drdlist.DataSource = dt;
              drdlist.DataValueField = "IM_SERIALNO";
               drdlist.DataTextField = "IM_ITEMNAME";
           drdlist.DataBind();

       }
       }
       catch { }
   }
Posted
Updated 14-May-12 22:06pm
v2

Sounds like you are relaoding the grid on every postback which in turn reloads the dropdownlist.

Did you use VS DEBUGGER and saw the execution? I am sure once you do it, you will bind that the dropdown data is getting rebind.

Make sure, Grid data bind on every postback is avoided using Page.IsPostBack. Then make sure that the dropdown is not re-bind either.
 
Share this answer
 
It seems that the whole page renders when you choose an Item from it,

Please confirm that you r Dropdownlist's AutoPostBack = false.

And please bind the datasource for these DropDownlists in the Page_laod (!IsPostBack){……}
 
Share this answer
 
v2
Comments
devausha 15-May-12 5:27am    
I am Creating GridView Row dynamically when I click Addnewrow button the new row added. So Everytime the combobox bind

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