"Go" DataGridView several ways to bind a data source

Source: Internet
Author: User

The first type:



The second type:
DataTable dt=new


The third type:



The fourth type:


This.dataGridView1.DataMember = "table name";

The fifth type:
ArrayList Al = new ArrayList ();


The sixth type:



The seventh type:
DataView dv = new DataView ();
This.dataGridView1.DataSource = DV;

The eighth type:
This.dataGridVi.DataSource = new bindinglist<object> (list<object>);


Non-binding mode (ref:http://blog.csdn.net/yudandan10/article/details/9618763)

The so-called unbound pattern is that the data displayed by the DataGridView control is not from the bound data source, but instead can be populated manually by code into the DataGridView control, which is DataGridView control adds a lot of flexibility

Let's take a look at the DataGridView control has several types of columns , and these types are indirect or directly inherit the DataGridViewColumns () class , here are some of the types we can use long

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 bind time and is typically received for data binding

Datagridviewlinkcolumn

Used to display hyperlinks in cells that are not automatically generated at bind time, often requiring manual data binding

See the example below

  1. //'Create a column that displays a textbox ()Dim col1 as Datagridviewtextboxcolumn =New Datagridviewtextboxcolumn (); Col1. Name="Name"; col1. HeaderText="name";//'set the text that appears in the captionDim col3 as Datagridviewtextboxcolumn=Newdatagridviewtextboxcolumn (); Col3. Name="Sex"; col3. HeaderText="Sex";//'to add a new column to the controlDataGridView1.Columns.Add (col1);   DATAGRIDVIEW1.COLUMNS.ADD (COL3); //'Adding Rows//'Create a new rowDim row as DataGridViewRow =Newdatagridviewrow (); Row. Createcells (DATAGRIDVIEW1); //'set the value of a cellRow. Cells (0). Value ="Zhang San"; row. Cells (1). Value ="male"  ; DATAGRIDVIEW1.ROWS.ADD (row); //'Add a second rowIm Row1 as String () = {"John Doe","male"}  ; DATAGRIDVIEW1.ROWS.ADD (ROW1);

In addition, you can add a header column without code, but manually add columns by adding the following steps :

1, click on the DataGridView control to see a small triangle, click the small triangle pop-up menu Select the Edit column as shown below

Edit columns:

You can also modify the properties of a column

"Go" DataGridView several ways to bind a data source

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.