Problem to reveal value through DDL

Hi,
Using these
        private void BindDropDown(DropDownList par_ddl, string par_selVal)
        {
            SqlCommand cmd = new SqlCommand();
            cmd.CommandType = CommandType.Text;

            cmd.CommandText = "select distinct [rec_id],[Item_code] as [Item_code] from [Item] order by 2";

            par_ddl.DataSource = GetData(cmd);
            par_ddl.DataTextField = "Item_code";
            par_ddl.DataValueField = "rec_id";
            par_ddl.DataBind();

            //par_ddl.Items.Insert(0, new ListItem("Please select"));
            par_ddl.Items.Insert(0, new ListItem("Please select"));
            if (!string.IsNullOrEmpty(par_selVal))
            {
                ListItem li = par_ddl.Items.FindByValue(par_selVal);
                if (li != null)
                    li.Selected = true;
                else
                    par_ddl.SelectedIndex = 0;
                /*par_ddl.ClearSelection();
                par_ddl.Items.FindByValue(par_selVal).Selected = true;*/
            }
            else
                par_ddl.SelectedIndex = 0;
        }
        protected void ddl_DataBound(object sender, EventArgs e)
        {
            DropDownList list = sender as DropDownList;
            foreach (ListItem ddlItem in list.Items)
                ddlItem.Text = HttpUtility.HtmlDecode(ddlItem.Text);
        }

Open in new window


Before I click "Edit" below, it is fine to have these
https://dl.dropboxusercontent.com/u/40211031/tt106.png

I don't know why the relevant values are not revealed fine to Column 1 by the DDL, after I've clicked "Edit" to 1st row, like
https://dl.dropboxusercontent.com/u/40211031/tt104.png
LVL 12
HuaMin ChenProblem resolverAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Lokesh B RDeveloperCommented:
Hi,

You need to set the DropDown List Selected Value in RowDataBound event of GridView.
0
HuaMin ChenProblem resolverAuthor Commented:
Thanks.
Yes, I have these
        protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            //ds = GetDataSet();
            string selVal;
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                if ((e.Row.RowState & DataControlRowState.Edit) > 0)
                {
                    DropDownList ddl = (DropDownList)e.Row.FindControl("ddl2");
                    if (ddl == null)
                    {
                        return;
                    }
                    else
                    {
                        if (e.Row.RowIndex >= 0)
                            selVal = GridView1.DataKeys[e.Row.RowIndex].Value.ToString();
                        else
                            selVal = null;

                        this.BindDropDown(ddl, selVal);
                    }

                    Label lb_rt = (Label)e.Row.FindControl("curr_rate");
                    ddl.Enabled = true;
                }
                else
                {
                    DropDownList ddl = (DropDownList)e.Row.FindControl("ddl");
                    if (ddl == null)
                    {
                        return;
                    }
                    else
                    {
                        if (e.Row.RowIndex >= 0)
                            selVal = GridView1.DataKeys[e.Row.RowIndex].Value.ToString();
                        else
                            selVal = null;
                        this.BindDropDown(ddl, selVal);
                    }
                    Label lb_rt = (Label)e.Row.FindControl("curr_rate");

                    ddl.Enabled = false;
                }
            }

            if (e.Row.RowType == DataControlRowType.Footer)
            {
                DropDownList ddl = (DropDownList)e.Row.FindControl("ddl3");
                if (ddl == null)
                {
                    return;
                }
                else
                {
                    /*if (e.Row.RowIndex >= 0)
                        selVal = GridView1.DataKeys[e.Row.RowIndex].Value.ToString();
                    else*/
                        selVal = null;
                    this.BindDropDown(ddl, selVal);
                }
                Label lb_rt = (Label)e.Row.FindControl("curr_rate");

                ddl.Enabled = false;
            }
        }

Open in new window

0
Lokesh B RDeveloperCommented:
Hi,

here is the sample GridView based on your requirement.

protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                PopulateData();
            }
        }

        private void PopulateData()
        {
            DataTable table = new DataTable();
            DataColumn idColumn = table.Columns.Add("ID", typeof(System.Int32));
            table.Columns.Add("Item_Code", typeof(System.String));
            table.Columns.Add("Item_Rate", typeof(System.Decimal));
            table.PrimaryKey = new DataColumn[] { idColumn };

            table.Rows.Add(new object[] { 1, "HKD", 0.1000 });
            table.Rows.Add(new object[] { 2, "RMD", 0.4500 });
            table.Rows.Add(new object[] { 3, "USD", 0.4500 });
            table.Rows.Add(new object[] { 4, "HK", 0.125 });
            table.AcceptChanges();

            GridView1.DataSource = table;
            GridView1.DataBind();
        }

        protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            string selVal;
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                if ((e.Row.RowState & DataControlRowState.Edit) > 0)
                {
                    DropDownList ddl = (DropDownList)e.Row.FindControl("ddl2");
                    if (ddl == null)
                    {
                        return;
                    }
                    else
                    {
                        if (e.Row.RowIndex >= 0)
                            selVal = GridView1.DataKeys[e.Row.RowIndex].Value.ToString();
                        else
                            selVal = null;

                        this.BindDropDown(ddl, selVal);
                    }

                    Label lb_rt = (Label)e.Row.FindControl("curr_rate");
                    ddl.Enabled = true;
                }
                else
                {
                    DropDownList ddl = (DropDownList)e.Row.FindControl("ddl");
                    if (ddl == null)
                    {
                        return;
                    }
                    else
                    {
                        if (e.Row.RowIndex >= 0)
                            selVal = GridView1.DataKeys[e.Row.RowIndex].Value.ToString();
                        else
                            selVal = null;
                        this.BindDropDown(ddl, selVal);
                    }
                    Label lb_rt = (Label)e.Row.FindControl("curr_rate");

                    ddl.Enabled = false;
                }
            }

            if (e.Row.RowType == DataControlRowType.Footer)
            {
                DropDownList ddl = (DropDownList)e.Row.FindControl("ddl3");
                if (ddl == null)
                {
                    return;
                }
                else
                {
                    /*if (e.Row.RowIndex >= 0)
                        selVal = GridView1.DataKeys[e.Row.RowIndex].Value.ToString();
                    else*/
                    selVal = null;
                    this.BindDropDown(ddl, selVal);
                }
                Label lb_rt = (Label)e.Row.FindControl("curr_rate");

                ddl.Enabled = false;
            }
        }

        private void BindDropDown(DropDownList par_ddl, string par_selVal)
        {
            DataTable table = new DataTable();
            DataColumn idColumn = table.Columns.Add("ID", typeof(System.Int32));
            table.Columns.Add("Item_Code", typeof(System.String));

            table.PrimaryKey = new DataColumn[] { idColumn };

            table.Rows.Add(new object[] { 1, "HKD" });
            table.Rows.Add(new object[] { 2, "RMD" });
            table.Rows.Add(new object[] { 3, "USD" });
            table.Rows.Add(new object[] { 4, "HKB" });
            table.AcceptChanges();

            par_ddl.DataValueField = "ID";
            par_ddl.DataTextField = "Item_Code";
            par_ddl.DataSource = table;
            par_ddl.DataBind();

            par_ddl.Items.Insert(0, new ListItem("Please select"));

            if (!string.IsNullOrEmpty(par_selVal))
            {
                ListItem li = par_ddl.Items.FindByText(par_selVal);
                if (li != null)
                {
                    li.Selected = true;
                }
                else
                {
                    par_ddl.SelectedIndex = 0;
                }
            }
            else
            {
                par_ddl.SelectedIndex = 0;
            }
        }

        protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {

        }

        protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
        {

        }

        protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {

        }

        protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
        {
            GridView1.EditIndex = e.NewEditIndex;
            this.PopulateData();
        }

        protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
        {

        }

Open in new window


GridView ASPX

<asp:GridView ID="GridView1" runat="server" Width="550px"
                AutoGenerateColumns="false" DataKeyNames="Item_code" Font-Names="Arial"
                Font-Size="11pt" AlternatingRowStyle-BackColor="#C2D69B"
                HeaderStyle-BackColor="green" AllowPaging="true"
                ShowFooter="true" OnPageIndexChanging="GridView1_PageIndexChanging"
                OnRowCancelingEdit="GridView1_RowCancelingEdit" OnRowEditing="GridView1_RowEditing"
                OnRowUpdating="GridView1_RowUpdating" PageSize="10" OnRowDeleting="GridView1_RowDeleting" OnRowDataBound="GridView1_RowDataBound">
                <Columns>
                    <asp:BoundField DataField="Item_code" HeaderText="Item code" />
                    <asp:TemplateField HeaderText="Item">
                        <ItemTemplate>
                            <asp:DropDownList ID="ddl" AutoPostBack="true" runat="server">
                            </asp:DropDownList>
                        </ItemTemplate>
                        <EditItemTemplate>
                            <asp:DropDownList ID="ddl2"  AutoPostBack="true" runat="server">
                            </asp:DropDownList>
                        </EditItemTemplate>
                        <FooterTemplate>
                            <asp:DropDownList ID="ddl3" AutoPostBack="true" runat="server">
                            </asp:DropDownList>
                        </FooterTemplate>
                    </asp:TemplateField>
                    <asp:TemplateField Visible="false">
                        <ItemTemplate>
                            <asp:TextBox ID="tb_Item_code" runat="server" Text='<%# Eval("Item_code") %>' Visible="false" />
                        </ItemTemplate>
                        <EditItemTemplate>
                            <asp:TextBox ID="tb_Item_cd" runat="server" Text='<%# Eval("Item_code") %>' Visible="false" />
                        </EditItemTemplate>
                        <FooterTemplate>
                            <asp:TextBox ID="tb_Item_cd2" runat="server" Text='<%# Eval("Item_code") %>' Visible="false" />
                        </FooterTemplate>
                    </asp:TemplateField>
                    <asp:TemplateField ItemStyle-Width="150px" HeaderText="Act Rate">
                        <ItemTemplate>
                            <asp:Label ID="Item_rate" runat="server"
                                Text='<%# Eval("Item_rate")%>'>
                            </asp:Label>
                        </ItemTemplate>
                        <EditItemTemplate>
                            <asp:Label ID="Item_rate" runat="server"
                                Text='<%# Eval("Item_rate")%>'>
                            </asp:Label>
                        </EditItemTemplate>
                        <FooterTemplate>
                            <asp:Label ID="Item_rate2" runat="server"
                                Text='<%# Eval("Item_rate")%>'>
                            </asp:Label>
                        </FooterTemplate>
                    </asp:TemplateField>
                    <asp:TemplateField HeaderText="Delete?">
                        <ItemTemplate>
                            <span onclick="return confirm('Are you sure to delete the record?')">
                                <asp:LinkButton CssClass="forbutton" ID="lnkdelete" runat="server" Text="Delete" ForeColor="Red" CommandName="Delete" />
                            </span>
                        </ItemTemplate>
                        <FooterTemplate>
                            <asp:Button CssClass="forbutton" ID="btnadd" runat="server" Text="Add"  />
                        </FooterTemplate>
                    </asp:TemplateField>
                    <asp:CommandField ShowEditButton="true" />
                </Columns>
                <AlternatingRowStyle BackColor="White" />
                <EditRowStyle BackColor="#efefef" />
                <FooterStyle BackColor="#507CD1" Font-Bold="true" ForeColor="White" />
                <HeaderStyle BackColor="#507CD1" Font-Bold="true" ForeColor="White" />
                <PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" />
                <RowStyle BackColor="#EFF3FB" />
                <SelectedRowStyle BackColor="#D1DDF1" Font-Bold="true" ForeColor="#333333" />
                <SortedAscendingCellStyle BackColor="#F5F7FB" />
                <SortedAscendingHeaderStyle BackColor="#6D95E1" />
                <SortedDescendingCellStyle BackColor="#E9EBEF" />
                <SortedDescendingHeaderStyle BackColor="#4870BE" />
            </asp:GridView>

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
.NET Programming

From novice to tech pro — start learning today.