Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C#ASP.NET
I have 3 dropdownlist in gridview. Dropdownlist selected indexchanged event,i have bank name,branch,account,corresponding branch
$acoount_depending
on the bank name i've already used dropdownlist on selecteditem. Text to get text and value to assign as value. i've also assigned value to another column but while I'm adding new row previously set selectedvalue is cleared. plse reply me..thanks
 
This my code:
  protected void btnAddBnkDtls_Click(object sender, EventArgs e)//rosna
        {            
            DataTable dt = blSupplierObj.BindBnkDtlsGrid(ref GrdvewBnkDetls);
            DataRow dr = dt.NewRow();
            
            dt.Rows.Add(dr);
            ViewState["dtBnk"] = dt;
            rowid = 0;
            GrdvewBnkDetls.DataSource = dt;
            GrdvewBnkDetls.DataBind();           
        }
 
        protected void GrdvewBnkDetls_RowDataBound(object sender, GridViewRowEventArgs e)
        {            Trace.Write("ADMIN.Master.frmPartySupplier_Customer.GrdvewBnkDetls_RowDataBound");
 
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                if (e.Row.RowIndex != -1)
                {
 
                        e.Row.Cells[0].Text = Convert.ToString((e.Row.RowIndex + 1));
                        DropDownList ddlBank = (DropDownList)e.Row.FindControl("ddlBnkNme");
                        ddlBank.DataSource = dtBankDetails;
                        ddlBank.DataTextField = "Name";
                        ddlBank.DataValueField = "Uoid";
                        ddlBank.DataBind();
                        DataTable dt = (DataTable)ViewState["dtBnk"];
                        if (dt != null)
                        {
                            if (rowid < dt.Rows.Count)
                            {
                                ddlBank.SelectedValue = dt.Rows[rowid]["BankName"].ToString();
                            }
                            if (ddlBank.SelectedValue != "0")
                            {
                                DropDownList ddlBranch = (DropDownList)e.Row.FindControl("ddlBrnch");
                                blSupplierObj.BindCombo(ref ddlBranch, "Uoid", "Name", "SELECT Uoid,Name fROM CFN_TB_BankBranch Where FK_BankID ='" + ddlBank.SelectedValue + "' ");
                                if (dt.Rows[rowid]["Branch"].ToString() != "0")
                                    ddlBranch.SelectedValue = dt.Rows[rowid]["Branch"].ToString();
                            }
                            DropDownList ddlAcount = (DropDownList)e.Row.FindControl("ddlAccnt");
                            ddlAcount.DataSource = dtAccount;
                            ddlAcount.DataTextField = "Name";
                            ddlAcount.DataValueField = "Uoid";
                            ddlAcount.DataBind();
           
                            string str = DataBinder.Eval(e.Row.DataItem, "Account").ToString().Trim();
                            if (str != "")
                            {
                                ddlAccount.SelectedValue = str;
                            }
                        }
                        else
                        {
                            DropDownList ddlAcount = (DropDownList)e.Row.FindControl("ddlAccnt");
                            ddlAcount.DataSource = dtAccount;
                            ddlAcount.DataTextField = "Name";
                            ddlAcount.DataValueField = "Uoid";
                            ddlAcount.DataBind();
                        }
                }
            }
        }
 
        protected void GrdvewBnkDetls_RowCommand(object sender, GridViewCommandEventArgs e)//rosna
        {
            Trace.Write("ADMIN.Master.frmPartySupplier_Customer.GrdvewBnkDetls_RowCommand");
            GridViewRow objGridViewRow = (GridViewRow)((ImageButton)e.CommandSource).Parent.Parent;
 
            if (e.CommandName == "onDelete")
            {
                DataTable dt = blSupplierObj.BindBnkDtlsGrid(ref GrdvewBnkDetls);
                if (dt.Rows.Count > 0)
                {
                    int id = int.Parse(objGridViewRow.Cells[0].Text);
                    BL_PartySupplier_Customer.Tablefilter(ref dt, "  RowID <> " + id.ToString());
                    rowid = 0;
                    GrdvewBnkDetls.DataSource = dt;
                    GrdvewBnkDetls.DataBind();
                    ViewState["dtBnk"] = dt;                    
                }
                else
                {
                }
            }
        }
public string GetBankDtils(ref GridView GrdvewBnkDetls, string buttonText, ENT_PartySupplier_Customer entbnkObj)
       {
           //...
           StringBuilder strQuery = new StringBuilder();
           string strQuerys = string.Empty;
           if (GrdvewBnkDetls.Rows.Count >= 1 &&
              (GrdvewBnkDetls.Rows[0].FindControl("ddlBnkNme") as DropDownList).SelectedValue != "0")
           {
               for (int i = 0; i < GrdvewBnkDetls.Rows.Count; i++)
               {
                   //--
                   strQuery.Append(" SELECT ");
                     strQuery.Append("'" + Guid.NewGuid() + "',");
                       strQuery.Append("'" + (GrdvewBnkDetls.Rows[i].Cells[1].FindControl("ddlBnkNme") as DropDownList).SelectedValue + "',");
                       strQuery.Append("'" + (GrdvewBnkDetls.Rows[i].Cells[2].FindControl("ddlBrnch") as DropDownList).SelectedValue + "',");
                       strQuery.Append("'" + entbnkObj.PartyUoid + "',");
                       strQuery.Append("'" + (GrdvewBnkDetls.Rows[i].Cells[3].FindControl("ddlAccnt") as DropDownList).SelectedValue + "',");
                       strQuery.Append("'" + Guid.NewGuid() + "',");
                       strQuery.Append("'" + clsCommon.convertDate(DateTime.Now.ToString("dd/MM/yyyy")) + "',");
                       strQuery.Append("0");
                       strQuery.Append(" UNION ALL ");
 
               }
               strQuerys = strQuery.ToString();
           }
 
           else
           {
               strQuerys = string.Empty;
           }
 
           if (strQuerys != string.Empty)
           {
               int x = strQuery.ToString().Length;
               strQuerys = strQuerys.Remove(strQuery.ToString().Length - 11, 11);
 
           }
 
           return strQuerys;
       }
Posted 7-Oct-12 22:39pm
rosren529
Edited 7-Oct-12 22:46pm
v2

1 solution

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

r u talking about ddlBank or ddlAccount or both..?
Please reply me..
 
And FYI when you assign a datasource or bind the ddl, a new set of data is populatd into the ddl. So your previously selectdvalue is gone. u can maintain ur selectd value in a hiddenfield or a viewstate so that you can restore it back after the databind() of that particular ddl.
Thanks
  Permalink  
Comments
rosren at 8-Oct-12 7:57am
   
both
rosren at 8-Oct-12 8:00am
   
I want code pleasee help me ..thnksss

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

  Print Answers RSS
Your Filters
Interested
Ignored
     
0 Sergey Alexandrovich Kryukov 422
1 sanket saxena 355
2 Abhinav S 258
3 OriginalGriff 250
4 CPallini 205
0 Sergey Alexandrovich Kryukov 8,704
1 OriginalGriff 5,000
2 Peter Leow 3,839
3 Maciej Los 3,535
4 Er. Puneet Goel 3,107


Advertise | Privacy | Mobile
Web04 | 2.8.140415.2 | Last Updated 8 Oct 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Use
Layout: fixed | fluid