您现在的位置: 爱51代码网 >> 范文 >> 文章正文
gridview如何返回droplistdown数据给数据库

gridview如何返回droplistdown数据给数据库

后台代码

public partial class Default : System.Web.UI.Page
{
    public string j;
    SqlConnection sqlcon;
    SqlCommand sqlcom;
    string strCon = "Data Source=.\\SQLEXPRESS;AttachDbFilename=E:\\业务\\App_Data\\yw.mdf;Integrated Security=True;User Instance=True";
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            bind();
        }
    }

    protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
    {
        GridView1.EditIndex = e.NewEditIndex;
        bind();
    }

     //删除数据
    protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        string sqlstr = "delete from yw where id='" + GridView1.DataKeys[e.RowIndex].Value.ToString() + "'";
        sqlcon = new SqlConnection(strCon);
        sqlcom = new SqlCommand(sqlstr, sqlcon);
        sqlcon.Open();
        sqlcom.ExecuteNonQuery();
        sqlcon.Close();
        bind();
    }

    //更新数据
    protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
        sqlcon = new SqlConnection(strCon);
        string sqlstr = "update yw set yw='"
            + ((TextBox)(GridView1.Rows[e.RowIndex].Cells[3].Controls[0])).Text.ToString().Trim() + "' where id='"
            + GridView1.DataKeys[e.RowIndex].Value.ToString() + "'";
        sqlcom = new SqlCommand(sqlstr, sqlcon);
        sqlcon.Open();
        sqlcom.ExecuteNonQuery();
        sqlcon.Close();
        GridView1.EditIndex = -1;
        bind();
    }

    //取消数据
    protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
    {
        GridView1.EditIndex = -1;
        bind();
    }

    //绑定数据
    public void bind()
    {
        //修改下面两行代码,注意where后面要有空格
        string i;
        i = DropDownList1.SelectedItem.Text;
        if (int.Parse(DropDownList1.SelectedValue) == 0)
            j = "yw= 'aa' or yw= 'bb'";
        if (int.Parse(DropDownList1.SelectedValue) == 1)
            j = "yw='" + i + "'";
        if (int.Parse(DropDownList1.SelectedValue) == 2)
            j = "yw='" + i + "'";
        string sqlstr = "select * from yw where " + j;
        sqlcon = new SqlConnection(strCon);
        SqlDataAdapter myda = new SqlDataAdapter(sqlstr, sqlcon);
        DataSet myds = new DataSet();
        sqlcon.Open();
        myda.Fill(myds, "yw");
        GridView1.DataSource = myds;
        GridView1.DataKeyNames = new string[] { "id" };//主键
        GridView1.DataBind();
        sqlcon.Close();
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        bind();
    }
}
前台代码
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" CellPadding="4"
     ForeColor="#333333" GridLines="None" OnRowEditing="GridView1_RowEditing" OnRowDeleting="GridView1_RowDeleting"
     OnRowUpdating="GridView1_RowUpdating" OnRowCancelingEdit="GridView1_RowCancelingEdit" >
        <FooterStyle BackColor="#990000" Font-Bold="True" ForeColor="White" />
        <Columns >
            <asp:BoundField DataField="id" HeaderText="用户ID" ReadOnly="True" />
            <asp:BoundField DataField="name" HeaderText="用户姓名" ReadOnly="true" />
            <asp:BoundField DataField="tel" HeaderText="联系方式" ReadOnly="true" />
            <asp:BoundField DataField="yw" HeaderText="业务情况" />
            <asp:BoundField DataField="qb" HeaderText="业务情况" Visible="False" />
            <asp:CommandField HeaderText="编辑" ShowEditButton="True" />
            <asp:CommandField HeaderText="删除" ShowDeleteButton="True" />
        </Columns >
        <RowStyle ForeColor="#000066" />
        <SelectedRowStyle BackColor="#669999" Font-Bold="True" ForeColor="White" />
        <PagerStyle BackColor="White" ForeColor="#000066" HorizontalAlign="Left" />
        <HeaderStyle BackColor="#006699" Font-Bold="True" ForeColor="White" />
    </asp:GridView >
   
        <asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="true">
            <asp:ListItem Value="0">y</asp:ListItem>
            <asp:ListItem Value="1">aa</asp:ListItem>
            <asp:ListItem Value="2">bb</asp:ListItem>
        </asp:DropDownList>
   
        <asp:Button ID="Button1" runat="server" Text="查询" onclick="Button1_Click" />
我该如何更改,使下面的输入框更改为下拉列表,并能将其数据返回到数据库

 <asp:TemplateField HeaderText="业务情况">
                <ItemTemplate>
                    <%# Eval("业务情况")%>
                </ItemTemplate>
                <EditItemTemplate>
                    <asp:HiddenField ID="HDFSex" runat="server" Value='<%# Eval("业务情况") %>' />
                   <asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="true">
            <asp:ListItem Value="0">y</asp:ListItem>
            <asp:ListItem Value="1">aa</asp:ListItem>
            <asp:ListItem Value="2">bb</asp:ListItem>
        </asp:DropDownList>

                </EditItemTemplate>
                <ItemStyle Width="100px" />
            </asp:TemplateField>

更新你就在这里面写语句啊:
 protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
获取选择的值
string value = ((DropDownList)GridView1.Rows[e.RowIndex].FindControl("DropDownList1")).SelectedValue;

  • 上一篇文章:

  • 下一篇文章: 没有了
  • 最新文章 热点文章 相关文章
    lotus数据列表文档个数如何实时统
    lotus代理中LS如何将字符串保存到
    在lotus BS系统里怎样方便实现统
    undefined reference timer_crea
    linux文件/usr/lib破坏了,还原后
    linux上运行system函数时,print
    Failed to open eth0
    android手机无法与eclipse或电脑
    C/C++洗牌算法源代码
    servlet技术实现用户名唯一的验证
    undefined reference timer_crea
    Failed to open eth0
    ZOJ 3700 Ever Dream 文章中单词
    TortoiseGit和msysGit安装及使用
    sharepoint 2010 获取用户信息Us
    mysql主从同步延迟方案解决的学习
    生日旅行总结
    中小板生日快乐随感
    送生日快乐桑葚乳酪小蛋糕
    写给女儿的生日快乐
    未能加载文件活程序集“Acce
    DataGridView绑定IList<T>实
    vb.net关于control.tag的问题
     



    设为首页 | 加入收藏 | 网站地图 | 友情链接 |