In ASP.NET DataGrid Data Display Control Programming, we have several ways to increase DataGrid Column. The most common way is to increase in the Web Forms designer, driven the DataGrid control to the web design page in the control toolbox, and then add a columns column in the property generator; further mode is to change in HTML view mode The way HTML code increases the columns column. But both of these methods are done in design, and they cannot be changed once the design is completed. In fact, we can also increase or delete columns columns during program runtime. In this article, I will introduce you how to program the implementation of the increase and delete columns columns in the running time, in fact, is implemented by hidden or reality columns columns.
DataGrid's columns attribute is the key to accessing DataGrid Columns. Accessing this property returns a collection object such as DataGridColumnCollection, which contains all DataGrigColumn objects. DataGridColumnCollection provides a method of adding a DataGrigColumn object and deleting an existing DataGrigColumn object. We will use the ADD method of the DataGridColumnCollection to add a DataGrigColumn object to increase a list of DataGrids during runtime. A DataGrigColumn represents a column of DataGrid, the Visible property of the DataGrid is used to display or hide a column.
Ok, let's let everyone create a DynamicDataGrid's C # ASP.NET project, which hides and displays the options for each column of DataGrid.
In the Web Application created with vs.net, I dragged and dropped a panel control on the design page. On this Panel control, I placed a DataGrid control, a DROPDOWNLIST control, two Button controls for changing the properties of the DataGrid control. The final design interface looks like this.
Now let's start to create two ways: FillDataGrid () and FillColumnslist () methods. FillDataGrid () is used to add a column to the DataGrid control and fill it with a DataSet data source. Here I am getting DataSet through the DB.GetDataSet () method. You can get more details in addition to the additional source code file (DB.cs).
The following code illustrates the implementation of CreateDataGrid (). As can be seen from the code, I created three columns, bind to the DataSet ID, Name, and Address fields with BoundColumn DataField properties. The BoundColumn class inherits from the DataGridColumn class.
Private void createDataGrid () {// set DataGrid PropertiesDataGrid1.autogenerateColumn = false;
// Get a DataSet Object Filled with DataDataSet DS = db.getdata ();
// Create ID Column & Add to DataGridBoundColumn Col = New BoundColumn (); Col.Headertext = "User ID"; col.Datafield = "ID"; DataGrid1.columns.add (col);
// Create Name column & add to DataGridcol = new BoundColumn (); col.HeaderText = "User Name"; col.DataField = "Name"; DataGrid1.Columns.Add (col); // Create Address column & add to DataGridcol = New BoundColumn (); col.Headertext = "User address"; col.Datafield = "address"; dataGrid1.columns.add (col);
// DataGrid Data BindingDataGrid1.datasource = DS.TABLES [0]; DataGrid1.databind ();
The FillColumnsList () method is just a simple name 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 (DataGridColumn Col in Grid.columns) {ColumnsList.Items.Add (Col.Headertext);}}
Then we add the hideDataGridColumn () method to display or hide a column through the index index and BOOL value. Here, I am just a simple setting of the Visible property of the columns column as True or False.
Private void HidedataGridColumn (int index, bool show) {dataGrid1.columns [index] .visible = show;
The final job is to increase the click event processing of Show Column and Hide Column. As we see in the code, I just simply call the hidedataGridColumn () method to display or hide the column, of course, it is necessary to pass the parameters.
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) id; DataBind ();
OK, all the work is over, let's take a look at the results of the run. You can select the column you want to hide or displayed by drop-down, and simply click on the show or hide button.