📄 cb199910hh_f.asp.htm
字号:
Object Inspector (see Figure 1). </P>
<P class=BodyText> </P>
<P class=Captions><IMG align=left height=202
src="image/cb199910hh_f_image002.jpg" width=333
tppabs="http://www.cbuilderzine.com/features/1999/10/cb199910hh_f/cb199910hh_f_image002.jpg">
<BR clear=all style="mso-ignore: vglayout"><B>Figure 1:</B> Creating an
index for the ItemsTotal field. </P>
<P class=BodyText> </P><BR clear=all>
<P class=BodyText>The key properties of <I>TIndexDef</I> are
<I>Fields</I>, <I>Name</I>, <I>DescFields</I>, <I>CaseInsFields</I>, and
<I>Options</I>. The <I>Name</I> property determines the name of the index
and corresponds with the <I>IndexName</I> property of ClientDataSet. To
activate an index, set the <I>IndexName</I> property to the name of the
index. Each index in the <I
style="mso-bidi-font-style: normal">IndexDefs</I> collection must have a
unique name. </P>
<P class=BodyText> </P>
<P class=BodyText>The <I style="mso-bidi-font-style: normal">Fields</I>
property contains the names of the fields involved in the index. You can
create an index with more than one field by separating the fields with
semicolons in the Object Inspector. By default, fields listed in the
<I>Fields</I> property will be sorted in ascending order and with case
sensitivity. There are times when you may want to create an index composed
of multiple fields where some of the fields are sorted in ascending order
and some in descending order. This is what the <I>DescFields</I> property
is for. Fields that are listed in <I>DescFields</I> will be sorted in
descending order. The <I>CaseInsFields</I> property works in a similar
manner. Fields listed in the <I>CaseInsFields</I> property will be sorted
without regard to case sensitivity. </P>
<P class=BodyText> </P>
<P class=BodyText>The <I style="mso-bidi-font-style: normal">Options</I>
property allows you to configure the behavior of the index. You can
specify whether the index sorts in descending or ascending order, and
whether the sorting is case-sensitive. You can also force uniqueness among
records (see Figure 2). </P>
<P class=BodyText> </P>
<P class=Captions><IMG align=left height=202
src="image/cb199910hh_f_image004.jpg" width=333
tppabs="http://www.cbuilderzine.com/features/1999/10/cb199910hh_f/cb199910hh_f_image004.jpg">
<BR clear=all style="mso-ignore: vglayout"><B>Figure 2: </B>Creating a
descending index for the ItemsTotal field. </P>
<P class=BodyText> </P><BR clear=all>
<P class=BodyText>Figures 1, 2, and 3 illustrate how you can use the
Object Inspector to configure the index definitions for a ClientDataSet.
Each figure shows indexes that have been set up for the Orders database
table. Figure 1 shows a simple, ascending index for the ItemsTotal field.
The index in Figure 2 sorts by ItemsTotal, but in descending order. The
index in Figure 3 demonstrates an index that sorts by ShipVIA in ascending
order and by ItemsTotal in descending order. </P>
<P class=BodyText> </P>
<P class=Captions><IMG align=left height=188
src="image/cb199910hh_f_image006.jpg" width=333
tppabs="http://www.cbuilderzine.com/features/1999/10/cb199910hh_f/cb199910hh_f_image006.jpg">
<BR clear=all style="mso-ignore: vglayout"><B>Figure 3:</B> Creating a
composite index on ShipVIA and ItemsTotal. </P>
<P class=BodyText> </P><BR clear=all>
<P class=BodyText>Before we move on, here are some key points to keep in
mind regarding indexes: </P>
<UL>
<LI>All fields in the index should be listed in the <I>Fields</I>
property of <I>TIndexDef</I>.
<LI>If you add a field to the <I>DescFields</I> property, it must also
be listed in the <I>Fields</I> property.
<LI>If you add a field to the <I>CaseInsFields</I> property, it must
also be listed in the <I>Fields</I> property.
<LI>Only use <I>CaseInsFields</I> and <I
style="mso-bidi-font-style: normal">DescFields</I> for indexes that
contain multiple fields. For a single field index, use the
<I>Options</I> property.
<LI>Indexes created with the <I>IndexDefs</I> property persist even when
you close the ClientDataSet. </LI></UL>
<P class=BodyText> </P>
<P class=Subheads>Creating an Index with the <I>IndexDefs</I> Property</P>
<P class=BodyText>The <I style="mso-bidi-font-style: normal">IndexDefs</I>
property is nice, but it can be cumbersome if you need to create several
indexes for a single ClientDataSet. In some cases, it might be easier to
create an index programmatically at run time. The code in Figure 4
demonstrates how to create the same three indexes from Figures 1 through
3. </P>
<P class=BodyText> </P>
<P class=Code><I><SPAN class=CodeBlue>// Creating the index in Figure 1.
</SPAN></I></P>
<P class=Code>TIndexDef *Index1 =
ClientDataSet1->IndexDefs->AddIndexDef();</P>
<P class=Code>Index1->Name = "Index1"; </P>
<P class=Code>Index1->Fields = "ItemsTotal"; </P>
<P class=Code> </P>
<P class=Code><I><SPAN class=CodeBlue>// Creating the index in Figure 2.
</SPAN></I></P>
<P class=Code>TIndexDef *Index2 =
ClientDataSet1->IndexDefs->AddIndexDef();</P>
<P class=Code>Index2->Name = "Index2"; </P>
<P class=Code>Index2->Fields = "ItemsTotal"; </P>
<P class=Code>Index2->Options = TIndexOptions() << ixDescending;
</P>
<P class=Code> </P>
<P class=Code><I><SPAN class=CodeBlue>// Creating the index in Figure 3.
</SPAN></I></P>
<P class=Code>TIndexDef *Index3 =
ClientDataSet1->IndexDefs->AddIndexDef();</P>
<P class=Code>Index3->Name = "Index3"; </P>
<P class=Code>Index3->Fields = "ShipVIA;ItemsTotal"; </P>
<P class=Code>Index3->CaseInsFields = "ItemsTotal"; </P>
<P class=Captions><B>Figure 4:</B> Creating indexes at run time with
<I>IndexDefs</I>.</P>
<P class=BodyText> </P>
<P class=Subheads>Creating an Index with <I>AddIndex</I></P>
<P class=BodyText>The <I style="mso-bidi-font-style: normal">AddIndex</I>
method gives you another way to create indexes for a ClientDataSet. It has
some restrictions, however. First, the ClientDataSet must be active when
you create an index with <I
style="mso-bidi-font-style: normal">AddIndex</I>; calling <I>AddIndex</I>
on a closed dataset generates an exception. Secondly, indexes created with
<I>AddIndex</I> will be deleted when you close the dataset. Lastly,
indexes created with <I style="mso-bidi-font-style: normal">AddIndex</I>
aren't saved with the data if you call the <I>SaveToFile</I> method of
ClientDataSet. </P>
<P class=BodyText> </P>
<P class=BodyText>Figure 5 shows how to create the same three indexes from
the previous examples. Notice that the properties of <I>TIndexDef</I> have
corresponding arguments in the <I>AddIndex</I> function. </P>
<P class=BodyText> </P>
<P class=Code><I><SPAN class=CodeBlue>// Creating the index in Figure 1
with AddIndex. </SPAN></I></P>
<P class=Code>ClientDataSet1->AddIndex("Index1","ItemsTotal", </P>
<P
class=Code> TIndexOptions(),</P>
<P
class=Code>
"","",0); </P>
<P class=Code> </P>
<P class=Code><I><SPAN class=CodeBlue>// Creating the index in Figure 2
with AddIndex. </SPAN></I></P>
<P class=Code>ClientDataSet1->AddIndex("Index2","ItemsTotal", </P>
<P
class=Code> TIndexOptions()
<< ixDescending, </P>
<P
class=Code> "","",0);
</P>
<P class=Code> </P>
<P class=Code><I><SPAN class=CodeBlue>// Creating the index in Figure 3
with AddIndex. </SPAN></I></P>
<P class=Code>ClientDataSet1->AddIndex("Index3","ShipVIA;ItemsTotal",
</P>
<P
class=Code> TIndexOptions(),</P>
<P
class=Code> "ItemsTotal","",0);
</P>
<P class=Captions><B>Figure 5:</B> Creating indexes at run time with
<I>AddIndex</I>.</P>
<P class=BodyText> </P>
<P class=BodyText>In C++Builder 3, ClientDataSet doesn't provide an
<I>IndexDefs</I> property. If you're still using C++Builder 3, your only
choice is the <I>AddIndex</I> method. For C++Builder 4, you probably want
to use the <I>IndexDefs</I> whenever possible. </P>
<P class=BodyText> </P>
<P class=Subheads>Example One: A Simple Sort</P>
<P class=BodyText>When you set the <I>IndexName</I> property of
ClientDataSet, data in the dataset is sorted based on the selected index.
You can use this knowledge to create programs that allow users to sort
data without re-fetching the data from the database. In this example,
we'll create a program that allows users to sort the Orders table by one
of two columns. Users select which column to sort by clicking a radio
button. Figure 6 shows the main form of the project at design time. </P>
<P class=BodyText> </P>
<P class=Captions><IMG align=left height=154
src="image/cb199910hh_f_image008.jpg" width=333
tppabs="http://www.cbuilderzine.com/features/1999/10/cb199910hh_f/cb199910hh_f_image008.jpg">
<BR clear=all style="mso-ignore: vglayout"><B>Figure 6: </B>The main form
for the Example 1 project at design time. </P>
<P class=BodyText> </P><BR clear=all>
<P class=BodyText>To support sorting, the program must do three things:
</P>
<P class=BodyText>1) Fetch data into
a ClientDataSet from a source dataset through a provider. </P>
<P class=BodyText>2) Create indexes
on the ClientDataSet. </P>
<P class=BodyText>3) Set the
<I>IndexName</I> property of the ClientDataSet to sort the data. </P>
<P class=BodyText> </P>
<P class=BodyText>A Query component acts as the source dataset. The Query
reads in all the records from the Orders table, with its <I>SQL</I>
property set to: </P>
<P class=BodyText> </P>
<P class=Code>SELECT * FROM Orders</P>
<P class=BodyText> </P>
<P class=BodyText>The main form also contains ClientDataSet and Provider
components. The <I>Provider</I> property of <I>ClientDataSet1</I> is set
to Provider1, which is the name of the provider control. The
<I>DataSet</I> property of <I>Provider1</I> is set to Query1. These three
controls form the ClientDataSet-Provider-DataSet bridge, which we
discussed earlier. The ClientDataSet control will fetch its data from the
source dataset through its provider. The source dataset in this example is
the Query component. </P>
<P class=BodyText> </P>
<P class=BodyText>The main form also contains a DBGrid control (see Figure
6). The grid is wired to the ClientDataSet through the DataSource. It's
crucial that we connect the grid to the ClientDataSet - not the Query
component. When we sort the ClientDataSet, the Query component isn't
affected. If we connect the grid to the Query component, users won't see
the effects of our sorting. In fact, they won't see any data, because the
Query component won't be active once the records are loaded into the
ClientDataSet. </P>
<P class=BodyText> </P>
<P class=BodyText><A
href="cb199910hh_f.asp.htm#ListingOne">Listing
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -