Hot questions about the DataGrid Web control

Source: Internet
Author: User
Tags header object model
datagrid|web| Control | Problem article translation at ease in the work of the actual problems encountered, and check MSDN, did not find the Chinese information, online on this aspect of practical information is not much, the intention to collect and translate, in this exchange with you, Welcome to criticize: zyfly0808@hotmail.com


Content--

Preface: The DataGrid control in a Windows Forms &web form

①. Controls the width, height, and alignment of columns.

②. Make the display and edit mode appearance of the column.

③. Format dates, currencies, and other data.

④. Dynamically display and hide columns.

⑤. Dynamically add columns.

⑥. Adds a record to the data source with the DataGrid control.

⑦. Displays a drop-down list in edit mode.

⑧. Select multiple items with a checkbox.

⑨. Edit multiple lines at once.

⑩. Click anywhere to select the row.


Brief introduction







DataGrid server control is a powerful tool for displaying information from a data source and is simple to use. You can display editable data only by setting some attributes, and make it look professional, and it also has a very complex object model that gives you a lot of flexibility in presenting your data.







This article focuses on issues that are frequently asked in the newsgroup network and other developers ' forums to develop a DataGrid display. Some of the issues discussed here are simple, but sometimes confusing, in which case they raise the question of how to master the basic functionality of the DataGrid control.







The article assumes that you are already familiar with the control-how to add it to a form and how to set it to display data. You also need to know how to turn its line into editable mode and other basic tasks. Ultimately, you'll find it helpful to learn how to use templates-adding template columns to the DataGrid and displaying controls in the template.








Preface: The DataGrid control in a Windows Forms &web form.







The DataGrid control in a Web form is not the equivalent of the control in Windows Forms, and is generally considered to be the same control with at least the same functionality. However, the entire programming paradigm of Web Forms differs greatly from Windows Forms, for example, when every process of Web Forms pages is uploaded back to the server, they must maintain state, have very different data-binding models, and so on.







It is because of these differences that they make a big difference in their respective controls, including
DataGrid control, in general, the Web Forms DataGrid control has fewer built-in properties, and several differences are listed below:
①. Do not inherit the support master--from the data structure.

②. As with other Web server controls, it does not support bidirectional data binding. If you want to update the data, you must write your own code.

③. You can edit only one row at a time.

④. Not inheritance also supports sorting, although you can sort the contents of the grid by handling the events it fires.

On the other hand:

①. You can bind the Web Forms DataGrid control to any object that supports the IEnumerable interface.

②. The Web Forms DataGrid control supports paging.

③. Compared to Windows Forms DataGrid, it is easier to standardize the appearance of the Web Forms DataGrid and the output of the data.







A Controls the column width, height, and alignment formulas.

By default, the DataGrid control automatically adjusts rows and columns so that they fit all of the things you assign to it

The height and width. Within the full width, it controls the width of the column based on the text of the column header. All data defaults

All are left-aligned.









To control the attributes of a column, you should turn off the auto-Generate Columns Property- Set the AutoGenerateColumns property to false. In practice, it is only true if it is used in a short time, such as a quick browsing of a page or a demo. For build applications, you should add columns by default, and each column can be a bound column or a template column.

To control the width of a column, you can generate a STYLE element for the column (style-element) and then
Set the Width property of an element to a standard unit, with the Width property setting for one item style below
The HTML code:
Set the Width property of an element to a standard unit, with the Width property setting for one item style below
The HTML code:


<asp:boundcolumn datafield= "title" sortexpression= "title"



<asp:boundcolumn datafield= "title" sortexpression= "title"


Header text= "Title" >







<itemstyle width= "100px" ></ItemStyle>







</asp:BoundColumn>







Note: The itemstyle-width= "100px" can be written to the Boundolumn.







You can use style elements to set alignment to "left", "right", or other values defined in the Horizontal alignment collection (in VS, each individual column alignment is visible)







Here is an example:







<asp:boundcolumn datafield= "title" sortexpression= "title"







headertext= "Title" >







<itemstyle width= "100px" horizontalalign= "right" ></ItemStyle>







</asp:BoundColumn>







You can also use a STYLE element to set the height of a column, and you may well feel that it is not set to be flexible, because setting a column's height also sets the height of all columns.







You can also set the width by code at run time, an example of this is the itemgreated event handle, and the following example sets the first two columns to 100 and 50 pixels respectively.







C#







Private void Datagrid-itemcreated (object sender,







System.Web.UI.WebControls.DataGridItemEventArgs e)







{







E.item.cells[0]width=new Unit (100);







E.ITEM.CELLS[1]. Width=new Unit (50);







}







Of course, it makes little sense to set the width values that can be set at design time by code. In general, you might just want to set the width based on the Run-time value, and you can set the width of the cell control to the standard unit (often pixels). However, it does not directly convert these length data to pixels--these are just the number of characters. However, when you create an item, you can use that data to check.







(unfinished)


③. Compared to Windows Forms DataGrid, it is easier to standardize the appearance of the Web Forms DataGrid and the output of the data.







A Controls the column width, height, and alignment formulas.

By default, the DataGrid control automatically adjusts rows and columns so that they fit all of the things you assign to it

The height and width. Within the full width, it controls the width of the column based on the text of the column header. All data defaults

All are left-aligned.









To control the attributes of a column, you should turn off the auto-Generate Columns Property- Set the AutoGenerateColumns property to false. In practice, it is only true if it is used in a short time, such as a quick browsing of a page or a demo. For build applications, you should add columns by default, and each column can be a bound column or a template column.

To control the width of a column, you can generate a STYLE element for the column (style-element) and then
Set the Width property of an element to a standard unit, with the Width property setting for one item style below
The HTML code:
Set the Width property of an element to a standard unit, with the Width property setting for one item style below
The HTML code:


<asp:boundcolumn datafield= "title" sortexpression= "title"



<asp:boundcolumn datafield= "title" sortexpression= "title"


Header text= "Title" >







<itemstyle width= "100px" ></ItemStyle>







</asp:BoundColumn>







Note: The itemstyle-width= "100px" can be written to the Boundolumn.







You can use style elements to set alignment to "left", "right", or other values defined in the Horizontal alignment collection (in VS, each individual column alignment is visible)







Here is an example:







<asp:boundcolumn datafield= "title" sortexpression= "title"







headertext= "Title" >







<itemstyle width= "100px" horizontalalign= "right" ></ItemStyle>







</asp:BoundColumn>







You can also use a STYLE element to set the height of a column, and you may well feel that it is not set to be flexible, because setting a column's height also sets the height of all columns.







You can also set the width by code at run time, an example of this is the itemgreated event handle, and the following example sets the first two columns to 100 and 50 pixels respectively.







C#







Private void Datagrid-itemcreated (object sender,







System.Web.UI.WebControls.DataGridItemEventArgs e)







{







E.item.cells[0]width=new Unit (100);







E.ITEM.CELLS[1]. Width=new Unit (50);







}







Of course, it makes little sense to set the width values that can be set at design time by code. In general, you might just want to set the width based on the Run-time value, and you can set the width of the cell control to the standard unit (often pixels). However, it does not directly convert these length data to pixels--these are just the number of characters. However, when you create an item, you can use that data to check.







(unfinished)


③. Compared to Windows Forms DataGrid, it is easier to standardize the appearance of the Web Forms DataGrid and the output of the data.







A Controls the column width, height, and alignment formulas.

By default, the DataGrid control automatically adjusts rows and columns so that they fit all of the things you assign to it

The height and width. Within the full width, it controls the width of the column based on the text of the column header. All data defaults

All are left-aligned.









To control the attributes of a column, you should turn off the auto-Generate Columns Property- Set the AutoGenerateColumns property to false. In practice, it is only true if it is used in a short time, such as a quick browsing of a page or a demo. For build applications, you should add columns by default, and each column can be a bound column or a template column.

To control the width of a column, you can generate a STYLE element for the column (style-element) and then
Set the Width property of an element to a standard unit, with the Width property setting for one item style below
The HTML code:
Set the Width property of an element to a standard unit, with the Width property setting for one item style below
The HTML code:


<asp:boundcolumn datafield= "title" sortexpression= "title"



<asp:boundcolumn datafield= "title" sortexpression= "title"


Header text= "Title" >







<itemstyle width= "100px" ></ItemStyle>







</asp:BoundColumn>







Note: The itemstyle-width= "100px" can be written to the Boundolumn.







You can use style elements to set alignment to "left", "right", or other values defined in the Horizontal alignment collection (in VS, each individual column alignment is visible)







Here is an example:







<asp:boundcolumn datafield= "title" sortexpression= "title"







headertext= "Title" >







<itemstyle width= "100px" horizontalalign= "right" ></ItemStyle>







</asp:BoundColumn>







You can also use a STYLE element to set the height of a column, and you may well feel that it is not set to be flexible, because setting a column's height also sets the height of all columns.







You can also set the width by code at run time, an example of this is the itemgreated event handle, and the following example sets the first two columns to 100 and 50 pixels respectively.







C#







Private void Datagrid-itemcreated (object sender,







System.Web.UI.WebControls.DataGridItemEventArgs e)







{







E.item.cells[0]width=new Unit (100);







E.ITEM.CELLS[1]. Width=new Unit (50);







}







Of course, it makes little sense to set the width values that can be set at design time by code. In general, you might just want to set the width based on the Run-time value, and you can set the width of the cell control to the standard unit (often pixels). However, it does not directly convert these length data to pixels--these are just the number of characters. However, when you create an item, you can use that data to check.







(unfinished)



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.