for (int i = 0; i <= dataGridView1.Rows.Count; i++)
{
string point_value += dataGridView1.Rows[i].Cells[1].Value + "|";
}
Does anyone know why I am getting this error
Invalid expression term '+='
for this code?
you create new variable with in your for loop that the reason
if you need to store data each loop you should create variable outside loop
like this
string point_value = "";
for(int i=0; i < dataGridView1.Rows.Count; i++)
{
point_value += dataGridView1.Rows[i].Cells[1].Value + "|";
}
for more information about loop pattern here
(edited to avoid empty value and out of range from DiplomacyNotWar's commented)
Related
Is there a way to get command line arguments in C# from args[1] and forward, excluding args[0]?
I've tried with this:
short argslenght = (short) args.Length;
string[] pargs = { "" };
for(int i = 1; i <= args.Length; i++)
{
pargs[i-1] = args[i];
}
But gives me this error:
System.IndexOutOfRangeException
Thanks for your time.
You are getting that error because you are trying to read a value from the array of index which exceeds the limit of the array length. It is happening for the last value. If there are 5 arguments, you can read the last one by args[4] but in your loop, you are trying to read it by args[5] which is causing the error.
You need to use Length -1 in your For loop, like this way:
for(int i = 1; i <= args.Length - 1; i++)
Or remove the = from the condition:
for(int i = 1; i < args.Length; i++)
for (int i = 0; i < CheckBoxList3.Items.Count + 1; i++)
{
if (CheckBoxList3.Items[i].Selected || CheckBoxList4.Items[i].Selected || CheckBoxList5.Items[i].Selected)
{
str1 += CheckBoxList3.Items[i].Text.ToString() + "," + CheckBoxList4.Items[i].Text.ToString() +"," + CheckBoxList5.Items[i].Text.ToString() + ",";
I receive the error call "Index was out of range. Must be non-negative and less than the size of the collection.
Parameter name: index". How to solve this issue?
What I want to do was collect the data from checkedbox in multiple check-box-list and combine together and store it into database.
Any suggestion or help would be great. Thanks
UPDATE : This is one of my check box list. The error point to my second line of the for loop.
<asp:CheckBoxList ID="CheckBoxList3" runat="server" RepeatDirection="Horizontal" Width="900px">
<asp:ListItem>Chicken</asp:ListItem>
<asp:ListItem>Tomato</asp:ListItem>
<asp:ListItem>Garlic</asp:ListItem>
</asp:CheckBoxList>
Wrong line 1: for (int i = 0; i < CheckBoxList3.Items.Count + 1; i++
Wrong line 2: CheckBoxList3.Items[i].Selected || CheckBoxList4.Items[i].Selected || CheckBoxList5.Items[i].Selected
Wrong line 3: str1 += CheckBoxList3.Items[i].Text.ToString() + "," + CheckBoxList4.Items[i].Text.ToString() +"," + CheckBoxList5.Items[i].Text.ToString() + ",";
You need to change your for loop condition from CheckBoxList3.Items.Count + 1 to CheckBoxList3.Items.Count
Also your condition checking only CheckBoxList3.Items.Count but you are using CheckBoxList4.Items.Count and CheckBoxList5.Items.Count
It seems CheckBoxList4 and CheckBoxList5 don't have enough item like CheckBoxList3.
Please Check your all individual CheckboxList objects to use same indexer.
Hope this helps.
//EDIT
Best practice:
string result = CombineCheckboxLists(checkBoxList1, checkBoxList2, checkboxlist3);
private string CombineCheckboxLists(params CheckBoxList[] list)
{
StringBuilder builder = new StringBuilder();
string result = string.Empty;
if (list?.Length > 0)
{
int minItemsCount = list.Min(l => l.Items.Count);
if (minItemsCount > 0)
{
for (int i = 0; i < minItemsCount; i++)
{
builder.Append(string.Join(",", list
.Where(l => l.Items[i].Selected)
.Select(l=> l.Items[i].Text)));
//if you want to merge all iteration via ",", please use following lines instead of above -- [marked as **]
// ** //builder.Append($"{string.Join(",", list.Select(l => l.Items[i].Value))},");
}
}
}
result = builder.ToString();
// ** // result = result.TrimEnd(',');
return result;
}
Just change your for loop like below:
for (int i = 0; i < CheckBoxList3.Items.Count; i++)
if (CheckBoxList3.Items[i].Selected)
str1 += CheckBoxList3.Items[i].Text.ToString() + ",";
for (int i = 0; i < CheckBoxList4.Items.Count; i++)
if (CheckBoxList4.Items[i].Selected)
str1 += CheckBoxList4.Items[i].Text.ToString() + ",";
for (int i = 0; i < CheckBoxList5.Items.Count; i++)
if (CheckBoxList5.Items[i].Selected)
str1 += CheckBoxList5.Items[i].Text.ToString() + ",";
UPDATE
#Caner LENGER has a good idea. But as I mentioned in the comment of his answer it's partially true, because list.Min returns minimum value of a list that in your case it's not gonna work. Why?
Imagine you have 3 CheckBoxLists that each one has 3 items in it except one of them that has 4. Using list.Min you'll get 3, No matter what's inside the 4th value of CheckBoxList.
So, I changed his code and here is the tested result:
public static string CombineCheckboxLists(params CheckBoxList[] list)
{
StringBuilder sb = new StringBuilder();
foreach (CheckBoxList checkBoxList in list)
{
int counter = checkBoxList.Items.Count;
for (int i = 0; i < counter; i++)
sb.Append(checkBoxList.Items[i].Selected ? checkBoxList.Items[i].Text + "," : "");
}
return sb.ToString();
}
I am creating a program that holds customers details and sends them an email alerting them of special offers. I am having trouble when I export the email data as I want to store it in the sendto.text field separating each address with a comma, How can I get the data from each row of the datagrid and can it be stored and separated with a comma in a textfield?.
Hope this makes sense. Thanks
for (int i = 0; i < dataGridView1.Rows.Count; i++)
{
sendto.text = dataGridView1.Rows[i].Cells[3].Value.ToString());
}
Like this?:
for (int i = 0; i < dataGridView1.Rows.Count; i++)
{
sendto.text += dataGridView1.Rows[i].Cells[3].Value.ToString() + (i < (dataGridView1.Rows.Count-1) ? "," : "");
}
you can add comma from second emailid onwards.
for (int i = 0; i < dataGridView1.Rows.Count; i++)
{
if(i>0)
sendto.text +=","+ dataGridView1.Rows[i].Cells[3].Value.ToString());
else
sendto.text = dataGridView1.Rows[i].Cells[3].Value.ToString());
}
StringBuilder stremail = new StringBuilder();
for (int i = 0; i < dataGridView1.Rows.Count; i++)
{
stremail.Append(dataGridView1.Rows[i].Cells[3].Value.ToString() + ",");
}
int stringWithoutLastComma = --stremail.Length;
sendto.text = stremail.ToString(0,stringWithoutLastComma);
You can:
for (int i = 0; i < dataGridView1.Rows.Count; i++)
{
sendto.text += string.Format("{0},", dataGridView1.Rows[i].Cells[3].Value.ToString());
}
How to get richtextbox lines without displaying the content on it, I have a code like this under the button2
for (int i = 0; i < split.Length; i++)
richTextBox2.Text* += split[i] + "\n";
MessageBox.Show("Tamam!");`
but it takes too much time to display the content on Richtext, If I did not use this code above, there have another code under the button3 which is related with the code above
foreach (string line in *richTextBox2.Lines*)
how should I do to solve this problem?
Not knowing what you exactly want to achieve but if updating of the control takes a lot of time you might try before to start the loop in buton2 to stop rendering the control. Don't forget to enable it again.
Like so:
richtextBox2.SuspendLayout();
for (int i = 0; i < split.Length; i++)
{
richTextBox2.Text* += split[i] + "\n";
}
MessageBox.Show("Tamam!");
richtexbox2.ResumeLayout();
http://msdn.microsoft.com/en-us/library/system.windows.forms.control.suspendlayout.aspx
Or buildup your string with a stringbuilder first and the assign:
var builder = new StringBuilder();
for (int i = 0; i < split.Length; i++)
{
builder.Append(split[i]);
builder.Append('\n');
}
richTextBox2.Text = builder.ToString();
http://msdn.microsoft.com/en-us/library/y9sxk6fy.aspx
int i = 1;
for (; i <= 10; i++)
{
string str = "test{0}" , i;
Console.WriteLine(str);
}
So this code doesn't work, and I want to know the reason, and what are correct ways to produce this?
I think you meant to wrap that with a String.Format call.
string str = String.Format("test{0}", i);
You should try this syntax:
for (int i = 1; i <= 10; i++) {
string str = String.Format("test{0}", i);
Console.WriteLine(str);
}
The way you have defined your string doesn't look correct to me at all. I'm guessing the code you're looking for is:
int i = 1;
for(; i <= 10; i++)
{
string str = string.Format("test{0}", i);
Console.WriteLine(str);
}
But in that case there's really no reason to create a new string and call Format() for every iteration. You can create a single string and let Console.WriteLine() handle the formating.
string str = "test{0}";
for(int i = 1; i <= 10; i++)
Console.WriteLine(str, i);
My guess is you want something like this:
for(int i=1;i<=10;i++)
Console.WriteLine(String.Format("test{0}",i);
You can put any number of things in brackets, separate each input with a comma.
string Month = "Jan";
int day = 21;
string temp = String.Format("Today is:{0} - {1}/{2}",Month,day,2011);
temp gets the value "Today is:Jan - 21/2011"
In the future the desired output would be helpful.
Edit: spelling
int i;
for (; i <= 10; i++) Console.WriteLine("test{0}", i);