GridView:如何从数据库中删除所选行中的值和dropdownlist =数据库中的值?

时间:2012-03-22 13:24:53

标签: c# asp.net sql

我正在开展一个项目,但我需要做点什么而且找不到办法。 我有:

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来绑定gridview中数据库的信息。

感谢你

1 个答案:

答案 0 :(得分:0)

那么,你是否将这个下拉列表绑定在网格中?

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