gridview选择一行取出数据

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

public partial class DataViewSelectRow : System.Web.UI.Page
...{
  
    protected void Page_Load(object sender, EventArgs e)
    ...{
        if (!IsPostBack)
        ...{
            lblStr.Text = "select  * from Products";
            DataSet ds = BindSqlServer(lblStr.Text);   //
            GridView1.DataSource = ds;
            GridView1.DataBind();
        }
    }
    
    private DataSet BindSqlServer(string str)
t').style.display='none'; document.getElementById('_712_951_Closed_Image').style.display='inline'; document.getElementById('_712_951_Closed_Text').style.display='inline';" alt="" src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif" align=top>    ...{
        SqlConnection Conn = new SqlConnection("server=.;uid =sa ; pwd=;database = northwind");

        DataSet ds = new DataSet();
        SqlDataAdapter da = new SqlDataAdapter(str, Conn);

        da.Fill(ds);
        return ds;
    }

    protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
    ...{
        GridView1.PageIndex = e.NewPageIndex;

        DataSet ds = BindSqlServer("select  * from Products");
        GridView1.DataSource = ds;
        GridView1.DataBind();
    }
    protected void GridView1_RowCreated(object sender, GridViewRowEventArgs e)
    ...{
        //注册行单击事件
        e.Row.Attributes.Add("onclick", "javascirpt:__doPostBack('GridView1','Select$" + e.Row.RowIndex + "')");
    }

    protected void GridView

1_SelectedIndexChanged(object sender, EventArgs e)
    ...{
        Response.Write(GridView1.SelectedValue);  //显示单示列的ID
        //得到ID后将其付给一个变量

        lblStr.Text = "select * from products where Productid =" + GridView1.SelectedValue;
        DataSet ds = new DataSet();
        ds = BindSqlServer(lblStr.Text);

        this.TextBox1.Text = ds.Tables[0].Rows[0]["QuantityPerUnit"].ToString();
    }
}

 

//另一种方法
先把用GridView把一个表里的数据绑出来,在GridView里加一个CheckBox,然后把选中的行的值插入到另外一表中!!

回答说明:
选中行的时候会触发事件的 可以通过人rowcommend捕捉
这个事件返回的e.commendname 好象是select
e.commendargement好象是行号
通过行号GridView1.Rows[int.prase(e.commendargement)].cells[??].text
就可以获取当前行某列的值
如果是控件就通过GridView1.Rows[int.prase(e.commendargement)].findcontrol(空间名).属性
获取控件的值
然后把获取的值写一个insert语句插入就可以了

posted @ 2010-08-09 11:23  midisong  阅读(749)  评论(0)    收藏  举报