none
Gridview selected index changed , RRS feed

  • Question

  • when i selecting row from gridview and data getting loading into textbox and dropdownlist,when data get load in dropdown list value getting double to display ....

        protected void GVCon_SelectedIndexChanged(object sender, EventArgs e)
        {
            cno.Text = GVCon.SelectedRow.Cells[1].Text;
            txtcontainer.Text = GVCon.SelectedRow.Cells[2].Text;
            txtWeight.Text = GVCon.SelectedRow.Cells[3].Text;
            txtbaletotal.Text = GVCon.SelectedRow.Cells[5].Text;
            //string SesnId = dm.GetData("select Cat_ID from dbo.ConCatagory where Cat_Name='" + GVCon.SelectedRow.Cells[6].Text.Trim() + "'").Rows[0][0].ToString();
            //DDLCAT.SelectedValue = SesnId;
    
            DDLCAT.SelectedItem.Text = GVCon.SelectedRow.Cells[6].Text;
    DDLCat.selecteditem.text = GVcon.selectedRow.cells[6].Text;

    here is screen shot...  Goodwill getting double display ....


    • Edited by Akhterhussain Friday, July 26, 2019 6:05 AM add screenshot
    Friday, July 26, 2019 6:02 AM

Answers

  • Hi Akhterhussain,

    Thanks for the feedback.

    I find the error may be that the text in dropdownlist is different from the text in Gridview.

    You could refer to the code I write.

    Html:

    <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="WebApplication1.WebForm1" %>
    
    <!DOCTYPE html>
    
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
        <title></title>
    </head>
    <body>
        <form id="form1" runat="server">
            <div>
                  <asp:GridView ID="GridView1" runat="server" Height="179px"  OnSelectedIndexChanged="GridView1_SelectedIndexChanged" Width="338px" AutoGenerateSelectButton="true" SelectedIndex="1">
                </asp:GridView>
            </div>
            <div>
                <asp:DropDownList ID="DropDownList1" runat="server"></asp:DropDownList>
    
    
            </div>
        </form>
    </body>
    </html>
    

    Code:

        public partial class WebForm1 : System.Web.UI.Page
        {
            protected void Page_Load(object sender, EventArgs e)
            {
                if (!IsPostBack)
                {
                    DataTable dt = new DataTable();
                    dt.Columns.Add("ID", typeof(int));
                    dt.Columns.Add("Weight", typeof(int));
                    dt.Columns.Add("Catagory", typeof(string));
                    dt.Rows.Add(78, 41655, "Goodwill");
                    dt.Rows.Add(79, 41455, "Private");
                    dt.Rows.Add(80, 41875, "Talize");
                    GridView1.DataSource = dt;
                    GridView1.DataBind();
                    DropDownList1.Items.Add("Goodwill");
                    DropDownList1.Items.Add("Private");
                    DropDownList1.Items.Add("Savers");
                    DropDownList1.Items.Add("Talize");
                }
            }
    
            protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
            {
                //Response.Write("test");
                DropDownList1.Text = GridView1.SelectedRow.Cells[3].Text;
                //DDLCat.text = GVcon.selectedRow.cells[6].Text;
            }
        }

    Result:

    Best Regards,

    Jack


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Marked as answer by Akhterhussain Saturday, July 27, 2019 5:18 AM
    Friday, July 26, 2019 7:28 AM
    Moderator

All replies

  • Greetings Akhterhussain.

    It looks like you are changing the text on the currently selected item. But what you want to do is select a different item.

    Try this.

    DDLCat.SelectedValue = GVcon.selectedRow.cells[6].Text;

    Friday, July 26, 2019 6:27 AM
  • Hi thanks for reply giving another error

    DDLCAT' has a SelectedValue which is invalid because it does not exist in the list of items.
    Parameter name: value

    Friday, July 26, 2019 6:48 AM
  • Hi Akhterhussain,

    Thank you for posting here.

    Based on my test, you could try the following code to solve the problem.

         DDLCat.text = GVcon.selectedRow.cells[6].Text;

    Best Regards,

    Jack


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Friday, July 26, 2019 7:11 AM
    Moderator
  • Hi Jack 

    thanks for reply...

    error is coming still...

    Friday, July 26, 2019 7:17 AM
  • Hi Akhterhussain,

    Thanks for the feedback.

    I find the error may be that the text in dropdownlist is different from the text in Gridview.

    You could refer to the code I write.

    Html:

    <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="WebApplication1.WebForm1" %>
    
    <!DOCTYPE html>
    
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
        <title></title>
    </head>
    <body>
        <form id="form1" runat="server">
            <div>
                  <asp:GridView ID="GridView1" runat="server" Height="179px"  OnSelectedIndexChanged="GridView1_SelectedIndexChanged" Width="338px" AutoGenerateSelectButton="true" SelectedIndex="1">
                </asp:GridView>
            </div>
            <div>
                <asp:DropDownList ID="DropDownList1" runat="server"></asp:DropDownList>
    
    
            </div>
        </form>
    </body>
    </html>
    

    Code:

        public partial class WebForm1 : System.Web.UI.Page
        {
            protected void Page_Load(object sender, EventArgs e)
            {
                if (!IsPostBack)
                {
                    DataTable dt = new DataTable();
                    dt.Columns.Add("ID", typeof(int));
                    dt.Columns.Add("Weight", typeof(int));
                    dt.Columns.Add("Catagory", typeof(string));
                    dt.Rows.Add(78, 41655, "Goodwill");
                    dt.Rows.Add(79, 41455, "Private");
                    dt.Rows.Add(80, 41875, "Talize");
                    GridView1.DataSource = dt;
                    GridView1.DataBind();
                    DropDownList1.Items.Add("Goodwill");
                    DropDownList1.Items.Add("Private");
                    DropDownList1.Items.Add("Savers");
                    DropDownList1.Items.Add("Talize");
                }
            }
    
            protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
            {
                //Response.Write("test");
                DropDownList1.Text = GridView1.SelectedRow.Cells[3].Text;
                //DDLCat.text = GVcon.selectedRow.cells[6].Text;
            }
        }

    Result:

    Best Regards,

    Jack


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Marked as answer by Akhterhussain Saturday, July 27, 2019 5:18 AM
    Friday, July 26, 2019 7:28 AM
    Moderator
  • Hi Jack..

    i calling dropdownlist from a table here is my query

     private void fillconcatagory()
        {
            con = new SqlConnection("Data Source=server;Integrated Security=SSPI;Initial Catalog=SilverProduction;MultipleActiveResultSets=True;");
    
            con.Open();
            SqlDataAdapter adpf = new SqlDataAdapter("select * from ConCatagory", con);
            DataSet dsf = new DataSet();
            adpf.Fill(dsf);
            DDLCAT.DataSource = dsf.Tables[0];
            DDLCAT.DataTextField = "Cat_Name";
            DDLCAT.DataValueField = "Cat_ID";
           DDLCAT.DataBind();
          //  DDLCAT.Items.Insert(0, new ListItem("Select Catagory", ""));
    
            con.Close();
            con.Dispose();
        }
    here  is my html

    <%@ Page Language="C#" AutoEventWireup="true" CodeFile="ConainterOpeningform.aspx.cs" Inherits="ConainterOpeningform" %>
    
    <!DOCTYPE html>
    
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
        <title></title>
        <style type="text/css">
            .auto-style1 {
                width: 100%;
            }
            .auto-style2 {
                color: #FFFFFF;
                text-align: right;
                background-color: #666699;
            }
            .auto-style3 {
                color: #FFFFFF;
                height: 98px;
                background-color: #666699;
            }
            .auto-style4 {
                color: #FFFFFF;
                height: 98px;
                background-color: #666699;
                width: 410px;
            }
            .auto-style5 {
                color: #FFFFFF;
                text-align: left;
                background-color: #666699;
                width: 410px;
            }
            .auto-style7 {
                color: #FFFFFF;
                text-align: right;
                background-color: #666699;
                width: 419px;
            }
            .auto-style8 {
                color: #FFFFFF;
                height: 98px;
                background-color: #666699;
                width: 419px;
            }
            .auto-style10 {
                color: #000000;
                text-align: right;
                background-color: #666699;
                width: 419px;
            }
        </style>
    </head>
    <body>
        <form id="form1" runat="server">
        <div>
        
            <table class="auto-style1">
                <tr>
                    <td class="auto-style8"></td>
                    <td class="auto-style4"><h1 style="width: 485px">Container Number Opening Form</h1></td>
                    <td class="auto-style3"></td>
                </tr>
                <tr>
                    <td class="auto-style7">Container ID :</td>
                    <td class="auto-style5">
    
                                      <asp:TextBox ID="cno" readonly="true" runat="server"></asp:TextBox>
    &nbsp; Container Catagory :
                                      <asp:DropDownList ID="DDLCAT" runat="server">
                                      </asp:DropDownList>
    </td>
                    <td class="auto-style2">&nbsp;</td>
                </tr>
                <tr>
                    <td class="auto-style7">Ref No :</td>
                    <td class="auto-style5">
                        <asp:TextBox ID="txtcontainer" runat="server" style="text-align: left"></asp:TextBox>
                    &nbsp;&nbsp;&nbsp; Container No :<asp:TextBox ID="txtcontrkno" runat="server"></asp:TextBox>
                    </td>
                    <td class="auto-style2">&nbsp;</td>
                </tr>
                <tr>
                    <td class="auto-style7">Total Weight</td>
                    <td class="auto-style5">
                        <asp:TextBox ID="txtWeight" runat="server"></asp:TextBox>
                    </td>
                    <td class="auto-style2">&nbsp;</td>
                </tr>
                <tr>
                    <td class="auto-style7">Total Bales</td>
                    <td class="auto-style5">
                        <asp:TextBox ID="txtbaletotal" runat="server"></asp:TextBox>
                    </td>
                    <td class="auto-style2">&nbsp;</td>
                </tr>
                <tr>
                    <td class="auto-style7">&nbsp;</td>
                    <td class="auto-style5">
                        <asp:Button ID="Add" runat="server" Text="Add" Width="54px" OnClick="Add_Click" />
                        <asp:Button ID="Update" runat="server" Text="Update" OnClick="Update_Click" />
                        <asp:Button ID="Delete" runat="server" Text="Delete" OnClick="Delete_Click" />
                    </td>
                    <td class="auto-style2">&nbsp;</td>
                </tr>
                <tr>
                    <td class="auto-style7">&nbsp;</td>
                    <td class="auto-style5">
                        &nbsp;</td>
                    <td class="auto-style2">&nbsp;</td>
                </tr>
                <tr>
                    <td class="auto-style10">Search by Ref_no : </td>
                    <td class="auto-style5">
                        <asp:TextBox ID="txtrefid" runat="server" OnTextChanged="txtrefid_TextChanged" AutoPostBack="True"></asp:TextBox>
                        <asp:LinkButton ID="lb_Cont" runat="server" OnClick="lb_Cont_Click">All Container Receiving Status</asp:LinkButton>
                    </td>
                    <td class="auto-style2">&nbsp;</td>
                </tr>
                <tr>
                    <td class="auto-style7">&nbsp;</td>
                    <td class="auto-style5">
                        <asp:GridView ID="GVCon" runat="server" AutoGenerateSelectButton="True" OnSelectedIndexChanged="GVCon_SelectedIndexChanged" CellPadding="4" ForeColor="#333333" GridLines="None">
                            <AlternatingRowStyle BackColor="White" />
                            <EditRowStyle BackColor="#2461BF" />
                            <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>
                    </td>
                    <td class="auto-style2">&nbsp;</td>
                </tr>
                <tr>
                    <td class="auto-style7">&nbsp;</td>
                    <td class="auto-style5">
                        &nbsp;</td>
                    <td class="auto-style2">&nbsp;</td>
                </tr>
                <tr>
                    <td class="auto-style7">&nbsp;</td>
                    <td class="auto-style5">
                        &nbsp;</td>
                    <td class="auto-style2">&nbsp;</td>
                </tr>
            </table>
        
        </div>
        </form>
    </body>
    </html>
    


    Friday, July 26, 2019 7:40 AM
  • Hi Akhterhussian,

    I think it is not related to your table, it is best for you to check that what is difference between my  code and your code then modified it.

    Whatever dropdownlist comes from, it will don't affect the result.

    Best Regards,

    Jack


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Friday, July 26, 2019 7:45 AM
    Moderator
  • same code i have in HTML as you have  but still error is coming as i used your code in C#

    'DDLCAT' has a SelectedValue which is invalid because it does not exist in the list of items.
    Parameter name: value

    Friday, July 26, 2019 7:51 AM
  • Hi Akhterhussain,

    Thanks for replying.

    If you want to solve the problem quickly, I suggest that you could upload a simple project via Onedrive.

    It will help us to reproduce your problem and solve your problem.

    Best Regards,

    Jack


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Friday, July 26, 2019 7:55 AM
    Moderator
  • Do you have anydesk?
    Friday, July 26, 2019 9:40 AM