Presentation is loading. Please wait.

Presentation is loading. Please wait.

Introduction to Microsoft Windows Forms DataGrid Control.

Similar presentations


Presentation on theme: "Introduction to Microsoft Windows Forms DataGrid Control."— Presentation transcript:

1 Introduction to Microsoft Windows Forms DataGrid Control

2 2 Agenda Introduction to Microsoft® Windows® Form DataGrid Introduction to Microsoft® Windows® Form DataGrid Microsoft Visual Basic® 6.0 DataGrid vs. Windows Forms DataGrid Microsoft Visual Basic® 6.0 DataGrid vs. Windows Forms DataGrid Properties, methods, and events Properties, methods, and events Databinding and navigation Databinding and navigation Managing DataGrid appearance Managing DataGrid appearance Capturing clicks Capturing clicks Adding controls Adding controls Frequently asked DataGrid questions Frequently asked DataGrid questions Additional references Additional references Questions Questions

3 3 Introduction to Windows Form DataGrid Displays data in a series of rows and columns Displays data in a series of rows and columns User interface for a dataset User interface for a dataset Hierarchical navigation Hierarchical navigation Formatting and editing capabilities Formatting and editing capabilities NOTE: The control handles the user interface, while data updates are handled by the Windows Forms databinding architecture and by ADO.NET data providers NOTE: The control handles the user interface, while data updates are handled by the Windows Forms databinding architecture and by ADO.NET data providers

4 4 Visual Basic 6.0 DataGrid vs. Windows Forms DataGrid Microsoft.NET DataGrid control does not need data-specific methods or events because all actions are performed through the data source Microsoft.NET DataGrid control does not need data-specific methods or events because all actions are performed through the data source Visual Basic 6.0 DataGrid Visual Studio.NET DataGrid AllowAddNew Accessed through AllowNew property of DataView class AllowDelete Accessed through AllowDelete property of DataView class AllowUpdate Accessed through AllowEdit property of DataView class ApproxCount Returned by BindingContext object; accessed through Count property of the BindingManagerBase class Bookmark Data is disconnected and items can be accessed directly FirstRow Set Position property to 0 on BindingManagerBase object CurrentCellModified RowState property on DataRow object

5 5 Properties, Methods, and Events Properties Properties CurrentCell CurrentCell CurrentRowIndex CurrentRowIndex DataSource DataSource DataMember DataMember PreferredColumnWidth PreferredColumnWidth PreferredRowHeight PreferredRowHeight TableStyles * TableStyles *

6 6 Properties, Methods and Events (2) Methods Methods SetDataBindings SetDataBindings BeginEdit BeginEdit EndEdit EndEdit GetCurrentCellBounds GetCurrentCellBounds HitTest * HitTest * Select Select Unselect Unselect Events Events CurrentCellChanged CurrentCellChanged KeyDown, KeyPress, KeyUp KeyDown, KeyPress, KeyUp ShowParentDetails ShowParentDetails

7 7 Windows Forms DataBinding Architecture Databinding in Windows Forms permits you to display and make changes to information from a data source in controls on the form Databinding in Windows Forms permits you to display and make changes to information from a data source in controls on the form You can bind not only to conventional data sources, but also to almost any structure that contains data You can bind not only to conventional data sources, but also to almost any structure that contains data You can bind any property of any control to the DataSource You can bind any property of any control to the DataSource

8 8 Providers of Data to Windows Forms DataTable DataTable DataView DataView DataSet DataSet DataViewManager DataViewManager Single dimensional array Single dimensional array

9 9 Design Time Databinding Bind DataGrid control to a data source using the DataSource and DataMember properties Bind DataGrid control to a data source using the DataSource and DataMember properties

10 10 Sample Code: Run Time Databinding Imports System.Data.SqlClient Create a DataSet Object ds = New DataSet("Customers") Dim cString As string = ConnectionString Dim cn As SqlConnection = New SqlConnection(cString) cn.Open() Dim cmd As SqlCommand = New SqlCommand("SELECT * FROM Suppliers") cmd.Connection = cn cmd.CommandType = CommandType.Text Dim ad As SqlDataAdapter = New SqlDataAdapter() ad.SelectCommand = cmd ' A table mapping tells the adapter what to call the table. ad.TableMappings.Add("Table", "Suppliers") ad.Fill(ds) DataGrid1.SetDataBinding(ds, "Suppliers")

11 11 Master-Details Lists

12 12 Sample Code: Master-Details Lists You can use two DataGrids to display data in a Master-Detail format You can use two DataGrids to display data in a Master-Detail format Sample code: Dim DR As DataRelation DR = New DataRelation("CustOrd", _ ds.Tables("Customers").Columns("CustomerID"), _ ds.Tables("Orders").Columns("CustomerID")) ' Add the relation to the DataSet. ds.Relations.Add(DR)GridCustomers.SetDataBinding(ds,"Customers")GridOrders.SetDataBinding(ds,"Customers.CustOrd")

13 13 Parent-Child Relationship

14 14 Sample Code: Parent-Child Relationship Dim relcustorder As DataRelation Dim relcustorder As DataRelation Dim parentcol, childcol As DataColumn Dim parentcol, childcol As DataColumn Dim cn As SqlConnection = New SqlConnection(ConnString) Dim cn As SqlConnection = New SqlConnection(ConnString) cn.Open() cn.Open() Dim DA As SqlDataAdapter = New SqlDataAdapter("Select * FROM Customers;Select * from Orders", cn) Dim DA As SqlDataAdapter = New SqlDataAdapter("Select * FROM Customers;Select * from Orders", cn) DA.TableMappings.Add("Customers1", "Orders") DA.TableMappings.Add("Customers1", "Orders") DA.Fill(MyDS, "Customers") DA.Fill(MyDS, "Customers") ' Define Columns and Create Relationship ' Define Columns and Create Relationship Parentcol = MyDS.Tables("Customers").Columns("CustomerID") Parentcol = MyDS.Tables("Customers").Columns("CustomerID") childcol = MyDS.Tables("Orders").Columns("CustomerID") childcol = MyDS.Tables("Orders").Columns("CustomerID") relcustorder = New DataRelation("CustomersOrders, parentcol, childcol) relcustorder = New DataRelation("CustomersOrders, parentcol, childcol) ' Add the relation to the DataSet. ' Add the relation to the DataSet. MyDS.Relations.Add(relcustorder) MyDS.Relations.Add(relcustorder) DataGrid1.DataSource = MyDS.Tables("Customers") DataGrid1.DataSource = MyDS.Tables("Customers")

15 15 Currency Manager

16 16 Currency Manager (2) Keeps track of the position and otherwise supervises bindings to that data source Keeps track of the position and otherwise supervises bindings to that data source Each DataSource is associated with a CurrencyManager Each DataSource is associated with a CurrencyManager If the controls on the form all bind to a single source, then they will share the same CurrencyManager If the controls on the form all bind to a single source, then they will share the same CurrencyManager Position property of CurrencyManager Position property of CurrencyManager

17 17 Sample Code: Currency Manager Private myCM As CurrencyManager Private Sub BindControl(myTable As DataTable) ' Bind a TextBox control to a DataTable column in a DataSet. TextBox1.DataBindings.Add("Text", myTable, "CompanyName") ' Specify the CurrencyManager for the DataTable. myCM = CType(me.BindingContext(myTable), CurrencyManager) ' Set the initial Position of the control. myCM.Position = 0 End Sub Code for Navigation Private Sub MoveNext(myCM As CurrencyManager) If myCM.Position = myCM.Count - 1 Then MessageBox.Show("You're at end of the records") If myCM.Position = myCM.Count - 1 Then MessageBox.Show("You're at end of the records")Else myCM.Position += 1 myCM.Position += 1 End If End Sub

18 18 Sample Code: Currency Manager (2) Private Sub MoveFirst(myCM As CurrencyManager) myCM.Position = 0 End Sub Private Sub MovePrevious(myCM As CurrencyManager) If myCM.Position = 0 Then MessageBox.Show("You're at the beginning of the records.") MessageBox.Show("You're at the beginning of the records.")Else myCM.Position -= 1 myCM.Position -= 1 End if End Sub Private Sub MoveLast(myCM As CurrencyManager) Private Sub MoveLast(myCM As CurrencyManager) myCM.Position = myCM.Count - 1 End Sub

19 19 Managing DataGrid Appearance System.Windows.Forms.DataGrid DataGridTableStyle Class DataGridColumnStyle Class DataGridTextBoxColumn Class DataGridTextBox Class DataGridBoolColumnClass

20 20 Adding TableStyle From the Designer

21 21 Adding ColumnStyles From the Designer

22 22 DataGrid Default and Custom TableStyle

23 23 DataGridBoolColumn Class Specifies a column in which each cell contains a check box for representing a Boolean value Specifies a column in which each cell contains a check box for representing a Boolean value Sample code: Dim ts1 As New DataGridTableStyle() ts1.MappingName = "Items ts1.MappingName = "Items ts1.AlternatingBackColor = Color.LightBlue ts1.GridLineStyle = DataGridLineStyle.Solid ts1.GridLineStyle = DataGridLineStyle.Solid ts1.HeaderBackColor = Color.Blue ts1.HeaderBackColor = Color.Blue ts1.HeaderForeColor = Color.White ts1.HeaderForeColor = Color.White Dim boolCol As New DataGridBoolColumn() boolCol.MappingName = "Available" boolCol.HeaderText = "Is Item Available" boolCol.Width = 100 ts1.GridColumnStyles.Add(boolCol)

24 24 DataGridTextBoxColumn Class DataGridTextBoxColumn Class hosts a TextBox control in a cell of a DataGridColumnStyle for editing text DataGridTextBoxColumn Class hosts a TextBox control in a cell of a DataGridColumnStyle for editing text DataGridTextBox Class represents a TextBox control that is hosted in a DataGridTextBoxColumn DataGridTextBox Class represents a TextBox control that is hosted in a DataGridTextBoxColumn Sample code: Dim TextCol As New DataGridTextBoxColumn() Dim TextCol As New DataGridTextBoxColumn() Set the MappingName to the DataColumn Name Set the MappingName to the DataColumn Name TextCol.MappingName = "ItemDate" TextCol.MappingName = "ItemDate" TextCol.HeaderText = "Item Date" TextCol.HeaderText = "Item Date" TextCol.Format = "dd/mm" TextCol.Format = "dd/mm" TextCol.Width = 100 TextCol.Width = 100 Add the TextBoxColumn to ColumnStyles collection Add the TextBoxColumn to ColumnStyles collection ts1.GridColumnStyles.Add(TextCol) ts1.GridColumnStyles.Add(TextCol) 'Add the DataGridTableStyle objects to the collection. 'Add the DataGridTableStyle objects to the collection. MyDataGrid.TableStyles.Add(ts1) MyDataGrid.TableStyles.Add(ts1)

25 25 Capturing Clicks: HitTestInfo Class

26 26 Sample Code: HitTest Method HitTestInfo class contains information about a part of the DataGrid at the specified coordinate HitTestInfo class contains information about a part of the DataGrid at the specified coordinate Private Sub MyDataGrid_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles MyDataGrid.MouseDown Dim myHitTest As DataGrid.HitTestInfo Dim Result As String ' Use the HitTest method with the x and y properties. ' Use the HitTest method with the x and y properties. myHitTest = MyDataGrid.HitTest(e.X, e.Y) myHitTest = MyDataGrid.HitTest(e.X, e.Y) MsgBox("Hit Test Column =" & myHitTest.Column ) MsgBox("Hit Test Column =" & myHitTest.Column ) MsgBox("Hit Test Row =" & myHitTest.Row) MsgBox("Hit Test Row =" & myHitTest.Row) End Sub End Sub

27 27 Determining Current Cell Information Use CurrentCell property in CurrentCellChanged event to get the column and row number Use CurrentCell property in CurrentCellChanged event to get the column and row number Sample code: Private Sub myDataGrid_CurrentCellChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles myDataGrid.CurrentCellChanged MessageBox.Show("Column is " & myDataGrid.CurrentCell.ColumnNumber _ & ", Row is " & myDataGrid.CurrentCell.RowNumber _ & ", Value is " & myDataGrid.Item(myDataGrid.CurrentCell)) End Sub

28 28 Adding Controls to DataGrid

29 29 Sample Code: Adding Controls to DataGrid Public MyCombo As New ComboBox() Form_load AddHandler MyCombo.TextChanged, AddressOf Ctrls_TextChanged MyCombo.Name = "MyCombo" MyCombo.Visible = False DataGrid1.PreferredRowHeight = MyCombo.Height DataGrid1.Controls.Add(MyCombo) DataGrid1_Paint Event If DataGrid1.CurrentCell.ColumnNumber = 3 Or DataGrid1.CurrentCell.ColumnNumber = 4 Then MyCombo.Width = DataGrid1.GetCurrentCellBounds.Width MyCombo.Width = DataGrid1.GetCurrentCellBounds.Width End If Sub Ctrls_TextChanged If DataGrid1.CurrentCell.ColumnNumber = 3 Or DataGrid1.CurrentCell.ColumnNumber = 4 Then DataGrid1.Item(DataGrid1.CurrentCell) = MyCombo.Text DataGrid1.Item(DataGrid1.CurrentCell) = MyCombo.Text End If

30 30 Sample Code: Adding Controls to DataGrid (2) Private Sub DataGrid1_CurrentCellChanged 'Combo Control Access If DataGrid1.CurrentCell.ColumnNumber = 3 Then MyCombo.Visible = False MyCombo.Visible = False MyCombo.Width = 0 MyCombo.Width = 0 MyCombo.Left = DataGrid1.GetCurrentCellBounds.Left MyCombo.Left = DataGrid1.GetCurrentCellBounds.Left MyCombo.Top = DataGrid1.GetCurrentCellBounds.Top MyCombo.Top = DataGrid1.GetCurrentCellBounds.Top MyCombo.Text = DataGrid1.Item(DataGrid1.CurrentCell) & "" MyCombo.Text = DataGrid1.Item(DataGrid1.CurrentCell) & "" MyCombo.Items.Clear() MyCombo.Items.Clear() MyCombo.Items.Add("Sales Representative") MyCombo.Items.Add("Sales Representative") MyCombo.Items.Add("Inside Sales Coordinator") MyCombo.Items.Add("Inside Sales Coordinator") MyCombo.Items.Add("Vice President, Sales") MyCombo.Items.Add("Vice President, Sales") MyCombo.Items.Add("Sales Manager") MyCombo.Items.Add("Sales Manager") MyCombo.Visible = True MyCombo.Visible = TrueElse MyCombo.Visible = False MyCombo.Visible = False MyCombo.Width = 0 MyCombo.Width = 0 End If

31 31 Frequently Asked DataGrid Questions How to implement a ComboBox inside DataGrid How to implement a ComboBox inside DataGrid How to hide a column in DataGrid How to hide a column in DataGrid How to customize the DataGrid view How to customize the DataGrid view How to do paging in DataGrid How to do paging in DataGrid How to prevent a new row in DataGrid How to prevent a new row in DataGrid How to select and whole row when clicked on a cell How to select and whole row when clicked on a cell How to color individual cells based on their values How to color individual cells based on their values How to restrict keystrokes in a column How to restrict keystrokes in a column How to programmatically determine the selected rows How to programmatically determine the selected rows How to bind an array to DataGrid How to bind an array to DataGrid

32 32 Additional References Knowledge Base articles Knowledge Base articles Q308052, HOW TO: Display Parent/Child Records in DataGrid Using VB.NET Q308052, HOW TO: Display Parent/Child Records in DataGrid Using VB.NETHOW TO: Display Parent/Child Records in DataGrid Using VB.NETHOW TO: Display Parent/Child Records in DataGrid Using VB.NET Q305271, HOW TO: Perform Paging with DataGrid Control by Using VB.NET Q305271, HOW TO: Perform Paging with DataGrid Control by Using VB.NETHOW TO: Perform Paging with DataGrid Control by Using VB.NETHOW TO: Perform Paging with DataGrid Control by Using VB.NET Q320566, HOW TO: Enable DataTable Editing by Using DataGrid in VB.NET Q320566, HOW TO: Enable DataTable Editing by Using DataGrid in VB.NETHOW TO: Enable DataTable Editing by Using DataGrid in VB.NETHOW TO: Enable DataTable Editing by Using DataGrid in VB.NET Q319082, HOWTO: Extend Windows Form DataGridTextBoxColumn to Display Data Q319082, HOWTO: Extend Windows Form DataGridTextBoxColumn to Display DataHOWTO: Extend Windows Form DataGridTextBoxColumn to Display DataHOWTO: Extend Windows Form DataGridTextBoxColumn to Display Data Q318604, HOW TO: Populate Datagrid on Bkgd Thread w/Data Binding (VB) Q318604, HOW TO: Populate Datagrid on Bkgd Thread w/Data Binding (VB)HOW TO: Populate Datagrid on Bkgd Thread w/Data Binding (VB)HOW TO: Populate Datagrid on Bkgd Thread w/Data Binding (VB) Q318581, HOW TO: Extend the Windows Form DataGridTextBoxColumn to custom format data Q318581, HOW TO: Extend the Windows Form DataGridTextBoxColumn to custom format dataHOW TO: Extend the Windows Form DataGridTextBoxColumn to custom format dataHOW TO: Extend the Windows Form DataGridTextBoxColumn to custom format data Q317951, HOW TO: Hide a Column in a Windows Form DataGrid Q317951, HOW TO: Hide a Column in a Windows Form DataGridHOW TO: Hide a Column in a Windows Form DataGridHOW TO: Hide a Column in a Windows Form DataGrid Q317041, HOW TO: Retrieve DataView of Bound Control in Visual Basic.NET Q317041, HOW TO: Retrieve DataView of Bound Control in Visual Basic.NETHOW TO: Retrieve DataView of Bound Control in Visual Basic.NETHOW TO: Retrieve DataView of Bound Control in Visual Basic.NET Q313482, INFO: Roadmap for Windows Forms Data Binding Q313482, INFO: Roadmap for Windows Forms Data BindingINFO: Roadmap for Windows Forms Data BindingINFO: Roadmap for Windows Forms Data Binding Q308070, HOW TO: Implement a Searchable DataGrid by Using ADO.NET Q308070, HOW TO: Implement a Searchable DataGrid by Using ADO.NETHOW TO: Implement a Searchable DataGrid by Using ADO.NETHOW TO: Implement a Searchable DataGrid by Using ADO.NET Q323167, HOW TO: Add ComboBox Control to Windows Form DataGrid Control Q323167, HOW TO: Add ComboBox Control to Windows Form DataGrid ControlHOW TO: Add ComboBox Control to Windows Form DataGrid ControlHOW TO: Add ComboBox Control to Windows Form DataGrid Control External links External links http://www.syncfusion.com/FAQ/WinForms/default.asp http://www.syncfusion.com/FAQ/WinForms/default.asp http://www.syncfusion.com/FAQ/WinForms/default.asp http://www.datagridcolumnstyles.net/ http://www.datagridcolumnstyles.net/ http://www.datagridcolumnstyles.net/


Download ppt "Introduction to Microsoft Windows Forms DataGrid Control."

Similar presentations


Ads by Google