[★] Issues needing attention in the real project of the datagridview

Source: Internet
Author: User

Datagridview:

I:
Binding data to the datagridview control is simple and intuitive. In most cases, you only need to set the datasource attribute.
When binding to a data source that contains multiple lists or tables, you only need to set the datamember attribute to the string of the list or table to be bound.

Ii. Data Source Definition

Any class that implements the ilist interface, including a one-dimensional array.
Any class that implements the ilistsource interface, such as the datatable and dataset classes.
Any class that implements the ibindinglist interface, such as the bindinglist class.
Any class that implements the ibindinglistview interface, such as the bindingsource class.

3. bindingsource component: replace dataset and dataview with XI or excellent ..

Bindingsource is usually bound to the bindingsource component, bindingsource is bound to another data source, or this component is filled with business objects.
The bindingsource component is the preferred data source, because it can be bound to various data sources and automatically solves many data binding problems. In actual projects, you usually need to pay attention to the following attributes:

The bindingsource component has two purposes. First, it provides an indirect layer to bind controls on the form to the data. This is done by binding the bindingsource component to the data source and then binding the controls on the form to the bindingsource component. Any further interaction with the data (including navigation, sorting, filtering, and updating) is done by calling the bindingsource component.

Use bindingsource to filter data
Set the filter attribute to the expected expression.
In the followingCodeIn the example, the expression is to add the value required for the column after the column name.
Bindingsource1.filter = "contacttitle = 'owner '"

Sort data using bindingsource
Set the sort attribute to the column names to be followed by ASC or DESC to indicate whether the column names are sorted in ascending or descending order.
Multiple columns are separated by commas.
Bindingsource1.sort = "country DESC, address ASC ";

== enable line breaks in text-based cells
This. datagridview1.defaultcellstyle. wrapmode =
datagridviewtristate. true;
== set the format of the currency and date value
This. datagridview1.columns ["unitprice"]. defaultcellstyle. format = "C"; currency
This. datagridview1.columns ["shipdate"]. defaultcellstyle. format = "D"; short date
==< br> in actual projects, you only need to pay attention to these attributes ..
allowusertoaddrows indicates whether to display the option used to Add rows to the user.
Use columnheadersheight with columnheadersheightsizemode .. used to set the column height
rowheadersvisible indicates whether to display a column containing the row title
selectionmode indicates how to select a cell of the datagridview
allowusertoresizerows indicates whether the user can adjust the row size
background color of the backgroudcolor datagridview
readonly datagridview is read-only
editmode: determines the mode in which the cell editing starts.
autosizerowsmode determines the automatic adjustment mode of visible rows.
allowusertodeleterows indicates whether to allow users to delete rows from the daagridview
columnheadefaultcellstyle default Column Title style

Binding method:

1. automatically generate columns 2. Bind columns [visible, read-only, frozen]

After a column is frozen,. Frozen = true. Columns starting from the column to the left will be affected.
For example, in column a B c d e, the frozen attribute set for column E to frozen = true a B c d is automatically set to true without a horizontal scroll bar .. and scrollbars is preferred.
After Column B is set to frozen... column a B does not move. When you drag the horizontal scroll bar, c d e moves. It seems to be two layers...

If the column is automatically generated... we have nothing to say... if the column is bound. If you bind the field to Table A, the query is table B. No results .....
When you bind a column, a column will still be automatically created. You can cancel the automatic column creation method:
SlaveProgramAngle: We generally use *... to change the query result to a specific column to avoid automatic generation of more columns ..

Then obtain the value of a column in the current row: maid. currentrow. cells [I]. value. tostring ();

 

 

 

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.