簡體   English   中英

GridView:如何從數據庫中刪除所選行中的值,下拉列表=數據庫中的值?

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

我正在做一個項目,但是我需要做一些事情,但找不到解決方法。 我有:

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

“選擇列”控件是一個具有2個值的下拉列表:是/否我正在嘗試執行以下操作:

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.

我怎樣才能做到這一點? 還是有另一種方法? 我正在使用sqldatasource綁定來自網格視圖中數據庫的信息。

謝謝

那么,您是否在網格內部像這樣綁定下拉菜單?

 <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> 

如果是這樣,請使用它-設置AutoPostBack="true" and OnSelectedIndexChanged="ddlchoose_SelectedIndexChange"

在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();
    }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM