求解决办法(利用GridView修改数据)

时间:2023-02-09 23:30:24
前几天用GridView和sqldatasource可以简单实现修改数据但是我现在使用gridview的模版在嵌如入一个gridview不能使用sqldatasource请问有办法进行修改?修改 的是潜入的gridview中的数据

11 个解决方案

#1


学习

#2


protected void GridView_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        //if (e.Row.RowType == DataControlRowType.DataRow)
        //{
        //    GridView oGridView = (GridView)e.Row.FindControl("DetailGridView");

        //    if (oGridView != null)
        //    {
        //        SqlCommand cmd = new SqlCommand("select zcpmc,zgg from Report where zcpdm= " + MasterGridView.DataKeys[e.Row.RowIndex].Value, MyConnection);
        //        SqlDataReader dr1 = cmd.ExecuteReader();
        //        oGridView.DataSource = dr1;
        //        oGridView.DataBind();
        //        dr1.Close();
        //        cmd.Dispose();
        //    }
        //}

       //上面的方法我调试了不行

       //改成如下:页面弄个空SqlDataSource 在这绑定数据       
       if (e.Row.RowType == DataControlRowType.DataRow)
        {
            GridView oGridView = (GridView)e.Row.FindControl("DetailGridView");//获取嵌套的GridView
            SqlDataSource sqldata = (SqlDataSource)e.Row.FindControl("SqlDataSource1");
            sqldata.ConnectionString = sqlcon;
            sqldata.SelectCommand = "select top 5 zdw,zxs1,zdw1,zxs2,zdw2 from Report where zcpdm like 'A%' ";
            oGridView.DataBind();
        }

    }

#3


原方法老提示GirdView不包含datasource属性 但是可以.出来DataSource

#4


前几天用GridView和sqldatasource可以简单实现修改数据但是我现在使用gridview的模版在嵌如入一个gridview不能使用sqldatasource请问有办法进行修改?修改 的是潜入的gridview中的数据
=======================
可以

#5


注意大小写

#6


没有时间做Demo了,简单说一下

       if (e.Row.RowType == DataControlRowType.DataRow)
        {
            GridView oGridView = (GridView)e.Row.FindControl("DetailGridView");//获取嵌套的GridView
            SqlDataSource sqldata = (SqlDataSource)e.Row.FindControl("SqlDataSource1");
            sqldata.ConnectionString = sqlcon;
            sqldata.SelectCommand = "select top 5 zdw,zxs1,zdw1,zxs2,zdw2 from Report where zcpdm like 'A%' ";
            oGridView.DataBind();
        }
你应该在内部的GridView里再放一个SqlDataSource,并且设置好它的SelectCommand,但它可以有一个参数暂时设置为none
然后在RowDataBound事件中
应该大致改成这样


       if (e.Row.RowType == DataControlRowType.DataRow)
        {
                SqlDataSource sqldata = e.Item.FindControl("SqlDataSource2") as SqlDataSource;
                if (sqldata != null)
                {
                    sqldata.SelectParameters["CategoryID"].DefaultValue = GridView2.DataKeys[ e.Row.RowIndex].Value.ToString();
                }
        }

#7


amandag的方法值得参考,我试一下好使马上给分

#8


其实现在我用objectdatsource就很简单的解决了

#9


up

#10


引用 6 楼 amandag 的回复:
没有时间做Demo了,简单说一下 

              if   (e.Row.RowType   ==   DataControlRowType.DataRow) 
                { 
                        GridView   oGridView   =   (GridView)e.Row.FindControl( "DetailGridView ");//获取嵌套的GridView 
                        SqlDataSource   sqldata   =   (SqlDataSource)e.Row.FindControl( "SqlDataSource1 "); 
                        sqldata.Connection…

#11


来自:http://www.cnblogs.com/dushouke/archive/2008/03/24/1120377.html 
前台代码: 

C# code<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default5.aspx.cs" Inherits="Default5" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>无标题页</title>
</head>
<body>
    <form id="form1" runat="server">
    <div align="center">
    
        <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" 
            CellPadding="4" ForeColor="#333333" GridLines="None" Height="285px" 
            onrowcancelingedit="GridView1_RowCancelingEdit" 
            onrowdeleting="GridView1_RowDeleting" onrowediting="GridView1_RowEditing" 
            onrowupdating="GridView1_RowUpdating" Width="771px">
            <FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
            <RowStyle BackColor="#F7F6F3" ForeColor="#333333" />
            <Columns>
                <asp:BoundField DataField="id" HeaderText="学号" />
                <asp:BoundField DataField="name" HeaderText="姓名" />
                <asp:BoundField DataField="sex" HeaderText="性别" />
                <asp:BoundField DataField="age" HeaderText="年龄" />
                <asp:BoundField DataField="department" HeaderText="专业" />
                <asp:BoundField DataField="grade" HeaderText="班级" />
                <asp:CommandField HeaderText="编辑 " ShowEditButton="True" />
                <asp:CommandField HeaderText="选择" ShowSelectButton="True" />
                <asp:TemplateField HeaderText="删除 " ShowHeader="False">
                    <ItemTemplate>
                        <asp:LinkButton ID="LinkButton1" runat="server" CausesValidation="False" 
                            CommandName="Delete" Text="删除" OnClientClick="return confirm('你确定要删除吗?')"></asp:LinkButton>
                    </ItemTemplate>
                </asp:TemplateField>
            </Columns>
            <PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" />
            <SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" />
            <HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
            <EditRowStyle BackColor="#999999" />
            <AlternatingRowStyle BackColor="White" ForeColor="#284775" />
        </asp:GridView>
    
    </div>
    </form>
</body>
</html>



后台代码: 

C# codeusing System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Data.SqlClient;

public partial class Default5 : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            Bind();
        }
    }

    private void Bind()
    {
        SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["Personal"].ConnectionString);
        SqlDataAdapter adq = new SqlDataAdapter("select * from information", conn);
        DataSet dataset=new DataSet();
        adq.Fill(dataset,"information");
        GridView1.DataSource=dataset;
        GridView1.DataKeyNames=new String[]{"id"};
        GridView1.DataBind();
    }
    protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
    {
        GridView1.EditIndex = e.NewEditIndex;
        Bind();
    }
    protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["Personal"].ConnectionString);
        SqlCommand comm = new SqlCommand("delete from information where id='" + GridView1.DataKeys[e.RowIndex].Value.ToString() + "'",conn);
        conn.Open();
        try
        {
            int i = Convert.ToInt32(comm.ExecuteNonQuery());
            if (i > 0)
            {
                Response.Write("<script language=javascript>alert('删除成功!')</script>");
            }
            else
            {
                Response.Write("<script language=javascript>alert('删除失败!')</script>");
            }
            Bind();
        }
        catch (Exception erro)
        {
            Response.Write("错误信息:"+erro.Message);
        }
        finally
        {
            conn.Close();
        }
    }
    protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
        string id = ((TextBox)GridView1.Rows[e.RowIndex].Cells[0].Controls[0]).Text.ToString().Trim();
        string name = ((TextBox)GridView1.Rows[e.RowIndex].Cells[1].Controls[0]).Text.ToString().Trim();
        string sex = ((TextBox)GridView1.Rows[e.RowIndex].Cells[2].Controls[0]).Text.ToString().Trim();
        string age= ((TextBox)GridView1.Rows[e.RowIndex].Cells[3].Controls[0]).Text.ToString().Trim();
        string department = ((TextBox)GridView1.Rows[e.RowIndex].Cells[4].Controls[0]).Text.ToString().Trim();
        string grade = ((TextBox)GridView1.Rows[e.RowIndex].Cells[5].Controls[0]).Text.ToString().Trim();
        SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["Personal"].ConnectionString);
        SqlCommand comm = new SqlCommand("update information set id='"+id+"', name='"+name+"' , sex='"+sex+"' , age='"+age+"' , department='"+department+"' , grade='"+grade+"' where id='"+GridView1.DataKeys[e.RowIndex].Value.ToString()+"'", conn);
        conn.Open();
        try
        {
            int i = Convert.ToInt32(comm.ExecuteNonQuery());
            if (i > 0)
            {
                Response.Write("<script language=javascript>alert('保存成功!')</script>");
            }
            else
            {
                Response.Write("<script language=javascript>alert('保存失败!')</script>");
            }
            GridView1.EditIndex = -1;
            Bind();
        }
        catch (Exception erro)
        {
            Response.Write("错误信息:" + erro.Message);
        }
        finally
        {
            conn.Close();
        }
    }
    protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
    {
        GridView1.EditIndex = -1;
        Bind();
    }
}


#1


学习

#2


protected void GridView_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        //if (e.Row.RowType == DataControlRowType.DataRow)
        //{
        //    GridView oGridView = (GridView)e.Row.FindControl("DetailGridView");

        //    if (oGridView != null)
        //    {
        //        SqlCommand cmd = new SqlCommand("select zcpmc,zgg from Report where zcpdm= " + MasterGridView.DataKeys[e.Row.RowIndex].Value, MyConnection);
        //        SqlDataReader dr1 = cmd.ExecuteReader();
        //        oGridView.DataSource = dr1;
        //        oGridView.DataBind();
        //        dr1.Close();
        //        cmd.Dispose();
        //    }
        //}

       //上面的方法我调试了不行

       //改成如下:页面弄个空SqlDataSource 在这绑定数据       
       if (e.Row.RowType == DataControlRowType.DataRow)
        {
            GridView oGridView = (GridView)e.Row.FindControl("DetailGridView");//获取嵌套的GridView
            SqlDataSource sqldata = (SqlDataSource)e.Row.FindControl("SqlDataSource1");
            sqldata.ConnectionString = sqlcon;
            sqldata.SelectCommand = "select top 5 zdw,zxs1,zdw1,zxs2,zdw2 from Report where zcpdm like 'A%' ";
            oGridView.DataBind();
        }

    }

#3


原方法老提示GirdView不包含datasource属性 但是可以.出来DataSource

#4


前几天用GridView和sqldatasource可以简单实现修改数据但是我现在使用gridview的模版在嵌如入一个gridview不能使用sqldatasource请问有办法进行修改?修改 的是潜入的gridview中的数据
=======================
可以

#5


注意大小写

#6


没有时间做Demo了,简单说一下

       if (e.Row.RowType == DataControlRowType.DataRow)
        {
            GridView oGridView = (GridView)e.Row.FindControl("DetailGridView");//获取嵌套的GridView
            SqlDataSource sqldata = (SqlDataSource)e.Row.FindControl("SqlDataSource1");
            sqldata.ConnectionString = sqlcon;
            sqldata.SelectCommand = "select top 5 zdw,zxs1,zdw1,zxs2,zdw2 from Report where zcpdm like 'A%' ";
            oGridView.DataBind();
        }
你应该在内部的GridView里再放一个SqlDataSource,并且设置好它的SelectCommand,但它可以有一个参数暂时设置为none
然后在RowDataBound事件中
应该大致改成这样


       if (e.Row.RowType == DataControlRowType.DataRow)
        {
                SqlDataSource sqldata = e.Item.FindControl("SqlDataSource2") as SqlDataSource;
                if (sqldata != null)
                {
                    sqldata.SelectParameters["CategoryID"].DefaultValue = GridView2.DataKeys[ e.Row.RowIndex].Value.ToString();
                }
        }

#7


amandag的方法值得参考,我试一下好使马上给分

#8


其实现在我用objectdatsource就很简单的解决了

#9


up

#10


引用 6 楼 amandag 的回复:
没有时间做Demo了,简单说一下 

              if   (e.Row.RowType   ==   DataControlRowType.DataRow) 
                { 
                        GridView   oGridView   =   (GridView)e.Row.FindControl( "DetailGridView ");//获取嵌套的GridView 
                        SqlDataSource   sqldata   =   (SqlDataSource)e.Row.FindControl( "SqlDataSource1 "); 
                        sqldata.Connection…

#11


来自:http://www.cnblogs.com/dushouke/archive/2008/03/24/1120377.html 
前台代码: 

C# code<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default5.aspx.cs" Inherits="Default5" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>无标题页</title>
</head>
<body>
    <form id="form1" runat="server">
    <div align="center">
    
        <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" 
            CellPadding="4" ForeColor="#333333" GridLines="None" Height="285px" 
            onrowcancelingedit="GridView1_RowCancelingEdit" 
            onrowdeleting="GridView1_RowDeleting" onrowediting="GridView1_RowEditing" 
            onrowupdating="GridView1_RowUpdating" Width="771px">
            <FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
            <RowStyle BackColor="#F7F6F3" ForeColor="#333333" />
            <Columns>
                <asp:BoundField DataField="id" HeaderText="学号" />
                <asp:BoundField DataField="name" HeaderText="姓名" />
                <asp:BoundField DataField="sex" HeaderText="性别" />
                <asp:BoundField DataField="age" HeaderText="年龄" />
                <asp:BoundField DataField="department" HeaderText="专业" />
                <asp:BoundField DataField="grade" HeaderText="班级" />
                <asp:CommandField HeaderText="编辑 " ShowEditButton="True" />
                <asp:CommandField HeaderText="选择" ShowSelectButton="True" />
                <asp:TemplateField HeaderText="删除 " ShowHeader="False">
                    <ItemTemplate>
                        <asp:LinkButton ID="LinkButton1" runat="server" CausesValidation="False" 
                            CommandName="Delete" Text="删除" OnClientClick="return confirm('你确定要删除吗?')"></asp:LinkButton>
                    </ItemTemplate>
                </asp:TemplateField>
            </Columns>
            <PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" />
            <SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" />
            <HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
            <EditRowStyle BackColor="#999999" />
            <AlternatingRowStyle BackColor="White" ForeColor="#284775" />
        </asp:GridView>
    
    </div>
    </form>
</body>
</html>



后台代码: 

C# codeusing System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Data.SqlClient;

public partial class Default5 : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            Bind();
        }
    }

    private void Bind()
    {
        SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["Personal"].ConnectionString);
        SqlDataAdapter adq = new SqlDataAdapter("select * from information", conn);
        DataSet dataset=new DataSet();
        adq.Fill(dataset,"information");
        GridView1.DataSource=dataset;
        GridView1.DataKeyNames=new String[]{"id"};
        GridView1.DataBind();
    }
    protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
    {
        GridView1.EditIndex = e.NewEditIndex;
        Bind();
    }
    protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["Personal"].ConnectionString);
        SqlCommand comm = new SqlCommand("delete from information where id='" + GridView1.DataKeys[e.RowIndex].Value.ToString() + "'",conn);
        conn.Open();
        try
        {
            int i = Convert.ToInt32(comm.ExecuteNonQuery());
            if (i > 0)
            {
                Response.Write("<script language=javascript>alert('删除成功!')</script>");
            }
            else
            {
                Response.Write("<script language=javascript>alert('删除失败!')</script>");
            }
            Bind();
        }
        catch (Exception erro)
        {
            Response.Write("错误信息:"+erro.Message);
        }
        finally
        {
            conn.Close();
        }
    }
    protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
        string id = ((TextBox)GridView1.Rows[e.RowIndex].Cells[0].Controls[0]).Text.ToString().Trim();
        string name = ((TextBox)GridView1.Rows[e.RowIndex].Cells[1].Controls[0]).Text.ToString().Trim();
        string sex = ((TextBox)GridView1.Rows[e.RowIndex].Cells[2].Controls[0]).Text.ToString().Trim();
        string age= ((TextBox)GridView1.Rows[e.RowIndex].Cells[3].Controls[0]).Text.ToString().Trim();
        string department = ((TextBox)GridView1.Rows[e.RowIndex].Cells[4].Controls[0]).Text.ToString().Trim();
        string grade = ((TextBox)GridView1.Rows[e.RowIndex].Cells[5].Controls[0]).Text.ToString().Trim();
        SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["Personal"].ConnectionString);
        SqlCommand comm = new SqlCommand("update information set id='"+id+"', name='"+name+"' , sex='"+sex+"' , age='"+age+"' , department='"+department+"' , grade='"+grade+"' where id='"+GridView1.DataKeys[e.RowIndex].Value.ToString()+"'", conn);
        conn.Open();
        try
        {
            int i = Convert.ToInt32(comm.ExecuteNonQuery());
            if (i > 0)
            {
                Response.Write("<script language=javascript>alert('保存成功!')</script>");
            }
            else
            {
                Response.Write("<script language=javascript>alert('保存失败!')</script>");
            }
            GridView1.EditIndex = -1;
            Bind();
        }
        catch (Exception erro)
        {
            Response.Write("错误信息:" + erro.Message);
        }
        finally
        {
            conn.Close();
        }
    }
    protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
    {
        GridView1.EditIndex = -1;
        Bind();
    }
}