how to cast variable data in wp7withC#? - windows-phone-7

I have one variable and this variable contain multiple data.
I want to find perticuler one data so how i can find it?
my code is like
private void g1_Hold(object sender, Microsoft.Phone.Controls.GestureEventArgs e)
{
var item=(sender as StackPanel).DataContext;
}
Here item contain multiple data like id, name ,imagepath,songpath etc.
Now i want just imagepath so how i can get it.
Help me .
Thanks in advance.

Try this one.
Hope its working if you store data in class.
private void g1_Hold(object sender, Microsoft.Phone.Controls.GestureEventArgs e)
{
StackPanel stk = (StackPanel)sender;
ClassName sg = (ClssName)stk.DataContext;
string path=sg.imagepath;
}

Its Quite easy store data into array or list than find position of data
For example
EDIT:
ListBox lst = (ListBox)sender;
classname obj = (classname)lst.SelectedItem;
now you can use obj.anything you want

Related

Access Data Source Fields from Report code behind

I want to programmatically access the Fields collection in the ObjectDataSource object of my Telerik report.
I did notice in the design portion of the Telerik report you can access the fields collection
in the Value by using the Edit Expression window.
Example:
= Fields.MyFieldName
How would I accomplish this task using C# code in the report code behind file?
I had the same problem. This is how I solved it, although I believe there should be an easier way.
First I created a method for the details section itemdatabinding:
private void detail_ItemDataBinding(object sender, EventArgs e)
{
Telerik.Reporting.Processing.DetailSection section = (sender as Telerik.Reporting.Processing.DetailSection);
object id = section.DataObject["Data Field You want to access"];
Variable Name = id.ToString();
}
You can now use that variable anywhere in your codebehind.
Note: The Data Field must appear in your detail section. In my case I did not need it to show, so I just made 'Visible=false'.
This worked for me.
Bind Data to your own data to your variables
string ItemCode = "a";
string ItemDesc = "aa"
Then bind it to the data source
var Output = new
{
ItemCode = a.ItemCode,
ItemDesc = a.ItemDesc,
};
this.DataSource = Output;
All these are in code behind. Then move to design portion and modify your text-box as shown below.
= Fields.ItemCode
= Fields.ItemDesc

Isolated Storage to store recent lists, windows phone

enter code hereI have got isolated storage working but it's only storing one item.
I want to be able to store a list of favourites for the user to use in a list.
At the moment, i can store loads of stops, but when i open the application again, it only rememers the last item. and deletes the rest.
private void ApplicationBarFavouriteButton_Click(object sender, EventArgs e)
{
IsolatedStorageSettings settings = IsolatedStorageSettings.ApplicationSettings;
// txtInput is a TextBox defined in XAML.
if (!settings.Contains("userData"))
{
settings.Add("userData", busStopName.Text);
}
else
{
settings["userData"] = busStopName.Text;
}
settings.Save();
MessageBox.Show("Bus Stop was added to your favourites");
}
then for displaying the list
if (IsolatedStorageSettings.ApplicationSettings.Contains("userData"))
{
listFav.Items.Add(IsolatedStorageSettings.ApplicationSettings["userData"] as string);
}
EDIT:
private void ApplicationBarFavouriteButton_Click(object sender, EventArgs e)
{
IsolatedStorageSettings settings = IsolatedStorageSettings.ApplicationSettings;
List<string> favourites = settings["favourites"] as List<string>;
if (favourites == null)
{
favourites = new List<string>();
settings.Add("favourites", favourites);
}
favourites.Add(busStopName.Text);
settings["favourites"] = favourites;
}
displaying the data
if (IsolatedStorageSettings.ApplicationSettings.Contains("favourites"))
{
listFav.Items.Add(IsolatedStorageSettings.ApplicationSettings["favourites"] as List<string>);
}
You can access your settings like you would a hash/dictionary. So if your store information in settings["bob"], you will overwrite settings["bob"] when you next store something with the same key ("bob"). In your case, you're using the key "userData", every time you use settings["userData"] = "something";, you're overwriting what is stored in that key in the settings.
You could use something like the following (I've renamed your setting to "favourites" so that it is more descriptive of it's contents):
List<string> favourites;
settings.TryGetValue("favourites", out favourites);
if (favourites == null)
{
favourites = new List<string>();
settings.Add("favourites", favourites);
}
favourites.Add(busStopName.Text);
settings["favourites"] = favourites;
and for displaying it:
if (IsolatedStorageSettings.ApplicationSettings.Contains("userData"))
{
listFav.Items.AddRange(IsolatedStorageSettings.ApplicationSettings["favourites"] as List<string>);
}
You probably need to store a generic List of type string of Stops and then read this List from the ApplicationSettings, add a new stop to the List and then store the List back to the ApplicationSettings.

Value of Column Name DataGridView

I'm using Visual Studio 2010 and i'm developing a windows form. Right now I'm using a data grid view and i want to write some functions that would allow you to automaticlly edit the datagrid by just changing the text in the Datagrid view. Right now, I am able to get the actual value but I need the value of the column in order to use it as a parameter when i use ADO.net here's what my code looks like now
private void dgv_DataLookup_CellValueChanged(object sender, DataGridViewCellEventArgs e)
{
DialogResult dr;
dr = MessageBox.Show("Are you sure you want to edit this field?", "Edit Cell", MessageBoxButtons.YesNo);
if (dr == DialogResult.Yes)
{
DataGridViewCell editItemCell = dgv_DataLookup[dgv_DataLookup.CurrentCell.RowIndex, dgv_DataLookup.CurrentCell.ColumnIndex];
string editItem = editItemCell.Value.ToString();
}
}
this here gets me the value of the current cell that is currently selected. I tried doing something like this
DataGridViewColumns columnCells = dgv_DataLookup.CurrentCell.ColumnIndex.Value.ToString()... something that would represent this but actual code. thanks!
According to what I understand you want to edit the value within a field, you can choose the value in this way.
private void button2_Click(object sender, EventArgs e)
{
if (MessageBox.Show("Are you sure you want to edit this field?",Application.ProductName.ToString(),MessageBoxButtons.YesNo)== DialogResult.Yes)
{
string editItem = this.dataGridView1.Rows[this.dataGridView1.CurrentRow.Index].Cells["NameField"].Value.ToString();
}
}
Bye

Basic Eventhandler Question

Ok right now I have an object containing 3 strings, along with setters and getters. Now I have two questions -
First, I'm new to C# is there any way to optimize the following methods and make them more efficient?
void getSearchResults(object sender, RoutedEventArgs e)
{
string baseURL = "http://api.search.live.net/xml.aspx?Appid=<MyAPPID>&query=%22";
string companyName = ((TaxiCompany)sender).CoName;
string formatAndKey = "%22&sources=web";
WebClient c = new WebClient();
c.DownloadStringAsync(new Uri(baseURL + companyName + formatAndKey));
c.DownloadStringCompleted += new DownloadStringCompletedEventHandler(findTotalResults);
}
//Parses search XML result to find number of results
void findTotalResults(object sender, DownloadStringCompletedEventArgs e)
{
lock (this)
{
string s = e.Result;
XmlReader reader = XmlReader.Create(new MemoryStream(System.Text.UTF8Encoding.UTF8.GetBytes(s)));
String results = "";
while (reader.Read())
{
if (reader.NodeType == XmlNodeType.Element)
{
if (reader.Name.Equals("web:Total"))
{
results = reader.ReadInnerXml();
break;
}
}
}
}
}
Second, I'm initializing an object - new Taxi Company (String name, String Phone, String Results). I've got name and number and I need to call the above two functions to get noOfResults so that I can initialize the object. However, I seem to run into a bunch of issues with the event handlers.
I've primarily been a web dev, so there might be something really basic I'm missing here. I have a feeling setting up the bing methods to return a string back to the constructor might be th easiest, but not quite sure how.
First of all, you don't need the lock on the main page. Then, I would say that your XmlReader block should be replaced with the LINQ-to-XML variation called XDocument, that will allow you to access the XML document with a single, elegant line:
XDocument doc = XDocument.Parse(e.Result);
Once you have the document, you can check whether it contains a specific XNode.

Can't bind a GridView to a LINQ to SQL Result

OK, I am amittedly new to LINQ and have spent the last week reading everything I could on it. I am just playing around, trying to follow some examples I have found (a PDF from Scott Gu on the topic, in fact) and I am at a complete loss. Can someone please tell me why, when I bind a GridView to the query below, using the code below, I get no data?? I can see the results while debugging, so I know they are coming back from the DB, they are just not apparently binding correctly. I read something saying you could not bind directly to the result,and that you have to use a BindingSource as an intermediate step?
Someone, please tell me what I am missing here.
protected void Page_Load(object sender, EventArgs e)
{
SwapDBDataContext db = new SwapDBDataContext();
var users = from u in db.aspnet_Users
select new
{
Name = u.UserName,
ID = u.UserId
};
GridView1.DataSource = users;
GridView1.DataBind();
}
I am just using an empty GridView. I had assumed that the binding would take care of setting up the columns to match the resulting columns from the query - was that a stupid beginners mistake?
<asp:GridView ID="GridView1" runat="server">
</asp:GridView>
You should not have to convert to a List or Array. Binding requires, at a minimum, an IEnumerable<T>, which is what your Users variable is. Anonymous types are simply pre-compile place holders for compiler generated concrete types, so you should also be able to bind to anonymous types.
Your GridView may not have the AutoGeneratedColumns property set, which is what is required to have the data source define what columns appear. Try enabling that, and see if your GridView displays your queries results.
Cant understand why this shouldn't work. I whipped together a page using (almost) your code. It works perfectly for me.
protected void Page_Load(object sender, EventArgs e)
{
BlodsockerkollenDataContext db = new BlodsockerkollenDataContext();
var members = from m in db.Members
select new { Id = m.Id, Email = m.Email };
GridView1.DataSource = members;
GridView1.DataBind();
}
I don't agree with the suggested answers stating you should use ToList(). The GridView is capable of taking and traversing an IEnumerable, and IQueryable inherits IEnumerable.
And no, there should be no need to declare a BindingSource.
Maybe you have declared something in your GridView? Mine is just empty, pulled straight in from the toolbox:
<asp:GridView ID="GridView1" runat="server">
</asp:GridView>
protected void Page_Load(object sender, EventArgs e)
{
SwapDBDataContext db = new SwapDBDataContext();
GridView1.DataSource = from u in db.aspnet_Users
select new
{
Name = u.UserName,
ID = u.UserId
};
GridView1.DataBind();
}
Try doing
GridView1.DataSource = users.ToList();
or
GridView1.DataSource = users.ToArray();
Is possibly that the query isn't executing at all, because of deferred execution.

Resources