I am trying to pass the id's of textboxes through an array index in c# so that I can get the values from the textboxes but for some reason I am unable to do it. I have almost 500 textboxes and its a data entry form.
This is what I have tried so far:
for(int i=2;i<542;i=i+7)
{
string a = TextBox[i].Text.ToString();
}
Please help me out.
You need to use the function Page.FindControl(), like that:
for (int i = 0; i < 542; i++)
{
string val = Page.FindControl('TextBox' + i.ToString()).Text; // Or use String.Format()
}
Related
Please help I have tried to move data from one datagridview column into an array in order to add (calculate) something from it and place it in another column called age. This is what I tried and wanted to see if the data is stored in an array but got a "use of unassigned local variable" error
string data;
int j = 3;
for (int i = 0; i < 10; i++)
{
data = Convert.ToString(dgvData[j, i].Value);
}
MessageBox.Show(data);
The variable data may not have a value assigned to it in the for loop, which would cause the MessageBox to fail. This can be resolved by instantiating data with a default value.
string data = "";
It looks like to me that you are also trying to display each value in the fourth column in the datagridview with MessageBox, so consider moving the MessageBox into the for loop. This could also allow you to move your data variable closer to the call, removing the need for the default value.
int j = 3;
for (int i = 0; i < 10; i++)
{
string data = Convert.ToString(dataGridView1[j, i].Value);
MessageBox.Show(data);
}
So im just learning C# and trying to use arrays, im getting input from user via a forms app and wish to copy it to an array called prevPos, in the format below
receiving data (string):
string1: "hello"
string2: "123"
//counting how many lines and using that to determine position associated with each
recieved
char[] prevPos;
prevPos = textBox_ReceievedData.Text.ToCharArray();
//count how many lines of receieved data in textbox
for (int i = 0; i < textBox_ReceievedData.Lines.Length; i++)
{
System.Console.WriteLine("charArray " +prevPos[i]);
}
right now if i wish to call it, i would get this, i do not want this fomat:
prevPos[1]=h
prevPos[2]=e
prevPos[3]=l
etc.
I want this output:
prevPos[1]=hello
prevPos[2]=123
please replace with the below and try
for (int i = 0; i < textBox_ReceievedData.Lines.Length; i++)
{
System.Console.WriteLine("Each Line " +textBox_ReceievedData.Lines[i]);
}
prevPos = textBox_ReceievedData.Text.Split('\n');
This will give you an array of all text separated by the new line character \n.
If that is the output you desire, then a string array (not a char array) is what you want. The Lines() property already gives you that, though:
string[] prevPos = textBox_ReceievedData.Lines;
for(int i=0; i< prevPos.Length; i++)
{
System.Console.WriteLine(prevPos[i]);
}
I have a listbox which contain some string ... and I would like to pass those string to an empty dropdownlist? Is it possible? Just the value in the listbox will be chosen.
I dont know what I'm doing .. And for this I have to use a for loop which I'm still confused about.
This is my code for now:
string [] lines = new string [cartListBox.Items.Count];
int select;
for (int i = 0; i < lines.Length ; i++)
{
select = cartListBox.SelectedValue
watchedMoviesDropDownList.Items.Add(cartListBox.Items.Count.ToString());
}
However, when I debug it, it gives me an error saying that index is out of range... :( Please help....!
It's much simpler to do it, you don't need:
string [] lines
nor:
int select
Just use this code:
for (int x = 0; x < cartListBox.Items.Count; x++)
watchedMoviesDropDownList.Items.Add(cartListBox.Items[x]);
I have created a new integer called int VactualSum = 0;
I need VactualSum to equal the sum of all the values in an object called singleSummary[i].actual. then display the results in a text box called actualsumsent.
singleSummary[i].actual has 4 numeric values that I want my result to be the total of them when added up. e.g 10,20,30,40 the actualsumsent text box should show the value 100.
{ int VactualSum = 0;
I thought maybe having -
Vactual = Vactual + function[i].actual;
Then to put it in the text box have -
actualsumsent.Text = System.Convert.ToString(returned.Vactual)
But this does not work, the section in the array I am trying to add up is -
function[i].account = el.Descendants("var").Where(x => (string)x.Attribute("name") == "account").SingleOrDefault().Attribute("value").Value;
Any advice would be appreciated.
Assuming singleSummary is an array (or IList<T>) then you can do:
actualsumsent.Text = singleSummary.Sum(s => s.actual).ToString();
EDIT: Looking at the edit to the question, it seems you want to sum a comma-separated string containing int values. In that case you can calculate the sum like this:
int sum = singleSummary[i].account.Split(",").Select(s => int.Parse(s)).Sum();
Note this will throw an exception if the string is not well-formed.
Am I missing something? Do you simply want to sum your values and place them into your text box? If you're using 3.5 or newer, you can use the following:
actualsumset.Text = singleSummary.Sum(q=>q.actual).ToString();
Otherwise, you could sum your array up the classic way:
int VactualSum = 0;
foreach(YourObject obj in singleSummary)
{
VactualSum+=obj.actual;
}
actualsumset.Text = VactualSum;
Is a Vactual a bit like a vacuole? Or is it like an actualValue?
Sum of Comma separate values as a input
public int getSum(String inputNumbersCSV)
{
String[] inputNumbers = inputNumbersCSV.Split(',');
int sumOfNum = 0;
for (int i = 0; i < inputNumbers.Length; i++)
{
sumOfNum = sumOfNum + int.Parse(inputNumbers[i]);
}
return sumOfNum;
}
I have a ListView which is bound to a DataTable. I would like to iterate over the DataTable's rows and access their data. I figured, to do this, I would simply iterate over the ListViewDataItems in the ListView. To test that I am properly accessing the data, I tried the following code, which should simply print the string at column 0 for each row.
for (int i = 0; i < MyListView.Items.Count; i++)
{
ListViewDataItem item = MyListView.Items[i];
DataRow row = (DataRow) item.DataItem;
Response.Write(row[0]);
}
However, nothing is printed. To verify that the ListView is not empty (which it shouldn't be as the data is properly rendered on my aspx page), I tried this:
Response.Write(MyListView.Items.Count);
This prints the number 16, which is correct as there are 16 rows in my ListView. I'm guessing I'm just not accessing the data correctly. I'd appreciate some insight on this.
The best way is to stop on breakpoint (at line DataRow row = (DataRow) item.DataItem;) and simply to check what you have .
for example like here :http://msdn.microsoft.com/en-us/library/ms173083(v=VS.90).aspx
I decided the best solution was to just iterate over the data directly in the DataTable rather than the ListViewDataItems.
for (int i = 0; i < myTable.Rows.Count; i++)
{
for (int j = 0; j < myTable.Columns.Count; j++)
{
object data = data.Rows[i][j];
// do stuff with data
}
}
For anyone still seeking the correct answer to this question, the following code will work (VB.NET):
Dim di as ListViewDataItem
For Each di in MyListView.Items
Response.Write(CType(di.FindControl("ExampleLabel"), Label).Text)
Next
Just substitute the Response.Write line with whatever you wanted to do to each list item. The example line is looking for a control called 'ExampleLabel', casts it back to a label then writes the text value onto the page.
Easily adapted to C# for anyone proficient (not I alas).