Paging
Protected void maid (Object source, maid E)
{
This. datagrid1.currentpageindex = E. newpageindex;
This. datagridview ();
}
Hide Columns
This. Maid [2]. Visible = false; // hide the third column (only hidden on the surface)
This. datagridview ();
Highlight the mouse over each row:
Protected void maid (Object sender, maid E)
{
If (E. Item. itemtype = listitemtype. Item | E. Item. itemtype = listitemtype. alternatingitem)
{
E. Item. Attributes. Add ("onmouseover", "c = This. style. backgroundcolor; this. style. backgroundcolor = '# 6699ff '");
E. Item. Attributes. Add ("onmouseout", "This. style. backgroundcolor = C ");
}
}
Bidirectional sorting:
The sortcommand event of datagrid1 activates the sorting event.
Protected void maid sortcommand (Object source, datagridsortcommandeventargs E)
{
If (viewstate ["order"] = NULL)
{
Viewstate ["order"] = "ASC"; // viewstate [""] is similar to session [""] and stored on the client
}
Else
{
If (viewstate ["order"]. tostring () = "ASC ")
{
Viewstate ["order"] = "DESC ";
}
Else
{
Viewstate ["order"] = "ASC ";
}
}
Sqlconnection con = connecttion. Ado. sqldb ();
Con. open ();
Sqldataadapter SDA = new sqldataadapter ();
SDA. selectcommand = new sqlcommand ("select * From person", con );
Dataset DS = new dataset ();
SDA. Fill (DS, "person ");
DS. Tables ["person"]. defaultview. Sort = E. sortexpression + "" + viewstate ["order"]. tostring ();
// The default view's sort attribute is the sorting attribute E. sortexpression, which is the field of the sorting expression in the property generator.
This. datagrid1.datasource = Ds. Tables ["person"]. defaultview ;//
This. datagrid1.databind ();
}
Bind to view detailed information columns
In show. aspx, use string id = request. querystring ["ID"] to receive the message.
Datakeyfield = "ID" in the DataGrid is obtained based on the index. The ID of the current row can be obtained during use.
When you want to delete or modify each row, you can use this. datagrid1.datakeys [E. Item. itemindex] to obtainD
DataGridImplementation of Deletion
First, add the delete button to the property generator, and then write the code in the deletecommand event of dadagrid.
Protected void maid (Object source, maid E)
{
String id = This. datagrid1.datakeyfield [E. Item. itemindex];
Sqlconnection con = connecttion. Ado. sqldb ();
Con. open ();
Sqlcommand cmd = new sqlcommand ("delete from person where pid = '" + ID + "'", con );
Cmd. executenonquery ();
This. datagridview ();
}
If you want to confirm the deletion before deleting the Function
Add the third row to the event of the datagrid1_itemdatabound.
Protected void maid (Object sender, maid E)
{
If (E. Item. itemtype = listitemtype. Item | E. Item. itemtype = listitemtype. alternatingitem) // events of normal and staggered rows
{
E. Item. Attributes. Add ("onmouseover", "c = This. style. backgroundcolor; this. style. backgroundcolor = '# 6699ff'"); // when the mouse is over
E. Item. Attributes. Add ("onmouseout", "This. style. backgroundcolor = C"); // when the mouse leaves
(Linkbutton) (E. Item. cells [4]. controls [0]). Attributes. Add ("onclick", "Return confirm ('Are you sure you want to delete it? ') "); // The event triggered when the 5th Column of any row is clicked
}
}
DataGrid Editing:
Update:
String id = This. datagrid1.datakeys [E. Item. itemindex]. tostring ();D
String pname = (textbox) (E. Item. cells [1]. controls [0]). Text; Value of the text box of the second field of the row
String struct x = (textbox) (E. Item. cells [2]. controls [0]). text;
Join the database below,
You can use the verification control to verify the update. First, convert the column into a template column in the property generator, and then add the verification control to the template.