简体   繁体   中英

GridView: how to delete from database a value from the selected row and dropdownlist = value from database?

I'm working on a project but I need to do something and I Couldn't find a way to do it. I have:

1.GridView
Columns:
No|Name|Age|Choose|Where|
1. Joh  21  Yes/No 

Where Choose column control is a dropdownlist with 2 values : Yes/No I'm trying to make something like:

If dropdownlist.selected value = "Yes" 
{
Insert Dropdownlist Selected value in the database cell "Choose" , and it should insert the value in the current editing row in the database.
And Dropdownlist value to get the value from the database cell.
}
If dropdownlist value = "NO" then delete the value from the database row.

How can I do that? or is there another way ? I'm using sqldatasource to bind the informations from database in the gridview.

THank you

So, are you binding your dropdown like this inside grid ?

 <asp:DropDownList ID = "ddlchoose" runat="server" 
  SelectedValue='<%# Eval("Active") %>' >      
  <asp:ListItem Text="Yes" Value="1"></asp:ListItem> 
  <asp:ListItem Text="No" Value="0"></asp:ListItem> 
  </asp:DropDownList> 

If so, use this-- Set AutoPostBack="true" and OnSelectedIndexChanged="ddlchoose_SelectedIndexChange"

On aspx.cs

protected void ddlchoose_SelectedIndexChange(object sender, EventArgs e)
    {           
        DropDownList ddlchoose= (DropDownList)sender;

        // get reference to the row
        GridViewRow gvr = (GridViewRow)(((Control)sender).NamingContainer);

        //Get the reference of this ddlchoose
        DropDownList refddlchoose = (DropDownList)gvr.FindControl("ddlchoose");
        Control ddlchooseParent = refddlchoose.Parent;

        int roeid = Convert.ToInt32((gridview1.SelectedRow.FindControl("ID")).ToString());
        if(ddlchoose.SelectedValue=="1")
        {
         //Update method
        Update(roeid , ddlchoose.SelectedValue.Trim());
        }
        else if(ddlchoose.SelectedValue=="0")
        {
        //Delete method
        Delete(roeid);
        }
        gridview1.DataBind();
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM