1. First, let's talk about the most basic dataset binding operation. The gridview is used as an example.
After an operation queries a dataset ds, You Can format some data, such:
CopyCode The Code is as follows: foreach (datarow DR in DS. Tables [0]. Rows)
{
If (Dr ["depth"]. tostring ()! = "1 ")
{
Dr ["columnname"] = stringhelper. stringofchar (convert. toint32 (Dr ["depth"])-1, "") + "comment" + Dr ["columnname"];
}
}
Gridview1.datasource = Ds;
Gridview1.databind ();
Copy codeThe Code is as follows: public static class stringhelper
{
/// <Summary>
/// Generate a string of the specified length, that is, generate strlong STR strings
/// </Summary>
/// <Param name = "strlong"> generated length </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 a date random code
/// </Summary>
/// <Returns> </returns>
Public static string getramcode ()
{
# Region
Return datetime. Now. tostring ("yyyymmddhhmmssffff ");
# Endregion
}
}
In this way, you can quickly format and bind some data to the control.
2. Now let's talk about what I want to talk about. What should we do if we want to format some strings after we use ilist to query data to achieve the desired display effect? Let's take a look at the answer.
For example, when we do unlimited classification, what should we do if we want to display the data as shown.
I just asked the dataset to write some formatting code. Now I believe everyone wants to know how to operate ilist, right?
I won't talk about the power of generic functions here. Now, there are two main methods to achieve this,
1. Convert the data obtained by ilist to dataset, so that you can return to the familiar operation again.
Ilist to dataset class (this is what I reference to others ^)
Copy code The Code is 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 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;
}
ThenCopy codeThe Code is as follows: DataSet DS = converttodataset (B. listcolumn ());
Now, let's get back to the familiar operation. However, it's not a detour to start this operation. Why not use dataset directly? That's right. We have taken a detour... I want to use ilist. I don't want to turn around. Isn't that a good solution?
Haha, the answer is certainly yes, and it is also the simplest. It is often because beginners do not understand ilist,
Now we will talk about the operation of the ilist object class.
Or the above example
For exampleCopy codeThe Code is as follows: Bll B = new BLL ();
B. listcolumn ();
We only need to do this for binding as shown in.
Copy code The Code is 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, "") + "bytes" + M. columnname + "<br> ";
}
List. Add (m );
}
Datalist1.datasource = List;
Datalist1.databind ();
Stringhelper is the one above. After such a simple process, we can format the topic name and then split the datalist binding! It's a matter of communication between cainiao and cainiao.