asp.net IList the data after the query data and then bind the control _ practical Tips

Source: Internet
Author: User
First, the most basic dataset binding operations, the GridView for example.
When the operation queries a DataSet DS, some data can be formatted, such as:
Copy Code code as follows:

foreach (DataRow Dr in DS. Tables[0]. Rows)
{
if (dr["Depth"]. ToString ()!= "1")
{
dr["ColumnName"] = Stringhelper.stringofchar (Convert.ToInt32 (dr["Depth"])-1, "") + "├" + dr["ColumnName"];
}
}
Gridview1.datasource = ds;
Gridview1.databind ();

Copy Code code as follows:

public static Class Stringhelper
{
<summary>
Generates a string of the specified length, that is, the Strlong str string is generated
</summary>
<param name= "Strlong" > The length of the build </param>
<param name= "str" > Generate a string with str </param>
<returns></returns>
public static string Stringofchar (int strlong, string str)
{
String returnstr = "";
for (int i = 0; i < Strlong; i++)
{
Returnstr + + str;
}
return returnstr;
}
<summary>
Generate date Random Code
</summary>
<returns></returns>
public static string Getramcode ()
{
#region
Return DateTime.Now.ToString ("Yyyymmddhhmmssffff");
#endregion
}
}

You can quickly format data and bind to controls like this.
Second, now I want to talk about the content, if we use IList query data, want to format some strings to achieve their desired display effect, how should we do? First look at the effect of the picture and then give you the answer.


For example, when we do an infinite classification, we often want to make the data as shown in the above figure, how do we do it.
Just on the surface of the dataset to write some formatting code, now I believe everyone wants to know how to operate IList, right?
Generics are powerful I'm not going to say this here, now, there are two main ways to achieve this,
One, the IList data into a dataset, so you can return to the familiar operation
IList DataSet Class (This is my reference to someone else ^ ^)

Copy Code code as follows:

public static DataSet converttodataset<t> (ilist<t> list)
{
if (list = = NULL | | list. Count <= 0)
{
return null;
}
DataSet ds = new DataSet ();
DataTable dt = new DataTable (typeof (T). Name);
DataColumn column;
DataRow Row;
system.reflection.propertyinfo[] Mypropertyinfo = typeof (T). GetProperties (System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.Instance);
foreach (T-T in list)
{
if (t = = null)
{
Continue
}
row = dt. NewRow ();
for (int i = 0, j = mypropertyinfo.length I < J; i++)
{
System.Reflection.PropertyInfo pi = mypropertyinfo[i];
String name = Pi. Name;
if (dt. Columns[name] = = null)
{
column = new DataColumn (name, pi. PropertyType);
Dt. Columns.Add (column);
}
Row[name] = pi. GetValue (t, NULL);
}
Dt. Rows.Add (row);
}
Ds. Tables.add (DT);
return DS;
}

And then
Copy Code code as follows:

DataSet ds = Converttodataset (B.listcolumn ());

Look, come back familiar with the operation of it, but, so do we Kai is not a detour, why not directly with a dataset on it? Yes, we are on the wrong course ... I want to use IList Ah, do not want to go around, there is no good way?
Haha, the answer is certainly some, is also the simplest, often novice friends do not understand the solution IList now all sorts of doubts,
Now talk about the operation of the IList object class
or the example above.
For example, when a query
Copy Code code as follows:

BLL B = new BLL ();
B.listcolumn ();

We're going to bind the image shown above by just doing this

Copy Code code as follows:

ilist<qzzm.model.columninfo> list = new list<qzzm.model.columninfo> ();
foreach (Qzzm.Model.ColumnInfo m in B.listcolumn ())
{
if (m.depth.tostring ()!= "1")
{
M.columnname = Stringhelper.stringofchar (Convert.ToInt32 (m.depth)-1, "") + "├" + m.columnname + "<br>";
}
List. ADD (m);
}
Datalist1.datasource = list;
Datalist1.databind ();

Stringhelper or the top one. After such a simple processing, we can format the column name and then DataList binding effect Diagram, finished! It is the exchange between rookie and rookie.

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.