| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 682 人关注过本帖
标题:菜鸟求高手帮忙
只看楼主 加入收藏
a256725
Rank: 1
等 级:新手上路
帖 子:7
专家分:0
注 册:2012-11-8
结帖率:100%
收藏
已结贴  问题点数:20 回复次数:9 
菜鸟求高手帮忙
我创建一个按钮和一个gridview
我想要是实现的是
点击按钮
连接到本地数据库
把本地数据库的一个表显示在gridview内
gridview内可以编辑删除
就这么简单。
搜索更多相关主题的帖子: 数据库 
2012-11-08 13:52
a256725
Rank: 1
等 级:新手上路
帖 子:7
专家分:0
注 册:2012-11-8
收藏
得分:0 
这么简单没人帮解决。。主要是单击按钮,gridview才绑定和显示sql表
2012-11-08 17:49
tww124849980
Rank: 2
等 级:论坛游民
帖 子:13
专家分:22
注 册:2010-6-29
收藏
得分:20 
你还是把你初步写的代码发出来,有不对的大家给你改下,实现的话谁有时间啊。
2012-11-09 11:15
a256725
Rank: 1
等 级:新手上路
帖 子:7
专家分:0
注 册:2012-11-8
收藏
得分:0 
protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
        string KeyId = GridView1.DataKeys[e.RowIndex].Value.ToString();
        string S_Number= Server.HtmlEncode(((TextBox)this.GridView1.Rows[e.RowIndex].Cells[1].Controls[0]).Text.ToString());//这里开始错了
        string S_Name = Server.HtmlEncode(((TextBox)this.GridView1.Rows[e.RowIndex].Cells[2].Controls[0]).Text.ToString());
        string Sex = Server.HtmlEncode(((TextBox)this.GridView1.Rows[e.RowIndex].Cells[3].Controls[0]).Text.ToString());
        string Birthday = Server.HtmlEncode(((TextBox)this.GridView1.Rows[e.RowIndex].Cells[4].Controls[0]).Text.ToString());
        string Nation = Server.HtmlEncode(((TextBox)this.GridView1.Rows[e.RowIndex].Cells[5].Controls[0]).Text.ToString());
        string Politics = Server.HtmlEncode(((TextBox)this.GridView1.Rows[e.RowIndex].Cells[6].Controls[0]).Text.ToString());
        string Department = Server.HtmlEncode(((TextBox)this.GridView1.Rows[e.RowIndex].Cells[7].Controls[0]).Text.ToString());
        string Addess = Server.HtmlEncode(((TextBox)this.GridView1.Rows[e.RowIndex].Cells[8].Controls[0]).Text.ToString());
        string PostalCode = Server.HtmlEncode(((TextBox)this.GridView1.Rows[e.RowIndex].Cells[9].Controls[0]).Text.ToString());
        string Phone = Server.HtmlEncode(((TextBox)this.GridView1.Rows[e.RowIndex].Cells[10].Controls[0]).Text.ToString());
        try{
            SqlConnection con=new SqlConnection(strCon);
            con.Open();
            string sql="update t_student set S_Name='" + S_Name + "',S_Number='" + S_Number + "',Sex='" + Sex + "',Birthday='" + Birthday + "',Nation='" + Nation + "',Politics='" + Politics + "',Department='" + Department + "',Addess='" + Addess + "',PostalCode='"+PostalCode+"',Phone='"+Phone+"' where S_Number="+KeyId;
            SqlCommand cmd=new SqlCommand(sql,con);
            cmd.ExecuteNonQuery();
            Response.Write("执行成功了.");
            GridView1.EditIndex=-1;
            con.Close();
        }
2012-11-09 17:21
a256725
Rank: 1
等 级:新手上路
帖 子:7
专家分:0
注 册:2012-11-8
收藏
得分:0 
后台代码
String strCon = "server=6KT3S91GBFLZ6ZK\\SQLEXPRESS;database=Stuinfo;Integrated Security=SSPI";
   
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!this.IsPostBack)
        {
            dd();
        }
    }
    private void dd()
    {
        SqlConnection con = new SqlConnection(strCon);
        con.Open();

        String sqlStr = "select * from t_student";
        SqlDataAdapter myAdapt = new SqlDataAdapter(sqlStr, con);

        DataSet ds = new DataSet();

        myAdapt.Fill(ds,"e1");
        GridView1.DataSource = ds.Tables["e1"];
        GridView1.DataBind();

        con.Close();
    }
    protected void Button2_Click(object sender, EventArgs e)
    {
        SqlConnection con = new SqlConnection(strCon);
        con.Open();
        string strSql = "Insert into t_student(S_Number,S_Name,Sex,Birthday,Nation,Politics,Department,Address,PostalCode,Phone)values('" + TextBox1.Text + "','" + TextBox2.Text + "','" + TextBox3.Text + "','" + TextBox4.Text + "','" + TextBox5.Text + "','" + TextBox6.Text + "','" + TextBox7.Text + "','" + TextBox8.Text + "','" + TextBox9.Text + "','" + TextBox10.Text + "')";
        SqlCommand cmd = new SqlCommand(strSql, con);
        cmd.ExecuteNonQuery();

        con.Close();
    }
    protected void Button3_Click(object sender, EventArgs e)
    {
        SqlConnection con = new SqlConnection(strCon);
        con.Open();

        String sqlStr = "select * from t_student";
        SqlDataAdapter myAdapt = new SqlDataAdapter(sqlStr, con);

        DataSet ds = new DataSet();

        myAdapt.Fill(ds,"e1");
        GridView1.DataSource = ds.Tables["e1"];
        GridView1.DataBind();

        con.Close();
    }
    protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
    {
        this.GridView1.EditIndex = e.NewEditIndex;
        dd();
    }
    protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        
        SqlConnection con = new SqlConnection(strCon);
        con.Open();
        string sql="delete from t_student where S_Number=" + GridView1.DataKeys[e.RowIndex].Value;
        SqlCommand cmd = new SqlCommand(sql, con);
        cmd.ExecuteNonQuery();
        Response.Redirect("default2.aspx");
    }
    protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
    {
        GridView1.EditIndex = -1;
    }
    protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
        string KeyId = GridView1.DataKeys[e.RowIndex].Value.ToString();
        string S_Number= Server.HtmlEncode(((TextBox)this.GridView1.Rows[e.RowIndex].Cells[1].Controls[0]).Text.ToString());
        string S_Name = Server.HtmlEncode(((TextBox)this.GridView1.Rows[e.RowIndex].Cells[2].Controls[0]).Text.ToString());
        string Sex = Server.HtmlEncode(((TextBox)this.GridView1.Rows[e.RowIndex].Cells[3].Controls[0]).Text.ToString());
        string Birthday = Server.HtmlEncode(((TextBox)this.GridView1.Rows[e.RowIndex].Cells[4].Controls[0]).Text.ToString());
        string Nation = Server.HtmlEncode(((TextBox)this.GridView1.Rows[e.RowIndex].Cells[5].Controls[0]).Text.ToString());
        string Politics = Server.HtmlEncode(((TextBox)this.GridView1.Rows[e.RowIndex].Cells[6].Controls[0]).Text.ToString());
        string Department = Server.HtmlEncode(((TextBox)this.GridView1.Rows[e.RowIndex].Cells[7].Controls[0]).Text.ToString());
        string Addess = Server.HtmlEncode(((TextBox)this.GridView1.Rows[e.RowIndex].Cells[8].Controls[0]).Text.ToString());
        string PostalCode = Server.HtmlEncode(((TextBox)this.GridView1.Rows[e.RowIndex].Cells[9].Controls[0]).Text.ToString());
        string Phone = Server.HtmlEncode(((TextBox)this.GridView1.Rows[e.RowIndex].Cells[10].Controls[0]).Text.ToString());
        try{
            SqlConnection con=new SqlConnection(strCon);
            con.Open();
            string sql="update t_student set S_Name='" + S_Name + "',S_Number='" + S_Number + "',Sex='" + Sex + "',Birthday='" + Birthday + "',Nation='" + Nation + "',Politics='" + Politics + "',Department='" + Department + "',Addess='" + Addess + "',PostalCode='"+PostalCode+"',Phone='"+Phone+"' where S_Number="+KeyId;
            SqlCommand cmd=new SqlCommand(sql,con);
            cmd.ExecuteNonQuery();
            Response.Write("执行成功了.");
            GridView1.EditIndex=-1;
            con.Close();
        }
        catch(Exception exc){
            Response.Write(exc.Message);
        }
        dd();
    }
2012-11-09 17:22
a256725
Rank: 1
等 级:新手上路
帖 子:7
专家分:0
注 册:2012-11-8
收藏
得分:0 
前台的gridview
<asp:GridView ID="GridView1" runat="server"
            onrowdeleting="GridView1_RowDeleting"
            onrowediting="GridView1_RowEditing"
             DataKeyNames="S_Number" onrowcancelingedit="GridView1_RowCancelingEdit"
            onrowupdating="GridView1_RowUpdating">
            <Columns>
                <asp:CommandField ShowDeleteButton="True" />
                <asp:CommandField ShowEditButton="True" />
            </Columns>
        </asp:GridView>
2012-11-09 17:23
a256725
Rank: 1
等 级:新手上路
帖 子:7
专家分:0
注 册:2012-11-8
收藏
得分:0 
System.InvalidCastException: 无法将类型为“System.Web.UI.WebControls.DataControlLinkButton”的对象强制转换为类型“System.Web.UI.WebControls.TextBox”。
string S_Number= Server.HtmlEncode(((TextBox)this.GridView1.Rows[e.RowIndex].Cells[1].Controls[0]).Text.ToString());
2012-11-09 17:26
a256725
Rank: 1
等 级:新手上路
帖 子:7
专家分:0
注 册:2012-11-8
收藏
得分:0 
自己慢慢做。
2012-11-09 17:31
跳过去
Rank: 8Rank: 8
等 级:贵宾
威 望:20
帖 子:282
专家分:976
注 册:2012-8-13
收藏
得分:0 
自己一个一个功能做就好啦

光棍中.....
2012-11-09 22:27
lock9658
Rank: 2
等 级:论坛游民
帖 子:9
专家分:21
注 册:2013-1-26
收藏
得分:0 
Text t = sender as Text;
GridViewRow row = t.Parent.Parent as GridViewRow;
Text t_s_num = (Text)row.FindControl("S_Number");
string s_num =  t_s_num.text.trim();
绑定改用TempleteField
2013-01-28 16:34
快速回复:菜鸟求高手帮忙
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.020697 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved