C# development-data table control data binding (15.12)

I. Overview

The data table control is an important control used in the WinForm form application to display data in the form of a table when querying. Similarly, the data table control can also use visual data binding and code to bind the data in the data table, and can The data table control realizes the modification and deletion of the data in the table.

The following respectively introduces the use of visual data binding to bind data table controls and the use of code to bind data table controls.

<!--more-->

Two visually bind the DataGridView control

The visual data binding of the data grid control is also completed through the task menu of the control. As shown in the figure below, "Select Data Source", "Edit Column", "Add Column" and "Enable Add" are provided in the "DataGridView Task" menu. "Enable editing", "Enable delete", "Enable column reordering", "Docking in parent container" and other options

 

among them:

  • Select the data source: The operation is the same as that of selecting the data source in the combo box control.

  • Edit column: used to edit columns in the DataGridView control, including operations such as adding columns and setting aliases for columns

  • Add column: used to add columns to the DataGridView control, and different types of controls can be added to the DataGridView control to display the newly added columns, for example, add a button to modify or delete data in the table

  • Enable add: Allow users to add a row to the DataGridView control, which is equivalent to setting the AllowUserToAddRows property in the DataGridView control to True

  • Enable editing: Allow users to edit the value in the DataGridView control, which is equivalent to setting the Readonly property in the DataGridView control to False

  • Enable delete: Allow users to delete the value in the DataGridView control, which is equivalent to setting the AllowUserToDeleteRows property in the DataGridView control to True

  • Enable column reordering: Allow manual column resetting, which is equivalent to setting the AllowUserToOrderColumn property in the DataGridView control to True

  • Dock in the parent container: Allow the DataGridView control to be maximized in the form where it is located

The following examples demonstrate how to bind the DataGridView control visually

Three use code to bind the DataGridView control

When using code to bind the DataGridView control, you need to set the DataSource property for the control. The specific statement is as follows.

The name of the DataGridView control. DataSource = DataTable object;

If you use the DataSet object to assign a value to the DataSource property, you need to use the Tables property of the DataSet object to select the specified data table.

Four examples

4.1 Example 1 Use the visual binding method to display the professional number and professional name in the professional information table in the DataGridView control, and set aliases for the column names in the table

4.1.1 Form design

Create a Windows Form application and add a form for displaying professional information, as shown in the following figure

 

4.1.2 Set the data source for the form

In the interface shown in the figure above, click the " Button" button on the DataGridView control, and set the data source for the DataGridView control in the select data source combo box in the pop-up "DataGridView Task" menu.

The method of selecting the data source is the same as the method of selecting the data source in the combo box introduced in the previous section C# ComboBox. Since the data source has been set for the application in the previous example of using the combo box, here is the DataGridView control The data source can directly select the data source that has been set.

After selecting the data source, the effect of the "DataGridView Task" menu is shown in the figure below

4.1.3 Edit column

Click "Edit Column" in the interface shown in the figure above, and the dialog box shown in the figure below will pop up

On the left side of the dialog box, all the columns in the major information table (major) are listed, and the column attributes are listed on the right side. Common attributes are shown in the following table

Attribute name Description
Frozen Set whether the column moves when the user scrolls the DataGridView control horizontally, the default is False
ColumnType Set the control type when displaying the column, the default is a text box
DataPropertyName Set the columns in the bound data source
HeaderName Set the column headings displayed in the DataGridView column
Visible Set whether the column is visible

In the dialog box shown in the figure above, set the header (HeaderText) property of the id column to "number", and set the header (HeaderText) property of the name column to "professional name"

4.1.4 Operation effect

 

4.1.4 Description

It can be seen from the above running effect that the use of visual data binding can quickly complete the operation of displaying the data in the data table in the DataGridView control, and can easily set the properties of the bound column

4.2 Example 2 Use code to bind the DataGridView control

4.2.1 Code

//Form loading event
private void DataGridViewForm_Load(object sender, EventArgs e)
{
    //Database connection string
    string connStr = "Data Source=.;Initial Catalog=test;User ID=sa;Password=root";
    //Create an instance of SqlConnection
    SqlConnection conn = null;
    try
    {
        conn = new SqlConnection(connStr);
        //Open the database
        conn.Open();
        string sql = "select * from major";
        //Create an object of the SqlDataAdapter class
        SqlDataAdapter sda = new SqlDataAdapter(sql, conn);
        //Create an object of the DataSet class
        DataSet ds = new DataSet();
        //Use the SqlDataAdapter object sda to fill the new search results into the DataSet object ds
        sda.Fill(ds);
        //Set the DataSource property of the table control
        dataGridView1.DataSource = ds.Tables[0];
    }
    catch (Exception ex)
    {
        MessageBox.Show("An error occurred!" + ex.Message);
    }
    finally
    {
        if (conn != null)
        {
            //Close the database connection
            conn.Close();
        }
    }
}

4.2.2 Description

It can be seen from the above running effect that the DataGridView control can be bound by setting the DataSource property of the DataGridView control, but the title in the bound DataGridView control is the column name in the data table.

If you need to change the column headings of the DataGridView control, you need to add the following code to the above code.

//Set the column header of the first column
dataGridView1.Columns[0] .HeaderText="Number";
//Set the column header of the second column
dataGridView1. Columns[1].HeaderText="Professional Name";

Guess you like

Origin blog.csdn.net/Calvin_zhou/article/details/108080267