Two articles about Dynamic Column addition in gridview

Source: Internet
Author: User

From: http://blog.csdn.net/liang4571231/archive/2009/03/26/4025944.aspx

 

Public class mytemplate: itemplate

{

Private string strcolumnname;

Private datacontrolrowtype dcrtcolumntype;

Public mytemplate (string strcolumnname, datacontrolrowtype dcrtcolumntype)

{

This. strcolumnname = strcolumnname;

This. dcrtcolumntype = dcrtcolumntype;

}

Public void instantiatein (control ctlcontainer)

{

Switch (dcrtcolumntype)

{

Case datacontrolrowtype. header: // Column Title

Literal LTR = new literal ();

LTr. Text = strcolumnname;

Ctlcontainer. Controls. Add (LTr );

Break;

Case datacontrolrowtype. datarow: // template column content -- load hyperlink1

Hyperlink HL = new hyperlink ();

Hl. ID = "hyperlink1 ";

Hl. navigateurl = "erro. aspx ";

Hl. Text = "asddddd ";

Ctlcontainer. Controls. Add (HL );

Break;

}

}

}

Gridview gdview = new gridview (); // (gridview) m_map.page.findcontrol ("gridview1 ");

 

 

Gdview. autogeneratecolumns = false;

Gdview. datakeynames = new string [] {"SDE. st_zd.zd_djh "};

Boundfield bf1 = new boundfield ();

Boundfield bf2 = new boundfield ();

Templatefield TF = new templatefield ();

Bf1.headertext = "cadastral number ";

Bf1.datafield = "SDE. st_zd.zd_djh ";

Bf1.readonly = true;

 

Bf2.headertext = "Name of the owner ";

Bf2.datafield = "SDE. st_zd.qlr_mc ";

TF. headertext = "template column test ";

TF. itemtemplate = new mytemplate ("aaaa", datacontrolrowtype. datarow );

Commandfield cf = new commandfield ();

Cf. buttontype = buttontype. Button;

Cf. showcancelbutton = true;

Cf. showeditbutton = true;

Gdview. Columns. Add (bf1 );

Gdview. Columns. Add (bf2 );

Gdview. Columns. Add (TF );

 

 

 

Article2. http://blog.csdn.net/Sandy945/archive/2009/07/31/4397142.aspx

 

When you dynamically add template columns in the gridview, this case is often encountered, and there are a lot of online related materials,

However, there is no question about how to dynamically add events to controls added in the template column. This article is intended to solve this problem.

CodeAs follows:

Gridviewtemplatedemo. aspx

View plaincopy to clipboardprint?
<% @ Page Language = "C #" autoeventwireup = "true" codefile = "gridviewtemplatedemo. aspx. cs" inherits = "gridview_gridviewtemplatedemo" %>
<! Doctype HTML public "-// W3C // dtd xhtml 1.0 transitional // en" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<HTML xmlns = "http://www.w3.org/1999/xhtml">
<Head id = "head1" runat = "server">

<Title> example of dynamically adding a template column in The gridview </title>

</Head>

<Body>

<Form ID = "form1" runat = "server">

<Asp: gridview id = "gridview1" runat = "server" autogeneratecolumns = "false">

</ASP: gridview>

</Form>

</Body>

</Html>
<% @ Page Language = "C #" autoeventwireup = "true" codefile = "gridviewtemplatedemo. aspx. cs" inherits = "gridview_gridviewtemplatedemo" %>
<! Doctype HTML public "-// W3C // dtd xhtml 1.0 transitional // en" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<HTML xmlns = "http://www.w3.org/1999/xhtml">
<Head id = "head1" runat = "server">

<Title> example of dynamically adding a template column in The gridview </title>

</Head>

<Body>

<Form ID = "form1" runat = "server">

<Asp: gridview id = "gridview1" runat = "server" autogeneratecolumns = "false">

</ASP: gridview>

</Form>

</Body>

</Html>

Gridviewtemplatedemo. aspx. CS

View plaincopy to clipboardprint?
Using system;
Using system. Data;
Using system. configuration;
Using system. collections;
Using system. Web;
Using system. Web. Security;
Using system. Web. UI;
Using system. Web. UI. webcontrols;
Using system. Web. UI. webcontrols. webparts;
Using system. Web. UI. htmlcontrols;
Public partial class gridview_gridviewtemplatedemo: system. Web. UI. Page
{
Private datatable getdatetable ()
{
Datatable dt = new datatable ();
DT. Columns. Add (New datacolumn ("ID", typeof (int32 )));
DT. Columns. Add (New datacolumn ("name", typeof (string )));
Datarow DR = DT. newrow ();
Dr [0] = 1;
Dr [1] = "01 ";
DT. Rows. Add (DR );
Dr = DT. newrow ();
Dr [0] = 2;
Dr [1] = "01011 ";
DT. Rows. Add (DR );
Dr = DT. newrow ();
Dr [0] = 3;
Dr [1] = "0120 ";
DT. Rows. Add (DR );
Dr = DT. newrow ();
Dr [0] = 4;
Dr [1] = "01001 ";
DT. Rows. Add (DR );
Dr = DT. newrow ();
Dr [0] = 5;
Dr [1] = "0105 ";
DT. Rows. Add (DR );
Return DT;
}

Protected override void oninit (eventargs E)
{
Templatefield customfield = new templatefield ();

Customfield. showheader = true;
Customfield. headertemplate = new gridviewtemplate (datacontrolrowtype. header, "dynamically add columns ");
Gridviewtemplate GVT = new gridviewtemplate (datacontrolrowtype. datarow, "lbtn", "name ");
GVT. EH + = new gridviewtemplate. eventhandler (lbtn_click );
Customfield. itemtemplate = GVT;
Gridview1.columns. Add (customfield );
Base. oninit (E );
}

Protected void page_load (Object sender, eventargs E)
{
If (! Ispostback)
{
Gridview1.datasource = getdatetable ();
Gridview1.databind ();
}
}

Public void lbtn_click (Object sender, eventargs E)
{
Clientscript. registerstartupscript (GetType (), "test", "alert ('OK');", true );
}
}

Public class gridviewtemplate: itemplate

{
Public Delegate void eventhandler (Object sender, eventargs E );
Public event eventhandler eh;

Private datacontrolrowtype templatetype;

Private string columnname;
Private string controlid;



Public gridviewtemplate (datacontrolrowtype type, string colname)

{

Templatetype = type;

Columnname = colname;

}
Public gridviewtemplate (datacontrolrowtype type, string controlid, string colname)
{
Templatetype = type;
This. controlid = controlid;
Columnname = colname;
}


Public void instantiatein (system. Web. UI. Control container)
{
Switch (templatetype)
{
Case datacontrolrowtype. header:
Literal lc = new literal ();
LC. Text = columnname;
Container. Controls. Add (LC );
Break;
Case datacontrolrowtype. datarow:
Linkbutton lbtn = new linkbutton ();
Lbtn. ID = This. controlid;
If (eh! = NULL)
{
Lbtn. Click + = new system. eventhandler (EH );
}
Lbtn. databinding + = new system. eventhandler (lbtn_databinding );

Container. Controls. Add (lbtn );

Break;
Default:
Break;
}
}
Void lbtn_databinding (Object sender, eventargs E)
{
Linkbutton lbtn = sender as linkbutton;
If (lbtn! = NULL)
{
Gridviewrow Container = lbtn. namingcontainer as gridviewrow;
If (container! = NULL)
{
Object datavalue = databinder. eval (container. dataitem, columnname );
If (datavalue! = Dbnull. value)
{
Lbtn. Text = datavalue. tostring ();
}
}
}
}

}

Related Article

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.