//绑定数据源
DataSet ds = new DataSet();
SqlConnection connection = new SqlConnection(netiFace.ConnectionString.ConnectionString);
connection.Open();
SqlDataAdapter da = new SqlDataAdapter("select * from dot", connection);
da.Fill(ds, "dot");
DataTable mytable = new DataTable();
mytable = ds.Tables["dot"];
dataGridView1.AutoGenerateColumns = true;
dataGridView1.DataSource = mytable;
dataGridView1.MultiSelect = false;//不可多选行
bindingSource1.DataSource = mytable;
bindingNavigator1.BindingSource = bindingSource1;
//隐藏一个页签
this.tabControl2.TabPages.Remove(tabControl2.TabPages[2]);
this.tabControl1.TabPages.Remove(tabControl1.TabPages[3]);
this.tabControl1.TabPages.Remove(tabControl1.TabPages[2]);
----------------------------- //bindingNavigator和dataGridView的联动----------------------------------------------
bindingNavigatorMoveLastItem.Click += new System.EventHandler(this.MoveDataControl);
bindingNavigatorMoveLastItem.Click += new System.EventHandler(this.MoveDataControl);
bindingNavigatorMovePreviousItem.Click+= new System.EventHandler(this.MoveDataControl);
bindingNavigatorMoveNextItem.Click += new System.EventHandler(this.MoveDataControl);
public void MoveDataControl(object sender, EventArgs e)
{
//下一行
int rowIndex = int.Parse(bindingNavigatorPositionItem.Text) - 1;
dataGridView1.ClearSelection();
//选中这一行
dataGridView1.Rows[rowIndex].Selected = true;
//最前面的黑色三角号跟着移动
dataGridView1.CurrentCell = dataGridView1.Rows[rowIndex].Cells[0];
//滚动条跟着移动
dataGridView1.FirstDisplayedScrollingRowIndex = dataGridView1.SelectedRows[0].Index;
}
‘-------------------------------------------------------------------------------------------------------------------------------
如果要在BindingNavigator中的删除按钮按下前,会出现是否删除的提示,请下列操作
1、将DataGridView中的DeleteItem属性与删除按钮取消绑定
2、添加删除按钮的Click事件
3、在删除按钮的Click事件中键入以下代码
if (!(操作员DataGridView.CurrentRow.IsNewRow))
{
if (MessageBox.Show("确认删除该笔数据?", "请选择", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2) == DialogResult.Yes)
{
操作员DataGridView.Rows.Remove(this.操作员DataGridView.CurrentRow);
}
'-----------------------------------------------------------------------------------------------------------------------------
‘VB.net对于datagride控件的使用
'刷新datagride控件
Public Sub RefreshDG(ByRef dic As Dictionary(Of String, Integer), ByRef MyTable As DataTable)
'datagride的列名和列宽
If IsNothing(MyTable) = False Then
If DataGrid1.TableStyles.Count = 0 Then
Dim keyvalue As KeyValuePair(Of String, Integer)
Dim MyTabStyle As New DataGridTableStyle()
Dim MyCol As DataGridTextBoxColumn
Dim intCnt As Integer = 0
MyTabStyle.MappingName = MyTable.TableName
For Each keyvalue In dic
MyCol = New DataGridTextBoxColumn()
MyCol.MappingName = MyTable.Columns(intCnt).ColumnName
MyCol.Width = keyvalue.Value
MyCol.HeaderText = keyvalue.Key
MyTabStyle.GridColumnStyles.Add(MyCol)
intCnt = intCnt + 1
Next
DataGrid1.TableStyles.Add(MyTabStyle)
keyvalue = Nothing
MyTabStyle = Nothing
MyCol = Nothing
intCnt = Nothing
End If
Me.DataGrid1.DataSource = MyTable
’刷新
DataGrid1.ResetBindings()
'选择最后一行为选中(行索引从0开始)
If Me.DataGrid1.VisibleRowCount > 0 Then
Me.DataGrid1.CurrentRowIndex = Me.DataGrid1.VisibleRowCount - 1
End If
End If
End Sub
'datagrid列表行指针的移动控制
’定义行指针移动管理器
Private WithEvents CMgr As CurrencyManager
Private Sub CMgr_PositionChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles CMgr.PositionChanged
If CMgr.Position = CMgr.Count - 1 Then
cmdNext.Enabled = False
cmdLast.Enabled = False
Else
cmdNext.Enabled = True
cmdLast.Enabled = True
End If
If CMgr.Position = 0 Then
cmdPrev.Enabled = False
cmdFirst.Enabled = False
Else
cmdPrev.Enabled = True
cmdFirst.Enabled = True
End If
lblRowClicked.Text = CMgr.Position
'lblRow.Text = "Row " & CType(Me.BindingContext(DTbl), CurrencyManager).Position
End Sub
Private Sub cmdCurrencyMangr_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdCurrencyMangr.Click
DTbl = CreateTableMemoryObjectNoPK()
AddDataToDTableFromSQL(DTbl)
‘把表的移动绑定到CurrencyManager
CMgr = CType(Me.BindingContext(DTbl), CurrencyManager)
'Clear any existing TableStyles
CGrid.ClearTableStyles(DataGrid1)
AttachTableStyle2() 'Now Detects Row Change Events In Grid
End Sub
---------------------------------------------------------------------- vb.net对bindingsource/bindingNavigator/dataGridView的绑定------------------------------
参考博客:http://xaobenyu.blog.sohu.com/43341191.html
http://www.cnblogs.com/huangfr/archive/2011/09/06/2168196.html
-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------