Frequently Used DataGrid skills

Source: Internet
Author: User

1. date display
2. pass the value in the DataGrid
3. dialog box for confirming deletion in the DataGrid
4. format the DataGrid: Convert the value 0, 1 from the original data to the actual text
5. select, confirm, and delete the multi-row check box list in the DataGrid
6. use the dropdownlist drop-down list box to display a field in the database table
7. get the checkbox return value in the DataGrid
8. add a statistical value to the DataGrid
9. how to Use the Program to hide and display a column in the DataGrid
10. How to control the textbox width of the editing function in the DataGrid?

1.
D. display the day as a number without leading zero (for example, 1 ).
Dd displays the day as a number with a leading zero (for example, 01 ).
Ddd displays the day as an abbreviation (for example, Sun ).
Dddd displays the day as the full name (for example, Sunday ).
M Displays the month as a number without leading zero (for example, January 1, January is expressed as 1)
MM displays the month as a number with a leading zero (for example, 01/12/01 ).
Mmm displays the month as an abbreviation (for example, Jan ).
Mmmm displays the month as the complete month name (for example, January ).
H uses the 12-hour system to display the hour as a number without leading zero (for example, 1:15:15 ).
HH uses the 12-hour system to display the hour as a number with a leading zero (for example, 01:15:15 ).
H uses the 24-hour system to display the hour as a number without leading zero (for example, 1:15:15 ).
HH uses the 24-hour system to display the hour as a number with a leading zero (for example, 01:15:15 ).
M Displays the minute as a number without leading zero (for example ).
MM displays the minute as a number with a leading zero (for example, 12:01:15 ).
S displays the second as a number without leading zero (for example ).
SS displays seconds as numbers with leading zeros (for example, 12:15:05 ).
Y displays the year (0-9) as a number without leading zero.
YY displays the year in the format of two digits with leading zeros (if applicable ).
YYY displays the year in the format of three digits.
Yyyy displays the year in four-digit format.

2. Pass the value in the DataGrid

Show. aspx backgroundCode

Private void page_load (Object sender, system. eventargs E)
{
// Place user code here to initialize the page
Response. Write ("the student ID you selected is:" + request. querystring ["ID"]);
}

3. Add the confirm deletion dialog box in the DataGrid

Private void dgshow_itemcreated (Object sender, system. Web. UI. webcontrols. datagriditemeventargs E)
{
Switch (E. Item. itemtype)
{
Case listitemtype. item:
Case listitemtype. edititem:
Case listitemtype. alternatingitem:
Button mydeletebutton = (button) E. Item. findcontrol ("btndelete ");
Mydeletebutton. Text = "delete this row ";
Mydeletebutton. Attributes. Add ("onclick", "Return confirm ('Do you really want to delete the" + E. Item. itemindex. tostring () + "Row? ');");
Break;
}
}
Private void dgshow_itemcommand (Object source, system. Web. UI. webcontrols. datagridcommandeventargs E)
{
If (E. commandname = "userdelete ")
Dgshow_deletecommand (source, e );
}

4. Convert the value 0, 1 from the original data to the actual text
Omitted
5. Select, confirm, and delete the multi-row check box list in the DataGrid.

Public void checkall (Object sender, system. eventargs E)
{
Checkbox cball = (checkbox) sender;
If (cball. Text = "select all ")
{
Foreach (datagriditem DGI in dgshow. Items)
{
Checkbox cb = (checkbox) DGI. findcontrol ("cbselect ");
CB. Checked = cball. checked;
}
}
}
Private void btndelete_click (Object sender, system. eventargs E)
{
Foreach (datagriditem DGI in dgshow. Items)
{
Checkbox cb = (checkbox) DGI. findcontrol ("cbselect ");
If (CB. Checked)
{
// Perform the following deletion operations:
Int nid = int. parse (DGI. cells [0]. Text );
String strsql = "delete from tbstudentinfo where studentid =" + NID;
Executesql (strsql );
}
}
Dgshow. currentpageindex = 0;
Binddata ();
}

6. Use the dropdownlist drop-down list box to display a field in the database table

Foreach (datagriditem DGI in dgshow. Items)
{
// The following uneditable binding drop-down list
Dropdownlist DDI = (dropdownlist) DGI. findcontrol ("ddlsexi ");
If (DDI! = NULL)
{
Bool bsex = (bool) ds. Tables ["studentinfo"]. Rows [DGI. itemindex] ["sex"];
If (bsex)
DDI. selectedindex = 0;
Else
DDI. selectedindex = 1;
}
// The following binding and editing status drop-down list
Dropdownlist DDE = (dropdownlist) DGI. findcontrol ("ddlsexe ");
If (DDE! = NULL)
{
Bool bsex = (bool) ds. Tables ["studentinfo"]. Rows [DGI. itemindex] ["sex"];
If (bsex)
DDE. selectedindex = 0;
Else
DDE. selectedindex = 1;
}
}

7. Get the checkbox return value in the DataGrid
8. Add statistics to the DataGrid.

Int COUNT = 0;
For (INT I = 0; I <Ds. Tables [0]. Rows. Count; I ++)
{
Count + = int. parse (Ds. Tables [0]. Rows [I] ["score"]. tostring ());
}
Int nav = count/Ds. Tables [0]. Rows. count;
Foreach (maid in dgshow. controls [0]. Controls)
{
If (DGI. itemtype = listitemtype. footer)
DGI. cells [6]. Text = "average:" + nav. tostring ();
}

9. How to hide and display a column in the DataGrid using a program

Dgshow. Columns [0]. Visible = false;

10. How to control the textbox width of the editing function in the DataGrid?

Private void dgshow_itemdatabound (Object sender, system. Web. UI. webcontrols. datagriditemeventargs E)
{
If (E. Item. itemtype = listitemtype. edititem)
{
For (INT I = 0; I <E. Item. cells. Count; I ++)
{
If (E. Item. cells [I]. Controls. Count> 0)
{
Textbox t = (textbox) E. Item. cells [I]. controls [0];
T. width = 100;
}
}
}
}

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.