DataGridView control is a key control developed by WinForm, which is very convenient for data rendering and operation, and DataGridView is simple and complex. The simple thing is that it integrates many methods, and it is complex to use it for complex data rendering and manipulation.
This article is an entry-level training, first introduced DataGridView simple application, complex application in the subsequent blog post will be presented.
DataGridView is mainly about presenting data and data manipulation, which is naturally inseparable from data.
The first is data binding, the DataGridView data source can be a dataset, DataTable or IList, and so on, as to what datasets, DataTable and IList are, follow-up or apes can find themselves online.
1, first introduce one of the simplest vs integration, do not need to write a line of code can be done with data binding, the following map operation can be
Of course, put a DataGridView on the form, then an arrow on the right, click Select Data Source, add a project data source directly
Here, if there is no data link, you need to create a new data link
Choose a table and field that you want to display in the DataGridView, and when you're done, we'll see the form, the form code, and the solution. There's something more.
There are three things that appear below the form
Private void Form1_Load (object sender, EventArgs e) { // TODO: This line of code loads the data into the table " Testdataset.student "in. You can move or delete it as needed. this. Studenttableadapter.fill (this. testdataset.student); }
Such a piece of code will appear in the form code
A dataset will appear in the solution directory, in fact this dataset is WinForm for us to automatically encapsulate the data set, double-click to see
By the way, it's this stuff that encapsulates the student table and of course provides a TableAdapter GetData method.
OK, the automatic binding data is done, run a look at the effect
It is so simple, do not write a line of code, of course, the table column name needs to be modified, changed to Chinese, or DataGridView right arrow, edit column Pop-up dialog box
Select the left column, modify the HeaderText of the column, we can also see a property datapropertyname, this is the corresponding data source inside the field name. In the future if you write code binding data, you need to specify this property, can be specified here or in the code can be specified.
Of course, this approach is the simplest and most convenient, but in the actual application of the development of the use of relatively few.
From there, we can also see an important attribute ColumnType, what is this, Nikki? Is the DataGridView column display of the data type, there are several types, the introduction of a ha
Class |
Description |
Datagridviewtextboxcolumn |
Used with text-based values, automatically generated when binding to values of numeric and string types |
Datagridviewcheckboxcolumn |
used with Boolean and checkstate values, automatically generated when binding to values of these types |
Datagridviewimagecolumn |
Used to display images that are automatically generated when bound to a byte array, animage object, or an Icon object |
Datagridviewbuttoncolumn |
Used to display a button in a cell, is not automatically generated at binding, and is typically used as an unbound column |
Datagridviewcomboboxcolumn |
The user displays a drop-down list in a cell that is not automatically generated at the time of binding and is typically manually data-bound |
Datagridviewlinkcolumn |
Used to display hyperlinks in cells that are not automatically generated at bind time, often requiring manual data binding |
Choose according to the form we need to display in the column.
WinForm simple data binding for developing DataGridView of common controls-automatic binding