We are in the process of doing project development, often encounter a lot of information on the selection of items recorded in the user's relevant settings of the problem, the more stupid way is that each entry corresponds to the user set a property field in the table, but such a design seems to be very poor, efficiency is not good where to go, This provides a quick way to store N records, using an integer data to record the selection status of an entry.
Content:
First, the table structure:
Two table
Item Item table
Key:id
Field Name
Type
Length
Default
Description
Id
Int
4
ID (AutoNumber)
ItemName
Nvarchar
50
0
Entry name
UserInfo User Information table
Key:userid
Field Name
Type
Length
Default
Description
Userid
Int
4
ID (AutoNumber)
Itemrecord
Int
4
0
Entry Selection Record
A note: Our goal is to put the user on the item all the records of the selection of records, after a certain amount of processing stored in the Itemrecord.
Note: The above code is to read the entry information and personal settings for the corresponding processing, Selectitemarray is a private method, the code is as follows:
#region Get the selected item recordset
<summary>
Get the selected item recordset
</summary>
<param name= "Tempitemidarray" > All project Recordsets </param>
<param name= "Tempitemrecord" > Project selection Information </param>
If you haven't read the above code, look at me now to explain it to you. The solution here is to use the binary data bits "0" "1" to identify whether or not to select a purpose, if selected is "1", each entry corresponds to a bits, if there are five pieces of data in the item, then all is "11111", such binary data, if we want to select the first, The second and fifth data, then the corresponding binary code is represented as "10101", and then we convert this binary data to decimal data: 16+0+4+0+1=21, so we can record various combinations of records by converting them. Note here is the question of the new entry, what if the item now becomes seven? Do not worry, become seven, then there are seven bit "1111111", then this time my original selection of three data is not to change, this increase of two bit is increased in the high, that is, now the storage representation into the "0010101" is: 0+0+16+0+4+0+1= 21, the value is unchanged. In general, we are adding entries, rarely reduce the situation of the entry, if you want to reduce the entry, you must write a program to reset the value of the Itemrecord.
So how do you reset the Itemrecord value after the user selects the entry again? Listen to me slowly, like this multiple selection of items are usually bound by CheckBoxList to select operations, we are the object of the element selection process, the code fragment as follows:
int itemcount=checkboxlist_item.items.count;
for (int i=0;i<itemcount;i++)
{
if (checkboxlist_item.items[i). Selected)
{
itemselect=itemselect+ "1";
}
Else
{
itemselect=itemselect+ "0";
}
}
int Itemrecord=convert.toint32 (ItemSelect, 2);
One point: First get the number of entries, and then do the loop, to determine whether the selected, if selected then the corresponding bit value is "1", otherwise "0", combining the binary string and then make a conversion, and then warehousing on all the finished.
Is it convenient? Not only is the convenience of this storage efficiency very high, if you have hundreds of entries to choose from, this scheme is a good choice, but if you really get hundreds of entries let others choose, unless it is forced to cope with the test, otherwise will encounter strong contempt, who has the patience to do this choice, haha!
Don't worry, give a reminder to see, do not make the order wrong, binding checkboxlist, in accordance with the order of DESC to bind elements list, otherwise, ho ho ...
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.