Gridview uses some records

Source: Internet
Author: User

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 ();
}
}  

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.