ADO Data Grid - C Sharp Programming - Lecture Slides, Slides of C Sharp Programming

This programming course teaches different programming concepts with respect to C Sharp Programming. Key points of this lecture are: Ado Data Grid, Datagridview Control, Capabilities, Binding, Formatting, Columns Editor, Row Events, Cell Events, Dataerror Event, Executescalar

Typology: Slides

2012/2013

Uploaded on 09/27/2013

vikrant
vikrant 🇮🇳

4.4

(9)

119 documents

1 / 20

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
ADO.NET Part 2
docsity.com
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14

Partial preview of the text

Download ADO Data Grid - C Sharp Programming - Lecture Slides and more Slides C Sharp Programming in PDF only on Docsity!

  • ADO.NET Part

Overview

The DataGridView Control

(Capabilities)

 Displays rows from a single table (DataTable) or view  Allows editing (add, change, delete)  A rich set of events for error handling  A rich set of properties and objects for formatting

The DataGridView Control

(Binding)

 Set the DataSource and DataMember properties  These can be  A DataSet (DataSource) and DataTable (DataMember)  A BindingSource (DataSource – the DataMember is not set)

The DataGridView Control

(Columns Editor)

DataGridView

(Row Events)

 In all, there are about 100 different events  Only selected events are of interest to us  RowEnter fires as a row gets input focus  RowLeave fires as a row loses focus  RowValidating fires after RowLeave and provides the chance for validation  This event can be cancelled

DataGridView

(Row Events 3)

 See tblLocationDataGridView event handlers in IS389ADOPart 2

DataGridView

(Cell Events 1)

 CellEnter fires when a cell gets focus  CellBeginEdit fires when editing begins  CellEndEdit fires when editing ends  CellLeave fires after end edit when cell loses focus

Using the OleDbCommand

Object (ExecuteScalar)

 Remember the ExecuteScalar method returns a single value  Use to get a max ID value

ExecuteScalar (Example)

 Get Max of field fldBoreHoleID in tblBoreHoleData System.Data.OleDb.OleDbCommand oCmd = new System.Data.OleDb.OleDbCommand(); oCmd.CommandType = CommandType.Text; oCmd.CommandText = "SELECT MAX(fldBoreHoleID) FROM tblBoreholeData"; oCmd.Connection = tblBoreholeDataTableAdapter.Connection; oCmd.Connection.Open(); int i; i = (int) oCmd.ExecuteScalar(); oCmd.Connection.Close(); docsity.com

Using the OleDbCommand

Object with Parameters

 Unlike the previous example, we generally don’t want to hardcode the data values  This is where parameters come in  Add one parameter for each dynamic field

Creating a Parameter

 An OleDbParameter has a  Name  Data type  Size  Mapping field  Example: System.Data.OleDb.OleDbParameter prop = new System.Data.OleDb.OleDbParameter( "fldCASIndexNumber",System.Data.OleDb.O leDbType.Integer,0,"fldCASIndexNumber") ;

Filling in the Parameter Values

 Set the Value property of a particular parameter oCmd.Parameters["fldCASIndexNumber"].Value = 234; oCmd.Parameters["fldChemicalName"].Value = "XXX"; oCmd.Parameters["fldCASRegNo"].Value = "YYY"; oCmd.Parameters["fldCarcinogenic"].Value = true;

A Parameterized insert

statement

 The INSERT statement looks like this

oCmd.CommandText = "INSERT INTO tblCasIndex (fldCASIndexNumber, fldChemicalName, fldCASRegNo, fldCarcinogenic) VALUES (?,?,?,?)";