|
DataGrid数据取到Textbox
以前用VB,数据库操作不适应
以下代码从数据库取得数据显示到DataGrid
Dim cString As String = server=192.168.1.13;database=test;user id=sa;password=123456
Dim cnNorthwind As SqlConnection = New SqlConnection(cString)
Create a SqlDataAdapter for the Suppliers table.
Dim adpSuppliers As SqlDataAdapter = New SqlDataAdapter
A table mapping tells the adapter what to call the table.
adpSuppliers.TableMappings.Add( Table , Suppliers )
cnNorthwind.Open()
Dim cmdSuppliers As SqlCommand = _
New SqlCommand( SELECT * FROM Suppliers , cnNorthwind)
cmdSuppliers.CommandType = CommandType.Text
adpSuppliers.SelectCommand = cmdSuppliers
Console.WriteLine( The connection is open. )
ds = New DataSet( Suppliers )
adpSuppliers.Fill(ds)
myGrid.DataSource = ds.Tables( Suppliers )
现在想在MyGrid移动的时候把当前记录的SupplierID,CompanyName显示到Textbox1和Textbox1,有两个问题:
1、我会以下代码取得,但是我希望用字段名来取得,有没有办法
TextBox1.Text = myGrid.Item(myGrid.CurrentRowIndex, 0)
TextBox2.Text = myGrid.Item(myGrid.CurrentRowIndex, 1)
2、DataGrid的什么事件可以检测到当前记录的移动,我用myGrid_CurCellChange事件没有反应。
谢谢。
__________________________________________________________________________
顶下回家
__________________________________________________________________________
可以用客户端Javascript实现
__________________________________________________________________________
顶下
__________________________________________________________________________
2.private void dataGrid1_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e)
{
System.Drawing.Point pt= new Point(e.X,e.Y);
DataGrid.HitTestInfo hti= this.dataGrid1.HitTest(pt);
MessageBox.Show(hti.Row.ToString());//hti.Row就是你双击的行
}
}
__________________________________________________________________________
谢谢楼上
用MouseDown、MouseUp不能检测到键盘操作的移动
第二个问题我基本上用CurCellChange解决了
__________________________________________________________________________
TextBox1.Text = myGrid.Item(myGrid.CurrentRowIndex, 0)
TextBox2.Text = myGrid.Item(myGrid.CurrentRowIndex, 1)
我基本上使用datebinding
__________________________________________________________________________
Me.UTE_GH.DataBindings.Add( Text , UltraGrid1.DataSource, 工号 )
Me.UTE_XM.DataBindings.Add( Text , UltraGrid1.DataSource, 姓名 )
Me.UCE_XB.DataBindings.Add( Text , UltraGrid1.DataSource, 性别 )
__________________________________________________________________________
谢谢 小黑MM
__________________________________________________________________________ |
|