dynamic management asp.net datagrid data columns

Source: Internet
Author: User
Tags bool
asp.net|datagrid| Dynamic | Data in the asp.net DataGrid Data Display control programming, we have several ways to increase the DataGrid columns. The most common approach is to add in the Web Forms Designer by dragging the DataGrid control to the Web design page in the Control Toolbox, and then adding the columns column to the Property builder; Another way is to change the HTML code in HTML view mode to increase the columns column. However, both of these methods are at design time and cannot be changed once the design is complete. In fact, we can also dynamically add or remove columns columns while the program is running. In this article, I will show you how to programmatically add and delete columns columns dynamically at run time, actually by hiding or columns columns.

The Columns property of the DataGrid is the key to accessing the DataGrid columns. Access to this property returns a collection object such as Datagridcolumncollection, which contains all the Datagrigcolumn objects. Datagridcolumncollection provides a way to add a Datagrigcolumn object and delete an existing Datagrigcolumn object. , we will use the Datagridcolumncollection Add method to add a Datagrigcolumn object that dynamically adds a column to the DataGrid at runtime. A datagrigcolumn represents a column of the DataGrid, and the Visible property of the DataGrid is used to show or hide a column.

Okay, now let's go with me to create a dynamicdatagrid C # asp.net project that he has the option of hiding and showing each column of the DataGrid.

Inside the Web application I created with vs.net, I dragged a panel control over the design page. On this Panel control, I placed a DataGrid control, a DropDownList control, and two button controls to change the properties of the DataGrid control.

Now we start by creating two methods: Filldatagrid () and Fillcolumnslist () methods. Filldatagrid () is used to add a column to the DataGrid control and populate it with a dataset data source. Here I get the dataset through the Db.getdataset () method. You can refer to additional source code files (DB.CS) to get more details.

The following code illustrates the implementation of the Createdatagrid (). As you can see from the code, I created three columns, bound to the Id,name and address fields of the dataset with the BoundColumn DataField property. The BoundColumn class inherits from the DataGridColumn class.

private void Createdatagrid ()
{
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 the column names from the DataGrid and populates the columns (Columns) names into the Drop-down lists of the DropDownList controls. We will use the DropDownList control to select the hidden or displayed columns.

private void Fillcolumnslist (DataGrid grid)
{
foreach (DataGridColumn col in grid. Columns)
{
ColumnsList.Items.Add (Col. HeaderText);
}
}

We then add the Hidedatagridcolumn () method to show or hide a column specifically by using the index index and the bool value of two parameters. Here, I simply set the Visible property of the Columns column to TRUE or FALSE.

private void Hidedatagridcolumn (int index, BOOL show)
{
Datagrid1.columns[index]. Visible = Show;
}

The final task is to increase the click event handling of Show column and hide column. As we can see in the code, I simply invoke the Hidedatagridcolumn () method to show or hide the columns, and of course pass in the arguments.

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 ();
}

OK, all the work is over, let's see how the results are running. You can choose which columns to hide or display by using the Drop-down list, and simply click the Show or hide button.



Related Article

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.