Grouping together values to display in Label using C# - c#

I have a situation and need some help to solve this.
enter image description here
I am not allowed to embed images directly to the post. As you can see the image, Data is displayed in the gridview, but client wants it to displayed as text one below another like text below the gridivew. However, there is a requirement to display Filenames together which have common date. E.g : 04/18/2015, there are two different file entries and want PAYROLL_******20150429.csv to be displayed under PAYROLL*****_20150422.csv. So no need for third entry, just want 2nd and 3rd to be clubbed together. There could be different files with same date entry, and they need to be clubbed together. The first Name, 191_20150418 is based on the date entry.
I am using Win forms with C#. I have gathered the data in the data table to display in the gridview.
This is my code below to display in the Label:
Label.Text += Environment.NewLine + "Name : " + dtSWAction.Rows[i]["Name"] + Environment.NewLine +
"PPED : " + dtSWAction.Rows[i]["Date"] + Environment.NewLine +
"Files : " + dtSWAction.Rows[i]["Files"] + Environment.NewLine;
Please suggest a better way if adding like this in label is not feasible.

There really isn't anything wrong with doing this:
Label.Text +=
Environment.NewLine + "Name : " + dtSWAction.Rows[i]["Name"] +
Environment.NewLine + "PPED : " + dtSWAction.Rows[i]["Date"] +
Environment.NewLine + "Files : " + dtSWAction.Rows[i]["Files"] +
Environment.NewLine;
However you could use string.Format():
Label.Text += string.Format("{0}Name : {1}{0}PPED : {2}{0}Files : {3}{0}",
Environment.NewLine,
dtSWAction.Rows[i]["Name"],
dtSWAction.Rows[i]["Date"],
dtSWAction.Rows[i]["Files"]);
Or string interpolation if you are using C# 6/.NET 4.6:
Label.Text += $"{Environment.NewLine}Name : {dtSWAction.Rows[i]["Name"]}{Environment.NewLine}PPED : {dtSWAction.Rows[i]["Date"]}{Environment.NewLine}Files : {dtSWAction.Rows[i]["Files"]}{Environment.NewLine}";
It looks like you are using a DataTable for accessing this data, but in the future if you are using a custom defined class you could override the ToString() method of your class using any one of the above methods:
public override string ToString()
{
return string.Format("{0}Name : {1}{0}PPED : {2}{0}Files : {3}{0}",
Environment.NewLine,
dtSWAction.Rows[i]["Name"],
dtSWAction.Rows[i]["Date"],
dtSWAction.Rows[i]["Files"]);
}
Then anytime in the future you could just call it like this:
Label.Text += instanceOfYourClass.ToString();

Related

Trying to update a text file

I'm trying to replace a certain line in a .txt file when I click the Update Button
This is what my program looks like
http://i.imgur.com/HKu4bGo.png
This is my code so far
string[] arrLine = File.ReadAllLines("Z:/Daniel/SortedAccounts.txt");
arrLine[accountComboBox.SelectedIndex] = "#1#" + firstNameInfoBox.Text + "#2#" + lastNameInfoBox.Text + "#3#" + emailInfoBox.Text + "#4#" + phoneNumberInfoBox.Text + "#5#EMAIL#6#";
File.WriteAllLines("Z:/Daniel/SortedAccounts.txt", arrLine);
This is what's inside SortedAccounts.txt
#1#Bob#2#Smith#3#Bob#Smith.com#4#5551234567#5#EMAIL#6#
#1#Dan#2#Lastyy#3#Daniel#Lastyy.com#4#5551234567#5#EMAIL#6#
The ComboBox is in the order as the Txt File.
So I get the same Index as the selected item in the ComboBox. And then I want to delete that line and then add a new line that same txt file with the updated information.
My code isn't doing this for some reason though and I can't figure it out
Try this out using List to easily remove an entry at a certain index. Don't forget to reload the combobox data source when the file is updated to avoid index mismatch etc..
List<string> arrLine = File.ReadAllLines("Z:/Daniel/SortedAccounts.txt").ToList();
arrLine.RemoveAt(accountComboBox.SelectedIndex);
string newLine = "#1#" + firstNameInfoBox.Text + "#2#" + lastNameInfoBox.Text + "#3#" + emailInfoBox.Text + "#4#" + phoneNumberInfoBox.Text + "#5#EMAIL#6#";
arrLine.Add(newLine);
File.WriteAllLines("Z:/Daniel/SortedAccounts.txt", arrLine);

Print a list after being converted from string to a Label

I'm creating a standard calculator with a history feature. Previous solutions will show in a label box every time the user clicks the "=" button
After converting this string:
string histo = (operand1 + " " + operation + " " + " " + operand2 + " = " + result);
To a list by using this code:
List<string> hist = histo.Split().ToList()
What I want to do next is to print it to a label box to show history. How can I do that? Thank you.
Your question is not very clear and I do not think that label is a good choice to show the history. Combobox would be a better option, if you want to allow users to select items from history.
As far as your question on how to display it in a label is concerned you can use the following code. You can replace "," with Environment.NewLine.
lbl.Text = String.Join(",", hist);

Web Form Cutting Off After Special Character

I have a simple web form where a person can enter into a textbox what kind of project they want. For example, they may type in: Sales & Projection report needs to be fixed.
They then click a submit button and it gets sent off to a third party website that keeps track of our projects.
The problem is, in the example given above, everything gets cut off after the '&' symbol.
it gets sent like this:
String request = "fct=createorcopyproject&guid=" + guid + "&projectname=" + TxtProjectName.Text + "&projectdesc=" + TxtDescription.Text +
"&nexttasknumber=1&budgethours=0&budgetcost=0&estimatedstartdate=" + year + "-" + month + "-" + day + "&estimatedenddate=" + year + "-" + month + "-" + day + "&estimatedhours=0&estimatedexpenses=0&projectpriorityid=" + priorityIndex + "&projectstatusid=NULL&projecttemplate=0&contactname=" + user +
"&defaultestimatedtime=0&defaulttaskstartdate=1&defaulttaskenddate=1&defaulttaskactualdates=2&clientid=" + areaIndex + "&createdefaults=True&languagedefaults=EN&projecttemplateid=0000003&keeptemplatelink=false&copyprojectassignments=True&copyprojectdocuments=True&copyforumtopics=False&copytasks=False&adjusttaskdates=False&copytaskdocuments=False&copytaskassignments=False&markproject=False&format=ds";
Where TxtDescription.Text is where we are getting the cutoff.
Is this something on their end or am I missing something?
Use HttpUtility.UrlEncode method to encode the values you send in an URL (assuming this is an URL)

Getting data from Multiple Combo Box

I'm trying to create a program wherein the users will provide their birthdate from multiple combo boxes. One for month, then date, and third is for year. And combine their values into one string. So far, here's my code:
string bdate = " "+ bday_month.SelectedText + " " + bday_date.SelectedText + ", " + bday_year.SelectedText;
MessageBox.Show(bdate);
The problem with this is that when I have values for all combo boxes, bdate only displays the third combo box value. See below:
Any idea?
Use the ComboBox.Text property instead of the ComboBox.SelectedText:
string bdate = " " + bday_month.Text + " " + bday_date.Text + ", " + bday_year.Text;
MessageBox.Show(bdate);
The ComboBox.SelectedText property returns the text highlighted in the editor, but not the entire text.
comboBox.SelectedText
is a value indicating the currently selected text in the control and
comboBox.Text
is the current text in the ComboBox
so use .Text instead

Pasting Data into excel file correctly from textbox

foreach (string TempSize in sizeArr)
{
txtResult.Text += store + "\t" + attributeSet + "\t" + configurableAttri + "\t" + type + "\t" + (ID + "_" + TempColour + TempSize) + "\n";
}
I am trying to generate Multiple Rows that i can use to paste into excel file. "\n" doesn't seem to show on the "new line" of the textbox.
I tried manually doing "\n" aka shift+enter and copy paste the 3 rows into excel, Excel place a empty space between each of them, anyway to remove that problem too?
Thanks in advance!
Problem Fixed with "\r\n" instead of "\n" hat's how Windows controls represent newlines

Categories