datagrid| Tips
1. Display of dates
2. Passing values from the DataGrid
3. Add Confirm Delete dialog box in DataGrid
4. Format DataGrid: Converts the 0,1 value in the data source to the actual text
5. Select, Confirm, delete multiline check box list in DataGrid
6. Use the DropDownList Drop-down list box to display a field in a database table
7. Get the checkbox return value in the DataGrid
Add statistical value in 8.datagrid
9. How to hide and display a column in a DataGrid with a program
10 How do I control the width of the textbox where the editing function appears in the DataGrid?
1.
D Displays the day as a number with no leading zeros (for example, 1).
DD Displays the day as a number with a leading zero (for example, 01).
DDD Displays the day as an abbreviated form (for example, Sun).
DDDD Displays the day as a full name (for example, Sunday).
M Displays the month as a number without a leading zero (as indicated in January as 1)
MM Displays the month as a number with a leading zero (for example, 01/12/01).
MMM Displays the month as an abbreviated form, such as a few.
MMMM Displays the month as the full month name (for example, January).
H displays the hour as a number with no leading zeros (for example, 1:15:15 PM) using a 12-hour system.
HH uses a 12-hour system to display hours as numbers with leading zeros (for example, 01:15:15 PM).
H displays the hour as a number with no leading zeros (for example, 1:15:15) using a 24-hour system.
HH uses a 24-hour system to display hours as a number with a leading zero (for example, 01:15:15).
M displays the minute as a number without a leading zero (for example, 12:1:15).
MM Displays the minute as a number with a leading zero (for example, 12:01:15).
s displays the second as a number with no leading zeros (for example, 12:15:5).
SS Displays the second as a number with a leading zero (for example, 12:15:05).
Y Displays the year (0-9) as a number with no leading zeros.
YY Displays the year in two-digit format with a leading zero, if applicable.
YYY Displays the year in three-digit number format.
YYYY displays the year in four-digit number format.
2. Passing values from the DataGrid
Show.aspx Background Code
private void Page_Load (object sender, System.EventArgs e)
{
Place user code here to initialize page
Response.Write ("The Student ID you selected is:" +request.querystring["id"]);
}
3. Add Confirm Delete dialog box in 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" (' You really want to delete the first "+ e.item.itemindex.tostring () +" OK? ’);");
Break
}
}
private void Dgshow_itemcommand (object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
if (e.commandname== "Userdelete")
Dgshow_deletecommand (source,e);
}
4. Convert the 0,1 value in the data source to the actual text
Slightly
5. Select, Confirm, delete multiline check box list in DataGrid
public void Checkall (object sender, System.EventArgs e)
{
CheckBox Cball = (checkbox) sender;
if (cball.text== "All selected")
{
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)
{
The following performs a delete operation
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 a database table
foreach (DataGridItem dgi in Dgshow.items)
{
The following binding is not an edited state 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 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
Add statistical value in 8.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 (DataGridItem Dgi 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 a DataGrid with a program
Dgshow.columns[0]. Visible = false;
10. How do I control the width of the textbox where the editing function appears 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;
}
}
}
}