After the ilist query data, format the data and then bind the control

Source: Internet
Author: User

This articleArticleIt was sent to beginners of. Net or a friend who encountered similar problems, that is, how ilist formats and binds data. I have seen not many friends on the Internet talking about the most basic problems in this regard. Now let me simply put it,CodeI will intercept some of them. If you do not understand them, you can leave a message or contact me.

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:

 

Code
Foreach (Datarow Dr In DS. Tables [ 0 ]. Rows)
{
If (Dr [ " Depth " ]. Tostring () ! =   " 1 " )
{
Dr [ " Columnname " ] = Stringhelper. stringofchar (convert. toint32 (Dr [ " Depth " ]) -   1 , " " ) +   " Bytes "   + Dr [ " Columnname " ];
}
}
Gridview1.datasource = DS;
Gridview1.databind ();

 

Code
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 using STR </Param>
///   <Returns> </returns>
Public   Static   String Stringofchar ( Int Strlong, String Str)
{
String Returnstr =   "" ;
For ( Int I =   0 ; I < Strlong; I ++ )
{
Returnstr + = STR;
}

ReturnReturnstr;
}

///   <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 ^)

 

Code
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];

StringName=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 );

ReturnDS;
}

 

Then

 

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 example

 

Bll B =   New Bll ();
B. listcolumn ();

 

We only need to do this for binding as shown in.

 

Code
Ilist < Qzzm. model. columninfo > List =   New List < Qzzm. model. columninfo > ();

Foreach(Qzzm. model. columninfo mInB. 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.

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.