DataGrid数据取到Textbox,该如何处理
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事件没有反应。
谢谢。
------解决方案--------------------
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就是你双击的行
}
}
------解决方案--------------------
Me.UTE_GH.DataBindings.Add( "Text ", UltraGrid1.DataSource, "工号 ")
Me.UTE_XM.DataBindings.Add( "Text ", UltraGrid1.DataSource, "姓名 ")
Me.UCE_XB.DataBindings.Add( "Text ", UltraGrid1.DataSource, "性别 ")
以前用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事件没有反应。
谢谢。
------解决方案--------------------
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就是你双击的行
}
}
------解决方案--------------------
Me.UTE_GH.DataBindings.Add( "Text ", UltraGrid1.DataSource, "工号 ")
Me.UTE_XM.DataBindings.Add( "Text ", UltraGrid1.DataSource, "姓名 ")
Me.UCE_XB.DataBindings.Add( "Text ", UltraGrid1.DataSource, "性别 ")