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

解决方案 »

  1.   

    DataGrid的mouse_up事件中
    把每个字段取出添到textBox
      

  2.   

    你这是DataGrid控件的事件,如果点击的是DataGrid中空白的地方呢?还有如果点击的是两个行之间的线条呢?
      

  3.   

    这是一个数据绑定到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;
                
            }
      

  4.   

    楼上的代码可以通过鼠标点击DataGridView行查看相应的数据行吗?没有鼠标点击的代码啊?难道是我看不懂?
      

  5.   

    TO:楼上的代码可以通过鼠标点击DataGridView行查看相应的数据行吗?没有鼠标点击的代码啊?难道是我看不懂?如果你是绑定的数据,就可以...行不行可以自己去试试...
      

  6.   

    this.textBox1.DataBindings.Add(new System.Windows.Forms.Binding("Text", this.dataSet11, "管理员信息.编号"));
    这是绑定吧?
      

  7.   

    我把一个TextBox绑定到数据库中某个表的某个字段,窗体加载的时候TextBox显示的是表中第一行的绑定字段,如果鼠标点击DataGrid其他行,TextBox没变化。
      

  8.   

    try..this.textBox1.DataBindings.Add(new System.Windows.Forms.Binding("Text", this.dataSet11.Tables["管理员信息"], "编号"));如果还是不行,请把代码贴出来看看...
      

  9.   

    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();
                    this.txtLEDPortName.Text = rowLED.Cells[2].Value.ToString();
                    this.txtLEDStationNO.Text = rowLED.Cells[3].Value.ToString();
                    this.numericUpDownLEDLight.Value = Convert.ToDecimal(rowLED.Cells[4].Value);
                    this.numericUpDownLEDScreenLength.Value = Convert.ToDecimal(rowLED.Cells[5].Value);
                    this.numericUpDownLEDSpeed.Value = Convert.ToDecimal(rowLED.Cells[6].Value);
                    this.numericUpDownLEDStoptime.Value = Convert.ToDecimal(rowLED.Cells[7].Value);
                    this.cobINFashion.SelectedValue = rowLED.Cells[8].Value.ToString();
                    this.cobOUTFashion.SelectedValue = rowLED.Cells[9].Value.ToString();
      

  10.   

    多谢大家的帮助,问题出在DataGrid上,先前DataGrid的DataSource=dataSet11.管理员信息,改成DataSource=dataSet11,DataMember="管理员信息"就可以实现TextBox中的值随鼠标点击不同的行而改变了。