天天看點

VB.net對于datagride控件的使用及C#中datagrideView\bindingSource\bindingNavigator\tabcontrol控件的應用

                //綁定資料源

                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

-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------