日期:2014-05-19  浏览次数:20810 次

当点击DataGrid中的行时的问题,
要求鼠标点击到DataGrid中的行时,将DataGrid中每一列的数据放到窗体中相应的TextBox中?没找到像这样的事件啊。

------解决方案--------------------
DataGrid的mouse_up事件中
把每个字段取出添到textBox
------解决方案--------------------
这是一个数据绑定到DataGridView的例子(和DataGrid原理是一样的),可以通过点击按钮来查看 "前一个 "和 "后一个 "数据,也可以直接通过点击DataGridView的行查看...

for example:

private DataSet ds = new DataSet();
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
DataBind();
}
private void DataBind()
{
SqlConnection con = new SqlConnection( "server=.;database=student;uid=sa;pwd=0421 ");
SqlDataAdapter sda = new SqlDataAdapter( "select sno,sname,sage from studentDetails ", con);
sda.Fill(ds, "student ");
//数据绑定
this.dataGridView1.DataSource = ds.Tables[ "student "];
this.textBox1.DataBindings.Add( "Text ", ds.Tables[ "student "], "sno ");
this.textBox2.DataBindings.Add( "Text ", ds.Tables[ "student "], "sname ");
this.textBox3.DataBindings.Add( "Text ", ds.Tables[ "student "], "sage ");

}
private void button1_Click(object sender, EventArgs e)
{
//前一个
this.BindingContext[ds.Tables[ "student "]].Position += 1;


}
private void button2_Click(object sender, EventArgs e)
{
//后一个
this.BindingContext[ds.Tables[ "student "]].Position -= 1;

}
------解决方案--------------------
private DataSet ds = new DataSet();
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
DataBind();
}
private void DataBind()
{
SqlConnection con = new SqlConnection( "server=.;database=student;uid=sa;pwd=0421 ");
SqlDataAdapter sda = new SqlDataAdapter( "select sno,sname,sage from studentDetails ", con);
sda.Fill(ds, "student ");
//数据绑定
this.dataGridView1.DataSource = ds.Tables[ "student "];
this.textBox1.DataBindings.Add( "Text ", ds.Tables[ "student "], "sno ");
this.textBox2.DataBindings.Add( "Text ", ds.Tables[ "student "], "sname ");
this.textBox3.DataBindings.Add( "Text ", ds.Tables[ "student "], "sage ");

}
private void button1_Click(object sender, EventArgs e)
{
//前一个
this.BindingContext[ds.Tables[ "student "]].Position += 1;


}
private void button2_Click(object sender, EventArgs e)
{
//后一个
this.BindingContext[ds.Tables[ "student "]].Position -= 1;

}


你可以在cellclick事件中
写下

把当前行的资料付给text
DataGridViewRow rowLED = dataGridViewLED.CurrentRow;
this.txtLEDID.Text = rowLED.Cells[0].Value.ToString();
this.txtLEDNportIP.Text = rowLED.Cells[1].Value.ToString();