Click here to Skip to main content
11,637,176 members (69,526 online)
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
0 OriginalGriff 295
1 jyo.net 295
2 stibee 288
3 himanshu agarwal 184
4 Sergey Alexandrovich Kryukov 184
0 OriginalGriff 9,031
1 Sergey Alexandrovich Kryukov 8,513
2 Mika Wendelius 6,922
3 F-ES Sitecore 2,388
4 Suvendu Shekhar Giri 2,320


Advertise | Privacy | Mobile
Web02 | 2.8.150728.1 | Last Updated 8 Oct 2012
Copyright © CodeProject, 1999-2015
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100