C # 28 Data binding

Source: Internet
Author: User

in Windows binding is the interface and data source data consistency, that is, the implementation of the user interface and add and remove changes and database additions and deletions to the same, said the data source index or data table, and the form can be a Windows Form or Web form, here, We study data binding on Windows Forms, generally in two types: simple binding and complex binding. Simple binding is the ability to bind a property of a control to a data element, such as the value of a column in a dataset table, which is a typical binding type for a control such as a TextBox or a label. Complex bindings are the ability to bind a control to multiple data elements, typically bound to multiple records in a database, such as DataGridView, which can be bound to a DataTable, displaying multiple records and values for multiple fields at once.

Focus:

? Mastering the use of DataGridView controls

? DataGridView Common Properties and methods

? Simple binding

? Using bound controls to read data from a data source

Preview Lessons:

? How to use DataGridView

? DataGridView Common Properties and methods

? Simple binding

5.1 DataGridView Controls

In the project development, how to display the data in the database to the interface? The DataGridView control in. NET gives us the ability to display tabular data, in addition to the ability to display data table data directly, the DataGridView control supports sorting, data binding, and the creation of its own cell types, custom control skins, and other advanced features.

The data in the DataGridView control is usually provided using bindings, such as if you can bind the DataGridView control to a data table in the dataset, then the DataGridView control will automatically display the data in that data table, as we do in this way. In addition, the binding function of the DataGridView control is very powerful, it can not only bind the data table, but also bind the dataset, Data View, collection, array, etc.

Properties and methods of 5.1.1 DataGridView

Important properties and events commonly used by DataGridView controls

Property

Description

Columns

The collection of included columns

DataSource

Data sources for DataGridView

ReadOnly

Whether cells can be edited

DataPropertyName

The name of the bound data column

HeaderText

Column header text

Visible

Specifies whether the column is visible

Frozen

Specifies whether the column moves when horizontal scrolling datagridview

ReadOnly

Specifies whether the cell is read-only

ColumnCount

Number of columns displayed in DataGridView

RowCount

Number of rows displayed in DataGridView

Rows

Rows for all controls

CurrentCell

Current cell

CurrentRow

The row of the current cell

Selectedrows

User-selected row

DataMember

Data source bound dataset, here is the name of the data table

DefaultCellStyle

Default appearance style for cells

Event

Description

CurrentCellChanged

Occurs when a cell is clicked

CellContentClick

Occurs when a cell is clicked

You can create a DataGrid control object by dragging a DataGridView control into the form design area from the data card in the Form Design Toolbox window. You can then set the control-related properties in the DataGridView Control Properties window, and in addition to the properties mentioned in the table above, we can see many other commonly used properties in the DataGridView control's Properties window, such as:

Dock properties: You can set how the control fills the parent container

L ReadOnly Property: Sets whether the control is editable, sets false, and the control is not editable

L Rowheadervisible Property: Whether column headings are displayed, column headings are displayed by default

L MultiSelect Property: Allows multiple rows to be selected

5.1.2 binding DataGridView to a DataSet

Bind the DataGridView property to a data source, and by setting its DataSource property, you can bind the DataGridView control directly to a data source, which can be an array, a collection, or a dataset. Binding a DataGridView control to a dataset through the DataSource property typically has three forms:

I. Directly bound data table

Datagridview1.datasource=ds. Tables[0];

Two. Data view of bound data table

Datagridview.datasource=ds. Tables[0]. DefaultView;

Three. Binding to datasets

Datagridview.datasource=ds;

Datagridview.datamember=ds. Tables[0]. TableName;

In the third way, we know that the DataGridView control can only display a single data table at a time, so once you bind a dataset, you must use the DataMember property to specify which table the DataGridView control displays in the dataset. Therefore, set the name of a data table in a dataset to the DataMember property.

5.1.3 Customizing DataGridView Controls

The DataGridView control allows edits by default, supports automatic sorting, supports selection mode, adjusts column spacing, and so on, where the user can click on the column header to sort, sort by the different types of columns, and if the column is a number, sort by size. If they are characters, they are sorted by character order. Users can set their own selection mode, such as single-selection, multi-line selections, so that the data in the list is easy to select. Users can also double-click on the column separator between headings so that the left column automatically expands or shrinks according to the contents of the cell.

Here is a diagram of the DataGridView control's composition (Figure 15.9), which we will discuss in detail about the DataGridView of cell values and the settings for the appearance of cells, rows, columns, and so on.

DataGridViewColumn

rowheaders

columns

 

 
 

DataGridViewRow

 

 

datagridviewcell

 

 

 

 

 

The composition of the DataGridView control is similar to the composition of a table and is simply divided into rows and columns. DataGridView control Each row is a DataGridViewRow object, and each row is divided by columns into a number of cells, and each cell is a DataGridViewCell object. In addition to rows and columns, the DataGridView Control has column and row headings, there are no corresponding classes for column headings and row headings, but there are many properties in the DataGridView control that can set the appearance of these two components.

You can get the data for any cell in the DataGridView control as you would get the data table data, because the collection of rows in the DataGridView control is also rows. In each row of objects (the DataGridViewRow object), there is another cell property that can get a column of the row. You can use the following code to traverse all the data in the DataGridView control:

foreach (DataGridViewRow R in This.dataGridView.Rows)

{

Print each cell value

for (int i=0;i<r.cells.count;i++)

{

Console.WriteLine (R.cells[i]. Value);

}

}

To get the value of a row or column cell you have selected, you need to use the previously mentioned CurrentRow and CurrentCell two properties to return the selected row object and return the selected cell object, respectively. These two properties allow you to find the value of the cell you want. For example:

Privatevoid datagridview_currentcellchanged (Object Sender,eventargs e)

{

Gets the column label of the currently selected cell

intn=this.datagridview.currentcelladdress.x;

Gets the value of the cell whose current row is labeled N

Stringval=this.datagridview.currentrow.cells[n]. Value.tostring ();

Use CurrentCell directly to get the value of the selected cell

Stringval=this.datagridview.currentcell.value.tostring ();

}

The DataGridView control provides the Currentcelladdress property to get the list and row labels of the selected cell, and the following code modifies the value of the item in the corresponding table in the selected cell in the DataGridView:

Privatevoid Datagridview_cellcontentclick (Object Sender,datagridviewcelleventargs e)

{

Get a bound data table

datatabledt= (DataTable) This.dataGridView.DataSource;

int x=datagridview.currentcelladdress.x; Get row labels

int y=datagridview.currentcelladdress.y; Get column Labels

Displays the value of the current cell

MessageBox.Show (This.datagridview.rows[x]. Cells[y]. Value.tostring ());//

Modify the value of the item in the data table for this cell

Dt. rows[x][y]= "Modified value";

Update to Database

Adapter. Update (DT);

Dt. AcceptChanges ();

}

In addition to providing flexible data access and editing capabilities, DataGridView also provides a powerful appearance setting feature that sets the appearance of the DataGridView control in the form of the DataGridView control that is prompted. We can figure out a total of five ways to set the DataGridView's appearance, which are line appearance, column appearance, row header appearance, column header appearance, and total appearance. The following five aspects are discussed below:

N Total Appearance settings

BackgroundColor: Setting its background color

BorderStyle: Set Border Style

Cellborderstyle: Set its cell border style

DefaultCellStyle: Set cell specific styles (font color, font type, alignment, data format, etc.)

Gridcolor: Set Gridline color

N Column Header appearance

Columnheadersborderstyle: Set border style for column headings

Columnheadersdefaultcellstyle: Setting column heading styles

Columnheadersheight: Setting the height of column headings

Columnheadersvisible: Sets whether column headings are displayed

N Row Header appearance

Rowheadersborderstyle: Set the border style for row headings

Rowheadersdefaultcellstyle: Set default row Heading style

Rowheaderswidth: Set the width of row header columns

Rowheadersvisible: Sets whether row headers are displayed

N Rows appearance

Rowtemplate: Set a row template to achieve the purpose of setting the line appearance

Rowsdefaultcellstyle: Sets the font color, font type, how and data format of the line

N Column appearance

The DataGridView control's column appearance is set in its Columns property. Within the Columns property you can set the appearance of each column. Each column contains attributes such as width, ColumnType, DefaultCellStyle, and so on. The Width property of each column allows you to set the column width.

DefaultCellStyle: Sets the properties of the column cell (font color, font type, alignment, data format, and so on)

ColumnType: Sets the type of cell, including six types: Datagridviewbuttoncolumn, Datagridviewcheckboxcolumn, Datagridviewcomboboxcolumn, Datagridviewimagecolumn, Datagridviewtextboxcolumn, Datagridviewlinkcolumn

C # 28 Data binding

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.