datagrid|web| Control | Problem dynamic display and Hide columns & dynamically add columns
--Dynamic display of hidden columns
One way to animate columns dynamically is to create them at design time, and then hide or display them when needed, by setting the Visible property of the column. The following example shows how to control the visibility of the second column of the DataGrid (the column indexed as one).
datagrid1.column[1].visible=! (datagrid1.column[1].visible);
--Add columns dynamically (columns that are read by the database)
If you know beforehand which columns you need, you can hide or show them, but sometimes you don't know what columns you need to run, and in that case, you can create columns dynamically and add them to the DataGrid.
The way to do this is to create an instance of a column type that the DataGrid supports-bound, button, or hyperlink columns (you can also add template columns, but slightly more complex).
The following example shows how to bind two columns to a DataGrid:
private void Button1_Click (object sender, System.EventArgs e)
{
Datagrid1.autogeneratecolumns = false;
DataGrid1.DataSource = THIS.DSBOOKS1;
DataGrid1.DataMember = "Books";
Datagrid1.datakeyfield = "BookID";
ADD two columns
BoundColumn dgc_id = new BoundColumn ();
dgc_id. DataField = "BookID";
dgc_id. HeaderText = "ID";
dgc_id. itemstyle.width = new unit (80);
DATAGRID1.COLUMNS.ADD (dgc_id);
This.sqlDataAdapter1.Fill (THIS.DSBOOKS1);
Datagrid1.databind ();
}
Any time you add a control to a column dynamically, you will encounter problems with how to persist. Dynamically incremented columns are not automatically added to the page's view state, so you need to add the page's logic so that the columns are visible each time the commit-postback process.
An excellent way to do this is to overload the page's LoadViewState method, which gives us an earlier opportunity to redefine the columns in the DataGrid control because the method is called before the Page_Load event is triggered. Adding columns in LoadViewState ensures that they are available to normal operations before any event code is executed.
The following example shows how to extend the previous example so that the columns are saved every time the page executes. As with the previous example, the Button1_Click event adds two columns to the DataGrid (in this case, the event handle invokes a separate method--addcolumns ()), and the page also contains a simple Boolean property, Called dynamiccolumnsadded to indicate whether column dynamics are increasing in the DataGrid. This property holds its value in view state. The LoadViewState method first invokes the LoadViewState method of its base class, which extracts the view state information and configures the control. If columns are added to the DataGrid in advance, the method adds them again.
private bool dynamiccolumnadded{
Get
{
Object B = viewstate["dynamiccolumnadded"];
return (b = = null)? False:true;
}
Set
{
viewstate["dynamiccolumnadded"] = value;
}
}
This.sqlDataAdapter1.Fill (THIS.DSBOOKS1);
Datagrid1.databind ();
This. Dynamiccolumnadded = true;
}
(unfinished all code has been tested, just get a dataset of your own)
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