这是我写的一个函数,调用事件textBox3_TextChanged
我对Windows.Forms下的dataGrid1 太不熟悉了
请教一下各位高手,dataGrid1 怎么样设置才可以默认选重第一行
当我双击第一行时,则把第一行的其中一个字段的值(事先指定好其中的一个字段),写到textBox3.Text.谢谢高手救命,分不够给我说 加就是了
下面是我写的函数,可以查出我想要的东西,具体怎么改请高手指点一下 
分不够可以加
public void FuWuSheng()
{
string strConn = " Initial Catalog=SYXT;Data Source = LEIKE;User ID=sa;Password=sa"; SqlConnection conn = new SqlConnection();
conn.ConnectionString = strConn;
try
{
SqlCommand selcmd = new SqlCommand();
selcmd.Connection = conn;
selcmd.CommandText = "select * from GuYuan where GYID like '" + textBox3.Text + "%'";
SqlDataAdapter da = new SqlDataAdapter();
da.SelectCommand = selcmd;
DataSet ds = new DataSet();
da.Fill(ds,"GuYuan");
dataGrid1.DataSource = ds.Tables["GuYuan"];
conn.Open ();
conn.Close();
}
catch (Exception a)
{
MessageBox.Show (a.Message,"警告",MessageBoxButtons.OK,MessageBoxIcon.Asterisk);
}
}

解决方案 »

  1.   

    可以这样,
    定义在datagrid的mouse up 或down事件
    i = datagrid.CurrentRowIndex
    Me.textbox3.Text = datagrid.Item(i, 0)
    Me.textbox4.Text = datagrid.Item(i, 1)这样 datagrid的第一行的第一列值就与textbox3.text属性绑定上了
      

  2.   

    Me.textbox4.Text = datagrid.Item(datagrid.currentrowindex, 列数?)
      

  3.   

    this.statDataGrid.Select(1); this.dataRowView = (DataRowView)this.statDataGrid.BindingContext[this.statDataGrid.DataSource,this.statDataGrid.DataMember].Current;ShowColumnsName.CurrEnterpriseEmployeeID = this.dataRowView.Row[0].ToString();
      

  4.   

    我更本找不到datagrid.Item Item的属性呢?
      

  5.   

    private DataTable dataT;
    textBox1.Text = dataT.Rows[dataGrid1.CurrentCell.RowNumber][0].ToString();
      

  6.   

    改一下楼上的
    textBox3.Text = dataGrid1.CurrentCell[dataGrid1.CurrentCell.RowNumber,0].ToString();
      

  7.   

    private void dataGrid1_MouseUp(object sender, System.Windows.Forms.MouseEventArgs e)
    {   
    i = dataGrid1.CurrentCell.RowNumber;
    dataGrid1.Select(i);
    }
    声明一个全局变量 int i = 0; 等于0则默认选种第一行
    当你选择行时,利用dataGrid1.CurrentCell.RowNumber当前的行数
    执行选择dataGrid1.Select(i);可以解决你的选择行的要求至于你的第二个问题:当我双击第一行时,则把第一行的其中一个字段的值(事先指定好其中的一个字段),写到textBox3.Text这个问题,我只能做到当你点击第一行时,的根据你点的哪个单元格的值传递给textBox3.Text所以达不到你的要求,我非常抱歉。
      

  8.   

    当双击第一行时,则把第一行的其中一个字段的值(事先指定好其中的一个字段),写到textBox3.Text.使用数据绑定比较好,
    http://blog.csdn.net/zhzuo/archive/2005/01/03/238273.aspx
    如果非要取值,
    取出DataGrid当前行对应的DataTable的值。
    如果DataGrid绑定的是DataView,
    DataRowView drv = (DataRowView)this.BindingContext[this.ds,this.ds.Tables[0].TableName].Current;
    //如果绑定的是DataTable
    //DataRow dw = (DataRow)this.BindingContext[this.dataGrid1.DataSource,this.dataGrid1.Memeber].Current;
    获取到drv就可以改数据,
    DataRow dw = drv.Row; // 得到DataRow,
    DataTable dt = dw.Table;//得到DataTable
    DataSet ds = dt.DataSet;
      

  9.   

    //定义一个全局的DataSet ds = null;
    private void button1_Click(object sender, System.EventArgs e)
    {
    FuWuSheng();
    this.dataGrid1.Select(0);
    } private void dataGrid1_MouseUp(object sender, System.Windows.Forms.MouseEventArgs e)
    {
    this.textBox1.Text = ds.Tables[0].Rows[dataGrid1.CurrentCell.RowNumber][dataGrid1.CurrentCell.ColumnNumber].ToString();
    }
    public void FuWuSheng()
    {
    string strConn = "Persist Security Info=False;Integrated Security=SSPI;database=pubs;server=epaib"; SqlConnection conn = new SqlConnection();
    conn.ConnectionString = strConn;
    try
    {
    SqlCommand selcmd = new SqlCommand();
    selcmd.Connection = conn;
    selcmd.CommandText = "select * from authors ";
    SqlDataAdapter da = new SqlDataAdapter();
    da.SelectCommand = selcmd;
    ds = new DataSet();
    da.Fill(ds,"authors");
    dataGrid1.DataSource = ds.Tables["authors"];
    conn.Open ();
    conn.Close();
    }
    catch (Exception a)
    {
    MessageBox.Show (a.Message,"警告",MessageBoxButtons.OK,MessageBoxIcon.Asterisk);
    }
    }
    我已经测试成功.....不知道是不是楼主要的结果
      

  10.   

    你要是不是想在DataGrid中选中行中某单元的值阿!
    dataGrid.HitTest(row, col);剩下的我想你就明白了吧!
    正行选中要在MouseUp事件里写dataGrid.Select(dataGrid.HitTest(e.x,e.y).Row);
      

  11.   

    纠正第一个问题,选中获取datagrid中的值是 datagrid[row,col].toString();
      

  12.   

    我试一试  sos0241(小白) epaib(Keeman)  你给出了详细代码 谢谢我现在马上试哈
      

  13.   

    epaib(Keeman)实在抱歉你的不能达到要求我要的是在DataGrid中选中任意行得到任意行中第二列的值