On gridview updated get all modified cells in a textbox + non edited? - c#

I'm trying to add in a textbox all values modified in cells + the others who aren't edited:
table name : info
columns : Name|Subname|Age|Birthday|Code
I'm using on grid view update
for(int i =0;i<4;i++){
if(e.OldValues[i].ToString() != e.NewValues[i].ToString())
{
Textbox1.text = GridView1.HeaderRow.Cells[i].Text + " " + e.OldValues[i].ToString() + " edited with " + e.NewValues[i].ToString();
}
It's not working I'm getting information just from 1 edited cell , if I edit 2 cells from a row I get random cell information , I mean If I edit Name and Sub Name I get Name old value and new value istead of getting both.
I want to add + the other values who aren't edited.
thanks

You could use LINQ to get the necessary informations:
var changed = e.NewValues.Cast<DictionaryEntry>()
.Where(entry => entry.Value != e.OldValues[entry.Key])
.Select(entry => String.Format("{0} was edited with {1}",entry.Key,entry.Value));
var notChanged = e.NewValues.Cast<DictionaryEntry>()
.Where(entry => entry.Value == e.OldValues[entry.Key])
.Select(entry => String.Format("{0} was not changed",entry.Key));
TxtChanged.Text = String.Join(Environment.NewLine, changed);
TxtUnchanged.Text = String.Join(Environment.NewLine, notChanged);

Right now you are not "adding" to the textbox, you are overwriting it with "="
Textbox1.text = GridView1.HeaderRow.Cells[i].Text + " " + e.OldValues[i].ToString() + " edited with " + e.NewValues[i].ToString();
Try this:
Textbox1.text += GridView1.HeaderRow.Cells[i].Text + " " + e.OldValues[i].ToString() + " edited with " + e.NewValues[i].ToString();
Changed the "=" for "+=".
And for the non edited values, something like? :
if(e.OldValues[i].ToString() != e.NewValues[i].ToString())
{
Textbox1.text += GridView1.HeaderRow.Cells[i].Text + " " + e.OldValues[i].ToString() + " edited with " + e.NewValues[i].ToString();
}
else
{
Textbox2.text += GridView1.HeaderRow.Cells[i].Text + " " + e.OldValues[i].ToString() + " didn't change";
}

Related

Not getting the complete result of selected records with single quotes inside IN operator

I have the below code:
List<string> queryEventIDList=new List<string>();//added new list<string>
foreach (ListItem lstAssign in lstEvent.Items)
{
if (lstAssign.Selected == true)
{
queryEventIDList.Add(lstAssign.Value);
logfield = logfield + "," + lstEvent.SelectedItem.Text;
}
}
string queryEventIDs = string.Join(",", queryEventIDList.ToArray());
From queryEventIDs, I am getting the selected values. I have a below mentioned query line in where condition. Suppose I am selecting A and B,it gives me the result of only A.
" (('" + queryEventIDs + "'='') OR (inq1.event_id in('" + queryEventIDs + "'))) AND " + //queryeventids(A,B)//skips result of B
So I slightly modified that line. I removed single quotes('') from it.
" (('" + queryEventIDs + "'='') OR (inq1.event_id in(" + queryEventIDs + "))) AND " + //Queryeventids(A,B)//gives both result
So this gave me the both selected results of A and B. This is strange. What is the difference for inq1.event_id in('" + queryEventIDs + "' with and without single quotes?
I am a beginner in C#.
Adding Replace solved my issue
" (('" + queryEventIDs + "'='') OR (inq1.event_id in('" + queryEventIDs.Replace(",", "','") + "'))) AND " +

Windows Form ListView - How to get values of selected Item

I am currently trying to create an app for a shopping basket where you enter an item and then a price in two boxes. On hitting the add button the Item is Added to a List View. You can do this multiple times to add more items to your Basket.
If you decide that you don't want an item in your basket i have a remove button which removes the item which you have selected by clicking on it. My problem is that no matter what item you remove, the values of the last item that you add is taken from the Total instead of the correct one.
My Code for adding an item here:
decimal total = numberPicker.Value * priceSetter.Value;
MessageBox.Show("You want " + numberPicker.Value + " " + txtName.Text + " which cost £" + priceSetter.Value + " each. Your Total cost is: £" + total, "My App");
ShoppingCart.Items.Add("Item: " + txtName.Text + " " +
"Value: " + priceSetter.Value + " " +
"Quantity: " + numberPicker.Value + " " +
"Total: " + (priceSetter.Value * numberPicker.Value)).ToString();
runningTotal = runningTotal + total;
resultBox.Text = runningTotal.ToString();
File.AppendAllText(#"C:\ShoppingCartExperiment\Reciept.txt", ("Item: " + txtName.Text + " " + "Price: £" + priceSetter.Value + " " + "Quantity: " + numberPicker.Value + " " + " Running Total: £" + runningTotal + Environment.NewLine));
My Code for the removal is here:
var selectedItem = ShoppingCart.SelectedItem;
ShoppingCart.Items.Remove(selectedItem);
decimal total = numberPicker.Value * priceSetter.Value;
runningTotal = runningTotal - total;
var SelectedTotal = (priceSetter.Value * numberPicker.Value);
File.AppendAllText(#"C:\ShoppingCartExperiment\Reciept.txt", ("You removed " + txtName.Text + Environment.NewLine));
MessageBox.Show(txtName.Text + " has been removed from basket");
File.AppendAllText(#"C:\ShoppingCartExperiment\Reciept.txt", ("You new total is: £" + runningTotal + Environment.NewLine));
My Project is very small so these two pieces of code are almost all that i have.
My Thought on this is that whenever i add a new Item, my priceSetter and NumberPicker (quantity) are being over ridden. Selecting the item does not seem to store the values of the item that i have selected, but only of what I added last.
I am quite new to coding to please excuse any Naivety. All help is Appreciated.

WPF saving inputs & updating

I've already got the the point where I can save all inputs to a text file but now I would like to create another feature where I can update that file but only through the form itself
private void button_Click(object sender, RoutedEventArgs e)
{
string lb1 = "";
string lb = "";
//Removes listboxtitem object name
ListBoxItem mySelectedItem0 = listBox.SelectedItem as ListBoxItem;
ListBoxItem mySelectedItem1 = listBox1.SelectedItem as ListBoxItem;
if (mySelectedItem0 != null && mySelectedItem1 != null)
{
lb1 = mySelectedItem1.Content.ToString();
lb = mySelectedItem0.Content.ToString();
}
TextWriter saveText = new StreamWriter(#"D:\blah.txt");
saveText.Write("===================================[Receipt]===================================" + "\r\n");
saveText.Write("|Title:" + " " + comboBox.Text + "\r\n");
saveText.Write("|Surname:" + " " + textBox1.Text + "\r\n");
saveText.Write("|Forname:" + " " + textBox2.Text + "\r\n");
saveText.Write("|Passanger[s]:" + " " + textBox6.Text + "\r\n");
saveText.Write("|Group:" + " " + checkBox.IsChecked + " " + "No: " + textBox4.Text + "\r\n");
saveText.Write("|Class:" + " " + comboBox1.Text + "\r\n");
saveText.Write("|Luggage:" + " " + textBox7.Text + "\r\n");
saveText.Write("|Suitcase:" + " " + checkBox1.IsChecked + " " + "No: " +textBox5.Text + "\r\n");
saveText.Write("|Departure On:" + " " + DatePicker.Text + " " + "At:" + " " +comboBox2.Text + "\r\n");
saveText.Write("|Destination From:" + " " + lb + " " + "To:" + " " + lb1 + "\r\n");
saveText.Write("===================================[Receipt]===================================");
saveText.Close();
File.AppendAllText(#"D:\blah.txt", Environment.NewLine);
}
so here it safes all inputs and values as strings into a blah.txt file, how can I now retrieve all information from the file into the form and update it?
You can read the entire content of a text file by using ReadAllText function. Also, you can condense your text construction:
String ticketText = String.Format(#"
===================================[Receipt]====================================
|Title: {0}
|Surname: {1}
|Forname: {2}
...
...
", comboBox.Text, textBox1.Text, textBox2.Text, ...);
saveText.Write(ticketText);
This way of constructing your text is better since everything is done at once and it is easier to read. This will be made even easier when with interpolated strings.
Also I recommend naming your control by their meaning: e.g. txtTitle or TitleText for Title.
In your code you are using 2 different methods to write to a file in one process. You use a StreamWriter to save the receipt. And then static File.AppendAllText to attach an empty line. No need to do that. I think it would be better to generate the message in a separate method and then use the static File.AppendAllText to save everything in one go:
private string GetReceipt()
{
StringBuilder receiptBuilder = new StringBuilder();
receiptBuilder.AppendLine("===================================[Receipt]===================================");
receiptBuilder.AppendLine("|Title:" + " " + comboBox.Text);
receiptBuilder.AppendLine("|Surname:" + " " + textBox1.Text);
receiptBuilder.AppendLine("|Forname:" + " " + textBox2.Text);
receiptBuilder.AppendLine("|Passanger[s]:" + " " + textBox6.Text);
receiptBuilder.AppendLine("|Group:" + " " + checkBox.IsChecked + " " + "No: " + textBox4.Text);
receiptBuilder.AppendLine("|Class:" + " " + comboBox1.Text);
receiptBuilder.AppendLine("|Luggage:" + " " + textBox7.Text);
receiptBuilder.AppendLine("|Suitcase:" + " " + checkBox1.IsChecked + " " + "No: " + textBox5.Text);
receiptBuilder.AppendLine("|Departure On:" + " " + DatePicker.Text + " " + "At:" + " " + comboBox2.Text);
receiptBuilder.AppendLine("|Destination From:" + " " + lb + " " + "To:" + " " + lb1);
receiptBuilder.AppendLine("===================================[Receipt]===================================");
receiptBuilder.AppendLine();
return receiptBuilder.ToString();
}
And then save to the file:
File.AppendAllText(filePath, GetReceipt());
With this small refactoring you can now easily achieve first part of your request: Updating the existing file.
When you need to update the file with new data you simply invoke the GetReceipt() and append the results.
Or if you need the old file to be erased then you can use WriteAllText instead:
File.WriteAllText(filePath, GetReceipt());
Now the reading of the file. Depends what you need. If you just read the text and display it in a text box then it's fairly easy. Just use ReadAllText
myTextBox.Text = File.ReadAllText(filePath);
However if you want to display it in appropriate combo boxes and text boxes then you would have to manually read the file. But if that's the case then I'd encourage you to refactor your code a little bit:
Create a class that holds all the information of the receipt. Each time you want to save your data create a new instance of this class and then serialize it (or save to a database) so it's easy to retrieve specific fields. If you still want to have a nice way to display a receipt you can override the ToString method.

How to display fields in new line in same textBox?

I am using Silverlight4 and Telerik Reporting Q3 2011. I am trying to Generate Reports of all Outlets.
I used Table to display its fields. And I want to Display Full Address in same cell.
How could I?
I Used following Experession to display Full Address in same cell.
= Fields.AddressLine1
+ ", " + Fields.AddressLine2
+ ", " + Fields.Suburb
+ ", " + Fields.City
+ ", " + Fields.State
+ ", " + Fields.Country
I want do display this in same cell but want output like below..
=====================
Address
=====================
15A,
xxxx xxxRoad,
xxxxxx,
xxxxxxxx
====================
But I m getting this
=====================
Address
=====================
15A, xxxx xxxRoad, xxxxxx, xxxxxxxx
=====================
How do I get Output Which I want?
Use Environment.NewLine for getting into new line, see below
=Fields.AddressLine1 + ", "
+ Environment.NewLine + Fields.AddressLine2 + ", "
+ Environment.NewLine + Fields.Suburb + ", "
+ Environment.NewLine + Fields.City + ", "
+ Environment.NewLine + Fields.State + ", "
+ Environment.NewLine + Fields.Country;
EDIT
Refer to the link how to insert a newline in Textblock in silverlight
Have you tried using \n or \r\n in your strings wherever you want to do line breaks?
Yes Got it.....
I made the Function NewLineFeeds() in this function I used Replace() Method to replace the specific string to newLine(\n) .
public static string NewLineFeeds(string text)
{
string result = text.Replace(", ", "\n");
result = result.Replace(", ", "\n");
result = result.Replace(", ", "\n");
return result;
}
and my Expression is
=NewLineFeeds(Fields.AddressLine1
+ ", " + Fields.AddressLine2
+ ", " + Fields.Suburb
+ ", " + Fields.City
+ ", " + Fields.State
+ ", " + Fields.Country)
This call the Function NewLineFeeds() and replace ", " to \n (new Line).
Add this will work Fine..
Use complete name System.Environment.NewLine and it will work.
I just confirmed it in my report.

How to Sort a List in Desending Order by the "Value" and Sort the Duplicates by the "Key"?

I have looked All over the Internet to try and find an example of How to fix the following.
How to Sort a List in Desending Order by the "Value" and Sort the Duplicates by the "Key" ?
Then Print out the Results in the format below.
I have enclosed my code and it works, but the problem happens when there are duplicate values, which occurs when you use SortedList(). I would GREATLY APPRECIATE it if someone could PLEASE Modify this Code or Show me EXACTLY how to do this another way, that is just as quick and efficent.
THANK YOU VERY MUCH in Advance.
BEFORE SORT:
VALUE's, KEY's
sL.Add(1269.63,"white");
sL.Add(1270.36,"orange");
sL.Add(1272.06,"yellow");
sL.Add(1271.50,"cyan");
sL.Add(1272.06,"black");
sL.Add(1274.12,"dodBlue");
sL.Add(1276.02,"blue");
sL.Add(1273.21,"green");
sL.Add(1275.52,"red");
AFTER SORT:
VALUE's, KEY's
sL.Add(1276.02,"blue");
sL.Add(1275.52,"red");
sL.Add(1274.12,"dodBlue");
sL.Add(1273.21,"green");
sL.Add(1272.06,"black");
sL.Add(1272.06,"yellow");
sL.Add(1271.50,"cyan");
sL.Add(1270.36,"orange");
sL.Add(1269.63,"white");
CODE:
SortedList sL = new SortedList();
sL.Add(SMA(8)[0], "white");
sL.Add(SMA(10)[0], "orange");
sL.Add(SMA(13)[0], "yellow");
sL.Add(SMA(20)[0], "cyan");
sL.Add(SMA(30)[0], "black");
sL.Add(SMA(40)[0], "dodBlue");
sL.Add(SMA(50)[0], "blue");
sL.Add(SMA(100)[0], "green");
sL.Add(SMA(200)[0], "red");
Print(" " + " " + sL.GetByIndex(8) + " " + ">=" + " " + sL.GetByIndex(7));
Print("&&" + " " + sL.GetByIndex(7) + " " + ">=" + " " + sL.GetByIndex(6));
Print("&&" + " " + sL.GetByIndex(6) + " " + ">=" + " " + sL.GetByIndex(5));
Print("&&" + " " + sL.GetByIndex(5) + " " + ">=" + " " + sL.GetByIndex(4));
Print("&&" + " " + sL.GetByIndex(4) + " " + ">=" + " " + sL.GetByIndex(3));
Print("&&" + " " + sL.GetByIndex(3) + " " + ">=" + " " + sL.GetByIndex(2));
Print("&&" + " " + sL.GetByIndex(2) + " " + ">=" + " " + sL.GetByIndex(1));
Print("&&" + " " + sL.GetByIndex(1) + " " + ">=" + " " + sL.GetByIndex(0));
PRINT OUT RESULTS:
blue >= red ;
&& red >= dodBlue ;
&& dodBlue >= green ;
&& green >= yellow ;
&& yellow >= black ;
&& black >= cyan ;
&& cyan >= orange ;
&& orange >= white ;
Why don't you use LINQ for this? OrderBy() and Distinct() methods will help you.
You can do this with LINQ as shown below:
Dictionary<int, string> lst = new Dictionary<int,string>(10);
lst.Add(7, "MAX");
lst.Add(2, "A");
lst.Add(1, "A");
lst.Add(8, "0");
Dictionary<int, string> newList = (lst.OrderBy(x => x.Value).ThenBy(x => x.Key)).ToDictionary(x => x.Key,x=>x.Value);

Categories

Resources