Webcast Notes 2-DataGrid Best Practices

Source: Internet
Author: User

DataGrid Best Practices)
Bytes ------------------------------------------------------------------------------------------------------------------
To avoid the last deletion problem, add:
If (maid. Count = 1)
{
If (maid! = 0)
Datagrid1.currentpageindex = maid-1;
}
Bytes ------------------------------------------------------------------------------------------------------------------
In the Add deletion confirmation dialog box, create a template column, add a button in the template column, and set attributes such as the button name and commandname (set as userdelete in the following example,
Write in the itemcreated event of the DataGrid:
Switch (E. Item. itemtype)
{
Case listitemtype. item;
Case listitemtype. edititem;
Listitemtype. alternatingitem;
Button mydeletebutton = (button) E. Item. findcontrol ("btndelete ");
Mydeletebutton. Text = "delete this column ";
// Add a prompt to delete the row. In the following example, the Row E. Item. itemindex + 1 can be displayed for the sake of humanization.
Mydeletebutton. Attributes. Add ("onclick", "Return confirm ('Do you really want to delete the" + E. Item. itemindex. tostring () + "Row ?? ');");
Break;
}
Delete the data written to the itemcommand (Object source, system. Web. UI. webcontrols. datagridcommandeventargs e) event of the DataGrid:
If (E. commandname = "userdelete ")
Datagrid1_deletecommand (source, e );
Bytes ------------------------------------------------------------------------------------------------------------------
The full selection is manually added in HTML, and the remaining work is polling in the Code. For details, see video45 minutes. | Button. Attributes. Add adds client attributes.
Bytes ------------------------------------------------------------------------------------------------------------------
Add the statistical value first, set the showfooter of the DataGrid to true, and then calculate the value cyclically and add the value. Then you can do the average or something.
Int COUNT = 0;
For (INT I = 0; I <Ds. Tables [0]. Rows. Count; I ++)
{
// Calculate the total score
Count + = int. parse (Ds. Tables [0]. Rows [I] ["score"]. tostring ());
}
// Calculate the average score
Int nav = count/Ds. Tables [0]. Rows. count;
// Round-robin: add all the values to be added.
Foreach (maid [0]. Controls)
{
If (DGI. itemtype = listitemtype. footer)
DGI. cells [6]. Text = "average:" + nav. tostring ();
}
Bytes ------------------------------------------------------------------------------------------------------------------
Specify the width and format of the text box in the editing status. The code below should be written in the itemdatabound () event of the DataGrid:
If (E. Item. itemtype = listitemtype. edititem)
{
For (INT I = 0, I <E. Item. cells. Count; I ++)
{
If (E. Item. cells [I]. Controls. Count> 0)
{
Try
{
Textbox t = (textbox) E. Item. cells [I]. controls [0];
// Each defined in this way is the same width.
T. width = 100;
}
Catch
{
}
}
}
}
Bytes ------------------------------------------------------------------------------------------------------------------
Hiding columns is simple. When bindgrid () is used, write datagrid1.columns [0]. Visible = false.
Bytes ------------------------------------------------------------------------------------------------------------------
Exporting a DataGrid to excel is also very easy. Two Methods: Use the browser's response method or Excel Automation method, the former is slightly safer, and the latter uses the component method.
Add an export button to the page and write it to its code:
// Specify the browser type
Response. contenttype = "Applicaion/vnd. MS-excel ";
// The character set is empty.
Response. charset = "";
This. enalbeviewstate = false;
System. Io. stringwriter Sw = new system. Io. stringwriter ();
System. Web. UI. htmltextwriter hW = new system. Web. UI. htmltextwriter (SW );
Datagrid1.rendercontrol (HW );
// You can specify the path
Response. Write (SW. tostring ());
Response. End ();
Bytes ------------------------------------------------------------------------------------------------------------------
Itemcreated is executed when each item is created; itemdatabound is executed when DataGrid. databind is executed.
Bytes ------------------------------------------------------------------------------------------------------------------
To add a row to the DataGrid, it is generally processed on a new page. You can also add a row to the dataset and bind it to the database and the DataGrid.
Bytes ------------------------------------------------------------------------------------------------------------------
To export a PDF file, use a crystal report.
Bytes ------------------------------------------------------------------------------------------------------------------
All columns on the DataGrid are changed to the template column, which is actually the repeater.

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.