![](/skin/img/sigline.gif)
海鸽 is My Lover!!
收录了.....
首先:
1:很奇怪,你取数据到label跟datagrid有什么联系吗?
2:怎么从数据库中取数据出来?存放到哪里,弄清楚概念了没?
3:把数据放在label也就是一个label1.text属性=的问题而已啊?
看看我的做法和最初的解决方法有什么不一样的:
呵,好象只是没有了datagrid.
private void Page_Load(object sender, System.EventArgs e)
{
//连接access数据库
if(!IsPostBack)
{
LabelContent.Text="";
lblTitle.Text="";
string getid=Request.QueryString["id"].ToString();
OleDbConnection conn=DB.create_access_Connection();//创建数据库连接
conn.Open();
OleDbCommand cmd=conn.CreateCommand();
cmd.CommandText="SELECT title,content FROM news where id="+getid;
cmd.CommandType=CommandType.Text;
OleDbDataReader reader=cmd.ExecuteReader(); //取得数据集
if(reader.Read())//取出数据,有数据就放进lable
{
lblTitle.Text=reader["title"].ToString().Trim();
LabelContent.Text=reader["content"].ToString().Trim();
}
reader.Close();
conn.Close();
}
}
=================================================
/*下面的一段程序功能是:动态获取dataGrid1中选中行的各字段的值*/
private void dataGrid1_CurrentCellChanged(object sender, EventArgs e)
{
string myID = dataGrid1[dataGrid1.CurrentCell.RowNumber, 0].ToString().Trim();
SqlConnection cn = new SqlConnection("Data Source=localhost;Initial Catalog=student;Integrated Security=True");
SqlCommand cmd = new SqlCommand("Select * From student where ID='" + myID + "'", cn);
cn.Open();
SqlDataReader dr = cmd.ExecuteReader();
if (dr.Read())
{
this.textBox1.Text = dr["sno"].ToString();
this.textBox2.Text = dr["sname"].ToString();
this.textBox3.Text = dr["sex"].ToString();
}
dr.Close();
cn.Close();
}