An interesting problem I keep coming across is how to re-order an arraylist item up or down, it's something I've needed to use on a number of applications, so I figured it's about time I got my head round the problem, and it's surprisingly simple.
First we create an Arraylist and populate it:
ArrayList MyRA = new ArrayList();
MyRA.Add("Item1");
MyRA.Add("Item2");
MyRA.Add("Item3");
MyRA.Add("Item4");
MyRA.Add("Item5");
Next we identify the item we want to move:
String myItem = "Item4";
And create an integer to hold the position of the item:
Int32 myIndex = new Int32();
Now get the position:
myIndex = MyRA.IndexOf(myItem);
Almost there, now we insert the Item at that the position before it's current on (Position - 1):
MyRA.Insert((myIndex - 1), myItem);
Finally we need to remove the old item, it's position would have moved up one since we've added an extra item:
MyRA.RemoveAt((myIndex + 1));
So if you're in a hurry and just want the full code here you go:
ArrayList MyRA = new ArrayList();
String myItem = "Item4";
Int32 myIndex = new Int32();
MyRA.Add("Item1");
MyRA.Add("Item2");
MyRA.Add("Item3");
MyRA.Add("Item4");
MyRA.Add("Item5");
myIndex = MyRA.IndexOf(myItem);
MyRA.Insert((myIndex - 1), myItem);
MyRA.RemoveAt((myIndex + 1));
Of course this only moves items down the list, to move them up simply reverse the numbers on the last two lines:
MyRA.Insert((myIndex + 1), myItem);
MyRA.RemoveAt((myIndex - 1));
Happy Coding :0)
Tags: ArrayList, Re-order, c#
Be the first to rate this post
- Currently 0/5 Stars.
- 1
- 2
- 3
- 4
- 5
I just came across this article on how to use a required field validator on a dropdownlist control - I think the beauty of it is it's simplicity:
By setting the Required Field's InitialValue property to be the same as the Dropdownlist's default value, we can force the validator to "kick off" if the value isn't changed.
Thanks to Bilal Haidar for the solution:
<asp:DropDownList id="DropDownList1" runat="server">
<asp:ListItem Value="-1">--> Choose <--</asp:ListItem>
<asp:ListItem Value="1">Lebanon</asp:ListItem>
<asp:ListItem Value="2">Kuwait</asp:ListItem>
</asp:DropDownList>
<asp:RequiredFieldValidator id="RequiredFieldValidator1"
runat="server" ErrorMessage="You must select a valid option"
ControlToValidate="DropDownList1" InitialValue="-1" Display="Dynamic" />
<P>
<asp:Button id="Button1" runat="server" Text="Post Back" />
</P>
Validate DropDownList - Bilal Haidar [MVP]
Tags: Dropdownlist, RegularFieldValidator, validation, ASP.NET 2.0, MVP
Be the first to rate this post
- Currently 0/5 Stars.
- 1
- 2
- 3
- 4
- 5
If your using a formview on a page it might be useful to set the default mode dynamically depending on whether the datasource returns a record or not.
For example I'm using a formview to populate a table - if a record already exists for my criteria I want to edit it, otherwise I want to add a new one.
I could test the datasource directly but I thought it would be neater to actually test the formview itself after it's databound. i.e.
protected void FormView1_DataBinding(object sender, EventArgs e)
{
if (FormView1.DataItemCount < 1)
{
FormView1.DefaultMode = FormViewMode.Insert;
}
else
{
FormView1.DefaultMode = FormViewMode.Edit;
}
}
Works a treat - I guess there could be lots of ways to achieve this, if anybody has any let me know. But it works for me :0)
Ta
Dean
Blogged with Flock
Tags: c#, formview, databinding, datasource, defaultmode
Be the first to rate this post
- Currently 0/5 Stars.
- 1
- 2
- 3
- 4
- 5
I've just found an interesting problem with my SQL2005 Management Studio. For whatever reason (I still don't know) somebody decided to reduce my domain permissions on my work login.
This had the effect of stopping me accessing quite a few of the applications on my 'start menu' because they were mapped to "my computer name + c$" e.g ""\\me\c$\Program Files\...etc". and I didn't have permission to access mine or any other pc from the network.
This problem stopped me from running most applications from the start - but SQL2005 had the strange problem that it would run almost totally then give me an error "Failed to Load". After a bit of searching around the forums, most people were suggesting full re-install of both Visual Studio and SQL2005. I simply didn't have a day spare to do all this so I thought I'd try one of the other solutions which was to change the application path to "c:\Program Files\... etc"
And it works. :0)
Blogged with Flock
Tags: SQL2005, Domain Permissions, Failed to Load
Be the first to rate this post
- Currently 0/5 Stars.
- 1
- 2
- 3
- 4
- 5
I've just had to fix a colleagues code, which had the same problem that I'd been struggling with yesterday - it's another one of those annoying quirks that has you tearing your hair out because you look at your code and even though it compiles it still wont work.
The issue was that we are trying to populate a dropdown list dynamically - using the c# funtion
dropdownlist.items.add("Text","value");
Although this does compile it didn't seem to work - I stand to be corrected.
The way I got round it was to create a new listitem object and populate it with my text and value. i.e
dropdownlist.items.add(new ListItem("Text","value"));
Hope this helps somebody cus it's such an annoying thing to get stuck with - another point was that the value must always be a string - you can cast it when you retrieve it - (Convert.ToInt32(dropdownlist.selectedItem.value) but it seems to only want strings (I guess this is because the value is going to be rendered into HTML so any cast information will be obliterated anyway. A Little like the classic ASP Response.Write("Some HTML") used to work.
Blogged with Flock
Be the first to rate this post
- Currently 0/5 Stars.
- 1
- 2
- 3
- 4
- 5
I've had this problem a few times so I thought I'd actually get round to blogging it.
Displaying Dates on a Datagrid is a nightmare when you don't want to display the full date and time in dd/mm/yyyy hh:mm:ss format.
There is a useful little property in the datagrid items editor which allows you to specify the format of the string using {0:d} formatting.
The problem I've found was that no matter what I did, it didn't make a bit of difference to the output. The solution after much googling was was set one of the other properties..
The trick is to set HTMLEncode to False - as if by magic it works.
Blogged with Flock
Be the first to rate this post
- Currently 0/5 Stars.
- 1
- 2
- 3
- 4
- 5
Assumed consent is evil and maybe illegal
One of the best ways for an internet-based company to enter the Social
Media world is through the creation of a Facebook application. A
successful app will drive traffic to the company's native site, create
direct sales, and promote online reputation.
So, what exactly is meant by a Facebook app? Well, it could be
a simple as the Vampires application which allows members to "bite" one
another and become vampires. This application is fun, simple, and has
gone viral (in the sense that it has become very popular). The
application is monetized by way of advertising horror movies in the
application's interface, and because Facebook is all about sharing, all
of one's friends can see one's Vampiric status via the same interface.
But now complaints regarding privacy are starting to surface
in regard to these apps. Facebook applications can be based solely on
direct sales. Companies like Overstock.com allow--or should I say
encourage--Facebook users to purchase from their website through their
application without ever leaving Facebook.com. The problem is that
purchase information is being shared. After making a purchase, the
Overstock.com app displays a small box in a corner of the browser
interface following a transaction. This box alerts users that
information will be shared with other Facebook users unless they click
on it to negate that information form being shared. The box fades away
after a half minute or so, after which consent is assumed, and all your
friends can see what you bought.
I'm no lawyer, but that sounds kind of "iffy" to me. And, even
if it is a legally binding procedure, it certainly isn't going to do
much for customer satisfaction! There are, evidently, other large
e-commerce sites with similar or identical interfaces, and my instinct
is that Facebook and these large companies will solve the problem
quickly. It makes a good example, however, of social media gone awry.
The term "social" does not automatically imply that sharing is the
default; part of being social is having the choice to share or not share.
Purchasing things, especially during the holidays, often involves
gifts, and the surprise of a well-chosen gift to a friend (who may well
be in your Facebook world) is as social as it gets.
Be the first to rate this post
- Currently 0/5 Stars.
- 1
- 2
- 3
- 4
- 5