CYQ. Data lightweight Data layer custom MDataTable binding Chapter 7)

Source: Internet
Author: User

In this chapter, we will continue to explain the original design ideas of the entire framework:

This chapter is a continuation of the chapter, indicating that I have previously written, yes, and I have previously written the construction of the entire internal MDataTable. However,

The last step is to bind the MDataTable to the GridView/DataList/Repeater. This chapter will be continued!

Here is a list of articles I have previously written about the construction of custom MDataTable:

Note: The following content is written in the early stage, with few texts and many codes. For details, please leave a message after the article!

 

1: CYQ. Data lightweight Data access layer (2) construct a Data Unit (I)

2: CYQ. Data lightweight Data access layer (3) construct a Data Unit (bottom)

3: CYQ. Data lightweight Data access layer (4) construct Data unit Columns

4: CYQ. Data lightweight Data access layer (5) construct Data rows

5: CYQ. Data lightweight Data access layer (6) construct a Data table

6: CYQ. Data lightweight Data access layer (7) custom Data tables to bind Common Data Controls (I)

7: CYQ. Data lightweight Data access layer (8) custom Data tables to bind Common Data Controls (medium)

8: CYQ. Data lightweight Data access layer (9) custom Data tables to bind Common Data Controls (bottom)

 

After writing article 8 (9), the binding function is not completed in our test results. Let's take a look at the test code:

MDataTable table = new MDataTable ("myTableName ");
Table. Columns. Add ("Url", SqlDbType. NVarChar );
Table. Columns. Add ("Name", SqlDbType. NVarChar );

MDataRow MCM = table. NewRow ();
MCM [0]. Value = "http://cyq1162.cnblogs.com /";
MCM [1]. Value = "passing by autumn ";
Table. Rows. Add (MCM );
GridView1.DataSource = table;
GridView1.DataBind ();

 

Like a normal able, we add two columns and assign values to the columns:

Let's take a look at the test results:

Obviously, the binding results are messy, not what we want.

 

After code comparison, we found that our MDataRow must implement the IDataRecord interface. Therefore, let IDataRecord inherit the interface and implement:

Public class MDataRow: List <MDataCell>, IDataRecord
{
///... Omit N lines of existing code...

# Region IDataRecord Member

Int IDataRecord. FieldCount
{
Get
{
Return base. Count;
}
}

Bool IDataRecord. GetBoolean (int I)
{
Return (bool) this [I]. Value;
}

Byte IDataRecord. GetByte (int I)
{
Return (byte) this [I]. Value;
}

Long IDataRecord. GetBytes (int I, long fieldOffset, byte [] buffer, int bufferoffset, int length)
{
Throw new Exception ("The method or operation is not implemented .");
}

Char IDataRecord. GetChar (int I)
{
Return (char) this [I]. Value;
}

Long IDataRecord. GetChars (int I, long fieldoffset, char [] buffer, int bufferoffset, int length)
{
Return (long) this [I]. Value;
}

IDataReader IDataRecord. GetData (int I)
{
Throw new Exception ("The method or operation is not implemented .");
}

String IDataRecord. GetDataTypeName (int I)
{
Return (string) this [I]. Value;
}

DateTime IDataRecord. GetDateTime (int I)
{
Return (DateTime) this [I]. Value;
}

Decimal IDataRecord. GetDecimal (int I)
{
Return (decimal) this [I]. Value;
}

Double IDataRecord. GetDouble (int I)
{
Return (double) this [I]. Value;
}

Type IDataRecord. GetFieldType (int I)
{
Return this [I]. Value. GetType ();
}

Float IDataRecord. GetFloat (int I)
{
Return (float) this [I]. Value;
}

Guid IDataRecord. GetGuid (int I)
{
Return (Guid) this [I]. Value;
}

Short IDataRecord. GetInt16 (int I)
{
Return (short) this [I]. Value;
}

Int IDataRecord. GetInt32 (int I)
{
Return (int) this [I]. Value;
}

Long IDataRecord. GetInt64 (int I)
{
Return (long) this [I]. Value;
}

String IDataRecord. GetName (int I)
{
Return (string) this [I]. Value;
}

Int IDataRecord. GetOrdinal (string name)
{
Return (int) this [name]. Value;
}

String IDataRecord. GetString (int I)
{
Return (string) this [I]. Value;
}

Object IDataRecord. GetValue (int I)
{
Return this [I]. Value;
}

Int IDataRecord. GetValues (object [] values)
{
Return 0;
}

Bool IDataRecord. IsDBNull (int I)
{
Return this [I]. Value = DBNull. Value;
}

Object IDataRecord. this [string name]
{

Get
{
Return this [name]. Value;
}
}

Object IDataRecord. this [int I]
{
Get
{
Return this [I]. Value;
}
}

# Endregion
}

 

Then I browsed the page, but it didn't have any effect.

 

Then I compared the code and found that the original MDataTable adopts the Inheritance Method List <MDataRow>,

So I got it below:

Public class MDataTable: IDataReader, IEnumerable
{
Private List <MDataRow> _ MCM;
Public List <MDataRow> Rows
{
Get
{
Return _ MCM;
}
}
//... N rows are omitted below...
}

 

 

Then I made a slight adjustment and browsed it again. The effect finally came out:

 

 

 

Now, the MDataTable series customized in the three parts of the framework is over.

 

 

Note: The complete framework source code will be published in another chapter after the end of this series. If you are not excited for the time being, it is important to learn your thoughts.

If you find any problems during the learning process, please leave a message!

 

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.