i need help to resolve this problem in asp.net c# [closed] - c#

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 4 years ago.
Improve this question
There are two datasets ds and ds1. want to change backcolor of datalist if values of two datasets are matched. We are comparing two datasets but value not compared.
for (Int32 i = 0; i < ds.Tables[0].Rows.Count-1; i++)
{
for (Int32 x = 0; x < ds1.Tables[0].Rows.Count-1; x++)
{
if (ds.Tables[0].Rows[i][0].ToString() == ds1.Tables[0].Rows[x][0].ToString())
{
DataList1.ItemStyle.BackColor = System.Drawing.Color.Red;
}
else
{
DataList1.ItemStyle.BackColor = System.Drawing.Color.Green;
}
}
}

You should break loops after finding match, otherwise loops continue and changes color each time it matches values or not until both loops end. Here is answer how to break out of nested loops: Breaking out of a nested loop

As mentioned #Greg, you really need to break a loop after matching was found.
Futhermore, if you need to scan all rows in datasets, change your loop definitions:
for (Int32 i = 0; i < ds.Tables[0].Rows.Count; i++)
{
for (Int32 x = 0; x < ds1.Tables[0].Rows.Count; x++)
In your code you will skip the last rows of the both datasets, because loop will end when its counter will be equal Count - 2.

Related

Sort float numbers List from small to large [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
Improve this question
I want to sort float numbers List from small to large. I tried to use the array.Sort () method but it doesn't work.
Below is my code.
List<float> array = new List<float>();
array.Add(x);
listBox1.Items.Add(x);
Just Sort():
List<float> array = new List<float>();
array.Add(x);
array.Sort();
When array (quite a strange name for List<float>) is sorted, we can insert x somewhere in the middle of listBox1.Items. Asuming that listBox1.Items contains ordered float items only, we can put
// Here's we have WinForms ListBox
int at = listBox1.Items.Count;
for (int i = 0; i < listBox1.Items.Count; ++i) {
if (x < (float) (listBox1.Items[i])) {
at = i;
break;
}
}
listBox1.Items.Insert(at, x);

In c#, how can i generate a set of strings like aa0000 > aa0001 >... > zz9999 [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 1 year ago.
Improve this question
Hi everyone I need to generate all strings from aa0000 to zz9999; where the first two position are only chars from a to z and the last four positions are from 0000 to 9999.
I tried everything I could but I can't find a way to do this.
Thanks in advance!
You can try nested loops, e.g.
Code:
public static IEnumerable<string> Generator() {
for (char a = 'a'; a <= 'z'; ++a)
for (char b = 'a'; b <= 'z'; ++b)
for (int i = 0; i <= 9999; ++i)
yield return $"{a}{b}{i:d4}";
}
...
foreach (string s in Generator()) {
//TODO: Put relevant code here
}
Demo:
Console.WriteLine(string.Join(Environment.NewLine, Generator()
.Skip(1_000_000 - 5) // skip some items
.Take(10))); // then take some items
Console.WriteLine();
Console.Write(Generator().Count()); // how many items do we have?
Outcome:
dv9995
dv9996
dv9997
dv9998
dv9999
dw0000
dw0001
dw0002
dw0003
dw0004
6760000

I cant increment value by 2 or more than 2 [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 5 years ago.
Improve this question
How do i increment a value by 2 instead of 1? when i run my code it will go from 1-10 by adding 1 but i want it to go 1-10 by adding 2
for (int x=0;x<10;x++)
{
Console.WriteLine(x);
}
You can do instead -
for (int x = 0; x < 10;) {
Console.WriteLine(x);
x = x + 2;
}
If I understand you correctly, this should be the solution for you.
for (int x = 0; x < 10; x += 2)
{
Console.WriteLine(x);
}

Making an array ints and replace the order of number I write [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
What I try to do, is for example if I write five numbers 1,2,3,4,5 after this it should print 5,4,3,2,1
int[] numbers = new int[5];
for (int i = 0; i < numbers.Length ; i++)
{
Console.Write("");
numbers[i] = int.Parse(Console.ReadLine());
}
for (int i = 0; i < numbers.Length; i++)
{
}
If you need just to print them in a reverse order, but you want to keep them stored as you inserted them, change the second loop like this:
for (int i=numbers.Length-1; i >= 0; i--){
Console.WriteLine(numbers[i]);
}
You can print the array with a reverse loop as in the previous answer, or you can do that in a single line:
Console.WriteLine(string.Join(",", numbers.Reverse()));

compilation error : cannot convert implicitly type int to bool [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
ERROR occurs at for loop
List<string> computers = Global.getAllComputers(Environment.UserDomainName);
computers.Sort();
if (dataGridView1.Rows.Count == 1)
{
foreach (var s in computers)
dataGridView1.Rows.Add(s);
}
else
{
foreach (string s in computers)
{
for (int i = 0; (dataGridView1.Rows.Count - 1); i++)
{
if (s.ToUpper() == dataGridView1.Rows[i].Cells[0].Value.ToString().ToUpper())
continue;
else
dataGridView1.Rows.Add(s);
}
}
}
This line doesn't contain a boolean expression as required by the for syntax, change it to
for (int i = 0; i < dataGridView1.Rows.Count; i++)
By not finding a boolean condition inside the evaluation part of the for loop the compiler complains that you are trying to use an integer (Rows.Count) instead of the boolean expected.
The For loop C# reference
Estabilish the initial value of variable i.
Evaluate the condition (i < dataGridView1.Rows.Count), the
value of i is compared to the number of rows in the datagrid. If i
is less than to Rows.Count, the condition evaluates to true, and the
body of the for loop is executed, otherwise exit the loop
Increment the value of i by 1.
Return to the step 2 to evaluate the condition again.
Use
for (int i = 0; dataGridView1.Rows.Count > i; i++)
instaed of
for (int i = 0; (dataGridView1.Rows.Count - 1); i++)

Categories

Resources