About GV and DataTable dynamic binding

Source: Internet
Author: User

Foreground aspx page

<asp:gridview id= "GridView1" runat= "Server" width= "100%" skinid= "Nviewskintotalstatistics"
>
<Columns>

</Columns>
</asp:GridView>

Back-end. CS Code

1.GV Dynamic Binding

<summary>
Dynamically binding the GridView column
</summary>
void Addgridviewcolumn ()
{
GridView1.Columns.Clear ();
First column
BoundField bfColumn0 = new BoundField ();
Bfcolumn0.datafield = "Month";
Bfcolumn0.headertext = "month";
GRIDVIEW1.COLUMNS.ADD (BfColumn0);

Dynamic acquisition of nuclear medicine reagent name
DataTable nuclearmedicinename = new DataTable ();
Nuclearmedicinename = new Ristotalmanage (). Getnuclearmedicinename ();
ienumerable<string> l_nuclearmedicinename = new list<string> ();
L_nuclearmedicinename = Nuclearmedicinename.asenumerable (). Select (t = t.field<string> (pub_dictionary. F_dicvalue));

foreach (var it in L_nuclearmedicinename)
{
BoundField bf = new BoundField ();
The name of the field that represents the table bound to the column
Bf. DataField = it;
Represents the name of the column
Bf. HeaderText = it;
GRIDVIEW1.COLUMNS.ADD (BF);
}

Radionuclide Treatment Volume column
BoundField bfcolumnend = new BoundField ();
Bfcolumnend.datafield = "Counts";
Bfcolumnend.headertext = "radionuclide Treatment volume";
GRIDVIEW1.COLUMNS.ADD (Bfcolumnend);
Gridview1.width = 12 * 60;
GridView1.RowStyle.HorizontalAlign = Horizontalalign.center;
}

Dynamic generation of a DataTable in the same format as GV after 2.GV dynamic binding

void Addstatisticscolumn (ref DataTable Dtstatistics)
{
DataColumn Dcclientdepart = new DataColumn ("Month", typeof (String));
Dcclientdepart.caption = "month";
DTSTATISTICS.COLUMNS.ADD (Dcclientdepart);

Dynamic acquisition of nuclear medicine reagent name
DataTable nuclearmedicinename = new DataTable ();
Nuclearmedicinename = new Ristotalmanage (). Getnuclearmedicinename ();
ienumerable<string> l_nuclearmedicinename = new list<string> ();
L_nuclearmedicinename = Nuclearmedicinename.asenumerable (). Select (t = t.field<string> (pub_dictionary. F_dicvalue));

foreach (var it in L_nuclearmedicinename)
{

DataColumn dcname = new DataColumn (it, typeof (String));
Dcname.caption = it;
DTSTATISTICS.COLUMNS.ADD (dcname);
}

DataColumn dccounts = new DataColumn ("Counts", typeof (String));
dccounts.caption = "radionuclide Treatment volume";
DTSTATISTICS.COLUMNS.ADD (dccounts);

}

3. Fill DataTable data and assign to GV to complete the operation

DataTable dtstatistics = new DataTable ();
Construct a DataTable in the same format as GV
Addstatisticscolumn (ref dtstatistics); Step Two
if (Dtmaskulin = = null) return null;
if (DtMaskulin.Rows.Count = = 0) return dtmaskulin;

All reagent names
DataTable nuclearmedicinename = new DataTable ();
Nuclearmedicinename = new Ristotalmanage (). Getnuclearmedicinename ();
ienumerable<string> l_nuclearmedicinename = new list<string> ();
L_nuclearmedicinename = Nuclearmedicinename.asenumerable (). Select (t = t.field<string> (pub_dictionary. F_dicvalue));

Start populating data
for (int i = 1; i <=; i++)
{
DataRow drNew1 = Dtstatistics.newrow ();

if (i = =)
{
drnew1["Month"] = "total";
for (int j = 0; J < DtMaskulin.Rows.Count; J + +)
{
DrNew1 [dtmaskulin.rows[j]["Contrastagentname"]. ToString ()] = drnew1[dtmaskulin.rows[j]["Contrastagentname"]. ToString ()] = Dtmaskulin.compute ("sum (dosages)", String. Format ("Contrastagentname= ' {0} '", dtmaskulin.rows[j]["Contrastagentname"]));
}
drnew1["Counts"] = Dtmaskulin.compute ("sum (dosages)", "");
}
Else
{
drnew1["month"] = i + "months";
drnew1["Counts"] = Dtmaskulin.compute ("sum (dosages)", String. Format ("DateTimes = ' {0} '", Syear + "-" + i.tostring (). GETSTR (2)));
for (int j = 0; J < DtMaskulin.Rows.Count; J + +)
{
drnew1[dtmaskulin.rows[j]["Contrastagentname"]. ToString ()] = Dtmaskulin.compute ("sum (dosages)", String. Format ("Contrastagentname= ' {0} ' and DateTimes = ' {1} '", dtmaskulin.rows[j]["contrastagentname"],syear + "-" + I. ToString (). GETSTR (2));
}
}

DtStatistics.Rows.Add (drNew1);
}

return dtstatistics;

About GV and DataTable dynamic binding

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.