我要在datagrid里加上(DropDownList模板列,在(DropDownList发生选择时,更改数据库?请教asp.net和c#怎么写?

解决方案 »

  1.   

    http://community.csdn.net/Expert/topic/3506/3506615.xml?temp=.3441889
      

  2.   

    <%@ Page language="c#" Codebehind="DataGrid.aspx.cs" AutoEventWireup="false" Inherits="ReadWord.DataGrid" %>
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
    <HTML>
    <HEAD>
    <title>DataGrid</title>
    <meta name="GENERATOR" Content="Microsoft Visual Studio .NET 7.1">
    <meta name="CODE_LANGUAGE" Content="C#">
    <meta name="vs_defaultClientScript" content="JavaScript">
    <meta name="vs_targetSchema" content="http://schemas.microsoft.com/intellisense/ie5">
    </HEAD>
    <body MS_POSITIONING="GridLayout">
    <form id="Form1" method="post" runat="server">
    <asp:DataGrid Runat="server" id="DataGrid1" ShowFooter="True">
    <Columns>
    <asp:TemplateColumn>
    <FooterTemplate>
    <asp:DropDownList Runat="server" ID="myDropDownList" AutoPostBack="True">
    <asp:ListItem Value="1">test1</asp:ListItem>
    <asp:ListItem Value="2">test2</asp:ListItem>
    </asp:DropDownList>
    </FooterTemplate>
    </asp:TemplateColumn>
    </Columns>
    </asp:DataGrid>
    </form>
    </body>
    </HTML>
      

  3.   

    刚做过
    #region 更新数据
    public void update1(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
    { string t1=((DropDownList)e.Item.FindControl("yourDropDownListname")).SelectedValue;
    conn.Open();
    cmd = new SqlCommand(sql,conn);
    cmd.ExecuteNonQuery();
    conn.Close();

    DataGrid1.EditItemIndex = -1;
    list();
    }
    #endregion
      

  4.   

    我用下面得方法
    System.Web.UI.WebControls.DropDownList myList;
    myList=(DropDownList)e.Item.Cells[0].Controls[0].FindControl("myDropDownList");
    myList.SelectedIndexChanged +=new EventHandler(myList_SelectedIndexChanged);
    }
    提示:未将对象引用设置到对象的实例。这是什么问题?
      

  5.   

    可以做到。。
    首先你在你的datagrid放了个dropdownlist对吧。现在我就只写dropdownlist的代码就好。恩
    <asp:DropDownList ID="ddl" Runat="server" OnSelectedIndexChanged="ddl_SelectedIndexChanged" AutoPostBack="True">
    <asp:ListItem Value="0" text="男"></asp:ListItem>
    <asp:ListItem Value="1" text="女"></asp:ListItem>
    </asp:DropDownList>
    注意。这里设置了AutoPostBack属性为True.还定义了dropdownlist的SelectedINdexChanged事件。恩。你会说。这个是潜逃在datagrid里的。那怎么找到dropdownlist在后台代码的事件呢?接着往下:在后台。你定义个方法用来供dropdownlist的selectedindexchanged事件用的。。    Protected Sub ddl_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs)
            Response.Write("<script>alert('你选择了 " & CType(sender, DropDownList).SelectedItem & "';</script>") 
        End Sub这样就可以了。记住要用Protected关键字。恩!
      

  6.   

    这不是和获取DataGrid 中的一个模板列里的
    CheckBox的Checked属性类似吗?