) Dynamically manage ASP. NET DataGrid data Columns

Source: Internet
Author: User
Dynamic Management of ASP. NET DataGrid data Columns
Author: younther Source: mobile network pioneer
In ASP. NET's DataGrid data display control programming, we can add DataGrid columns in several ways. The most common method is to add it in the web forms designer. Drag the DataGrid Control in the control toolbox to the web design page, and then add columns in the property generator; another way is to change HTML in HTML view mode. Code To add columns. However, both methods are implemented during design and cannot be changed once the design is complete. In fact, we can also Program Dynamically add or delete columns during running. In this article Article I will show you how to program and dynamically add and delete columns at runtime. In fact, this is achieved by hiding or actually using columns.

The columns attribute of the DataGrid is the key to accessing the DataGrid columns. The returned result is a collection object such as datagridcolumncollection, which contains all the datagrigcolumn objects. Datagridcolumncollection provides a method to add a datagrigcolumn object and delete an existing datagrigcolumn object ., We will use the Add method of the datagridcolumncollection to add a datagrigcolumn object, so as to dynamically Add a column to the DataGrid at runtime. A datagrigcolumn represents a column of the DataGrid. The visible attribute of the DataGrid is used to show or hide a column.

Now let's create a dynamicdatagrid C # ASP. NET project with me. It has the options to hide and display each column of the DataGrid.

In the web application I created with vs. net, I drag and drop a panel control on the design page. On this panel control, I placed a DataGrid Control, a dropdownlist control, and two buttons controls used to change the properties of the DataGrid Control. The final design interface looks like this.

 

 

Now we can create two methods: filldatagrid () and fillcolumnslist. Filldatagrid () is used to add a column to the DataGrid Control and fill it with the dataset data source. Here, I use the DB. getdataset () method to retrieve dataset. You can refer to the attachedSource codeFile (db. CS) for more details.

the following code describes the implementation of createdatagrid. The Code shows that I have created three columns and bound them to the ID, name, and address fields of dataset with the datafield attribute of boundcolumn. The boundcolumn class inherits from the datagridcolumn class.

private void createdatagrid ()
{< br> // set DataGrid properties
datagrid1.autogeneratecolumns = false;

// get a DataSet object filled with data
dataset DS = dB. getdataset ();

// create ID column & add to DataGrid
boundcolumn Col = new boundcolumn ();
Col. headertext = "User ID";
Col. datafield = "ID";
datagrid1.columns. add (COL);

// create name column & add to DataGrid
Col = new boundcolumn ();
Col. headertext = "User Name";
Col. datafield = "name";
datagrid1.columns. add (COL);

// create address column & add to DataGrid
Col = new boundcolumn ();
Col. headertext = "User address";
Col. datafield = "Address";
datagrid1.columns. add (COL);

// DataGrid Data Binding
datagrid1.datasource = Ds. tables [0];
datagrid1.databind ();
}

The fillcolumnslist () method simply reads column names from the DataGrid and fills these columns in the drop-down list of the dropdownlist control. We will use the dropdownlist control to select hidden or displayed columns.

Private void fillcolumnslist (DataGrid grid)
{
Foreach (maid in grid. columns)
{
Columnslist. Items. Add (Col. headertext );
}
}

Next, we will add the hidedatagridcolumn () method to display or hide a column by using two parameters: Index and bool value. Here, I simply set the visible attribute of columns to true or false.

Private void hidedatagridcolumn (INT index, bool show)
{
Datagrid1.columns [Index]. Visible = show;
}

The final task is to add the show column and hide column click event processing. As we can see in the code, I simply call the hidedatagridcolumn () method to display or hide columns. Of course, the parameter is required.

Private void hidecolumnbtn_click (Object sender, system. eventargs E)
{
Hidedatagridcolumn (columnslist. selectedindex, false );
This. databind ();
}
Private void showcolumnbtn_click (Object sender, system. eventargs E)
{
Hidedatagridcolumn (columnslist. selectedindex, true );
This. databind ();
}

Okay, all work is finished. Let's see how the running results are. You can select the columns to hide or display from the drop-down list, and click show or hide.

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.