locked
Selecting data from gridview using dropdownlist. RRS feed

  • Question

  • User-1683638147 posted

    Hi,

    Im currently working on a delivery system for my school project. That's a dropdownlist inside my gridview , my gridview consist of deliveryID, orderID, address, and the status(dropdownlist).
    so whenever I select "Received order",  an email will be send to the customer informing them that we received the order.
    The email consist of their order id and a chunk of message.

    The order id is inside the second column of my gridview, may I know how do I get that value ?

     protected void ddlStatus_SelectedIndexChanged(object sender, EventArgs e)
            {
                
                string deliveryId = GridView1.DataKeys[0].Value.ToString();
                string selectedValue = ((DropDownList)sender).SelectedValue;
            
                if (selectedValue == "Delivering in process") // i click this then send email
                {
                    GridViewRow row = GridView1.SelectedRow;
                    BLLDeliveryEmail del2 = new BLLDeliveryEmail();
                    int result2 = 0;
                    string Email1 = "testing@hotmail.com";
                    string subject1 = "Your delivery order is on the way!";
                    string body1 = "test";
                    string customername1 = "Test";
    
                    GridViewRow row = GridView1.SelectedRow;
                    int OrderID = int.Parse(row.Cells[1].Text); <<<<NULL REFERENCE ERROR>>>>
                  
                    result2 = del2.DeliveryEmail2(Email1, subject1, body1, customername1, OrderID);
                }
    
            }
    
    
               
               

    I've tried using row.Cells[1], but its giving a null reference error. 

    And the same problem goes to the deliveryId

    string deliveryId = GridView1.DataKeys[0].Value.ToString();

    This only give me the value from the first column and first row only. I cant retrieve the deliveryId value according to which row the dropdownlist is .

    Thank you!

    Sunday, July 3, 2016 4:37 AM

Answers

  • User61956409 posted

    Hi vodaaaaaa,

    You could refer to the following sample code.

    <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false" DataKeyNames="deliveryId,orderId">
    protected void ddlStatus_SelectedIndexChanged(object sender, EventArgs e)
    {
        int rowindex = ((GridViewRow)((DropDownList)sender).NamingContainer).RowIndex;
    
        string deliveryId = GridView1.DataKeys[rowindex].Values[0].ToString();
        string orderId = GridView1.DataKeys[rowindex].Values[1].ToString();
    
        //or
        //string orderId = GridView1.Rows[rowindex].Cells[1].Text;
                
    }
    

    Best Regards,

    Fei Han



    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, July 4, 2016 6:47 AM

All replies

  • User2103319870 posted

    The order id is inside the second column of my gridview, may I know how do I get that value ?

    You need to find the row based on the dropdownlist control which triggered the seleccted index changed event like given below

     protected void ddlStatus_SelectedIndexChanged(object sender, EventArgs e)
            {
                
                //Get the dropdownlist which triggerd the selecctedindexchanged event
                DropDownList ddl = (DropDownList)sender;
                //Get the row based on the dropdownlist
                GridViewRow row = (GridViewRow)ddl.NamingContainer;
                //string deliveryId = GridView1.DataKeys[0].Value.ToString();
                string selectedValue = ((DropDownList)sender).SelectedValue;
            
                if (selectedValue == "Delivering in process") // i click this then send email
                {
                    //GridViewRow row = GridView1.SelectedRow;
                    BLLDeliveryEmail del2 = new BLLDeliveryEmail();
                    int result2 = 0;
                    string Email1 = "testing@hotmail.com";
                    string subject1 = "Your delivery order is on the way!";
                    string body1 = "test";
                    string customername1 = "Test";
    
                    //GridViewRow row = GridView1.SelectedRow;
                    int OrderID = int.Parse(row.Cells[1].Text); //<<<<NULL REFERENCE ERROR>>>>
                  
                    result2 = del2.DeliveryEmail2(Email1, subject1, body1, customername1, OrderID);
                }
    
            }

    Sunday, July 3, 2016 4:02 PM
  • User61956409 posted

    Hi vodaaaaaa,

    You could refer to the following sample code.

    <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false" DataKeyNames="deliveryId,orderId">
    protected void ddlStatus_SelectedIndexChanged(object sender, EventArgs e)
    {
        int rowindex = ((GridViewRow)((DropDownList)sender).NamingContainer).RowIndex;
    
        string deliveryId = GridView1.DataKeys[rowindex].Values[0].ToString();
        string orderId = GridView1.DataKeys[rowindex].Values[1].ToString();
    
        //or
        //string orderId = GridView1.Rows[rowindex].Cells[1].Text;
                
    }
    

    Best Regards,

    Fei Han



    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, July 4, 2016 6:47 AM
  • User-1683638147 posted

    thank you!!!

    Tuesday, July 5, 2016 12:38 PM