Now, a project is being developed using ASP. NET 2.0. Some sporadic records about the use of gridview during the development process
1. Binding in templatefield
< ASP: templatefield >
< Itemtemplate > <% #Eval("Ocompany. Name") %> </ Itemtemplate >
</ ASP: templatefield >
2. Get the primary key value of the selected row through the checkbox (gridview supports multiple primary keys) Private String Getselectid ()
{
// Ensurechildcontrols ();
Gridview gvlist1 = (Gridview) This . Controls [ 0 ]. Findcontrol ( " Gvlist " );
Int Icount = Gvlist1.rows. count;
String ID = String . Empty;
For ( Int I = 0 ; I < Gvlist1.rows. Count; I ++ )
{
Checkbox CB = (Checkbox) gvlist1.rows [I]. findcontrol ( " Cb_id " );
// Obtain the information of the selected row.
If (Cb. Checked = True )
{
Datakey DKr=Gvlist1.datakeys [I];
ID=DKr. value. tostring ();
}
}
Return ID;
}
3. dynamically create templatefield Private Void Templatecolumn (gridview gvlist, String Column, gridlistattribute ATT)
{
Templatefield TF = New Templatefield ();
TF. headertext = Att. headertext;
TF. headerstyle. Width = Unit. parse (Att. width );
Columntemplate temp = New Columntemplate ();
Temp. Column = Att. column;
TF. itemtemplate = Temp;
Gvlist. Columns. Add (TF );
}
Columntemplate classPublic Class Columntemplate: itemplate
{
// First template Column
Public Void Instantiatein (control container)
{
Literalcontrol L= NewLiteralcontrol ();
L. databinding+ = NewEventhandler (This. Ondatabinding );
Container. Controls. Add (L );
}
Public Void Ondatabinding ( Object Sender, eventargs E)
{< br>
literalcontrol lt = (literalcontrol) sender; // literalcontrol sends a binding request
gridviewrow container = (gridviewrow) lt. namingcontainer;
lt. text = databinder. eval (container. dataitem, column ). tostring ();
}
}