I have a Gridview like this:
I need to change backcolor when Zero line greater than %10.
I tried like this (for just 15%):
if (e.Row.Cells[0].Text == "Zero")
{
for (int i = 0; i < e.Row.Cells.Count; i++)
{
if (e.Row.Cells[i].Text == "15%")
{
e.Row.Cells[i].BackColor = System.Drawing.Color.LightCoral;
}
}
}
I can't convert Row.Cells[i].Text to Int because of %. How can I use greater than 10 for backcolor?
just replace
if (e.Row.Cells[i].Text == "15%")
with
if (int.Parse(e.Row.Cells[i].Text.Trim('%'))>10)
Related
I read saved data from tbl in a list, and i want to edit the object, so when i start the program, combobox first to show saved value for that object, and others also to be in the combobox. Please help !
if (lstP.Count > 0)
{
for (int i = 0; i < lstP.Count; i++)
{
if (Stav.IDP == lstP[i].SP)
{
Prim.SelectedIndex = lstP[i].SP;
//ERROR
break;
}
}
}
SelectedIndex requires a number to be passed. What you need is to assign an i to it:
if (lstP.Count > 0)
{
for (int i = 0; i < lstP.Count; i++)
{
if (Stav.IDP == lstP[i].SP)
{
Prim.SelectedIndex = i;
break;
}
}
}
I am able to hide my text inside an Image. But when I tried to extract the text from my image, I always only able manage to get only the first character. I don't know where went wrong?
My embed operation code:
public static Bitmap embedMessage(string hiddenText, Bitmap oriImage)
{
Color currentPixel;
int[] colorRGB = new int[3];
List<int> messageValue = new List<int>();
messageValue = ConvertMessage(messageValue, hiddenText);
int messageIndex = messageValue.Count;
int binaryCount = 0;
int zeroAdded = 0;
for(int row = 0; row < oriImage.Height; row++)
{
for(int col = 0; col < oriImage.Width; col++)
{
currentPixel = oriImage.GetPixel(col, row);
colorRGB[0] = ConvertEven(currentPixel.R);
colorRGB[1] = ConvertEven(currentPixel.G);
colorRGB[2] = ConvertEven(currentPixel.B);
for(int rgbIndex = 0; rgbIndex < colorRGB.Length; rgbIndex++)
{
if(messageIndex > 0)
{
colorRGB[rgbIndex] += messageValue[messageValue.Count - messageIndex] % 2;
messageValue[messageValue.Count - messageIndex] /= 2;
}
}
if (messageIndex == 0 && zeroAdded < 8)
{
oriImage.SetPixel(col, row, Color.FromArgb(colorRGB[0], colorRGB[1], colorRGB[2]));
zeroAdded++;
Console.WriteLine("Adding zero number: " + zeroAdded);
}
else
{
Console.WriteLine("Set Pixel");
oriImage.SetPixel(col, row, Color.FromArgb(colorRGB[0], colorRGB[1], colorRGB[2]));
}
if (zeroAdded == 8)
{
Console.WriteLine("Final Pixel Add");
oriImage.SetPixel(col, row, Color.FromArgb(colorRGB[0], colorRGB[1], colorRGB[2]));
return oriImage;
}
binaryCount++;
if (binaryCount % 8 == 0) { messageIndex--; Console.WriteLine("Message Index deducted"); }
}
}
return oriImage;
}
My embed implementation is the same to this example As for extraction I used the exact extraction code from the example. No matter what I tried, I am still only getting the first character of my embedded text. I tried checking my code by printing each operations and all of them fires without any issue which means the embed operation should be working as expected.
Just found my problem. Everything in my embed method should be in the R G B for loop.
I have an array of four numbers which I need to compare to another array of numbers. I should tell how many of those numbers are right placed, and how many are right numbers but not right placed.
F.eks:
Code: 1501
Guess: 1305
Right numbers: 3
Right placement: 2
The right placement is easy. But the Right Number part is a bit tricky. This is how I solved it:
//Check if number exist in context
Rn = 0;
for (int g = 0; g < 4; g++)
{
resent[g] = 6;
}
for (int i = 0; i < 4; i++)
{
for (int j = 0; j < 4; j++)
{
if
(
guess[i] == Numbers[j]
&& guess[i] != resent[0]
&& guess[i] != resent[1]
&& guess[i] != resent[2]
&& guess[i] != resent[3]
)
{
Rn++;
resent[i] = guess[i];
}
}
}
ConsolePost("Right Numbers: " + Rn);
Is there a beter way to do this. Or is it possible to shorten this?
Like:
if(guess[i] == Numbers[j] && != resent[0-3])
{
Something;
}
You can use Any() to check if you decide to change the size of your array:
!resent.Any(c=>c==guess[i])
You can use Enumerable.Take(4) and Contains on your resent like:
!resent.Take(4).Contains(guess[i])
So your check could be:
if(guess[i] == Numbers[j] && !resent.Take(4).Contains(guess[i]))
See: Enumerable.Take<TSource> Method
Returns a specified number of contiguous elements from the start of a
sequence.
If resent size is always 4, then you can skip Take(4) and instead just use Contains like:
if(guess[i] == Numbers[j] && !resent.Contains(guess[i]))
I tried this but this is not working.
I'm getting index out of bound exception.
for (int x = 0; x < newText.Count; x++)
{
for (int y = 0; y < WordsList.words.Length; y++)
{
if (!newText[x].Contains(WordsList.words[y]))
{
for (int n = 0; n < 3; n++)
newText.RemoveAt(x);
}
}
}
newText is a List
words is string[]
newText format is like this:
index 0 = this is a text hello all
index 1 = time&date (6/14/2014....)
index 2 = empty ""
index 3 = text hello world
index 4 = time&date (6/14/2014....)
index 5 = empty ""
And so on...
What i want to do is to loop over newText and if in index 0 there no any word(string) from words then remove index 0,1,2 next itertion check index 3 for any words if not exist one word or more remove indexs 3,4,5.
If in index 0 or 3 there is one word or more then do nothing dont remove anything.
In the end newText should be in the same format as before:
index 0 text line
index 1 date&time
index 2 empty ""
Just the new newText content will be with text lines that contain one ore more strings from words.
EDIT
This is what i tried now:
First this is how i build the List:
List<string> t = filterNumbers(text);
for (int i = 0; i < t.Count; i++)
{
if (!newText.Contains(t[i]))
{
newText.Add(t[i]);
newText.Add(dateTime[i]);
newText.Add("");
}
}
Removing numbers and leave only text and add it.
In the end in this case i have in newText 150 indexs. That's 50 indexs of text lines.
Then i tried this:
int lastindex = newText.Count - 1;
for (int i = newText.Count - 1; i >= 0; i--)
{
for (int x = 0; x < WordsList.words.Length; x++)
{
if (!newText[i].Contains(WordsList.words[x]))
{
if (i != lastindex)
{
newText.RemoveAt(i + 1);
}
newText.RemoveAt(i);
}
}
}
But i'm getting exception on the line:
if (!newText[i].Contains(WordsList.words[x]))
Index was out of range. Must be non-negative and less than the size of the collection
EDIT
If I understood correctly that you wanted to check whether a specific line contains some words and if not remove that and the two following lines, here is a possible solution:
// start at the bottom in the first line "that matters" and go down by 3
for (int x = newText.Count - 3; x >= 0; x-=3)
{
// check if the line contains any of the words specified
if (!WordsList.words.Any(w => newText[x].Contains(w)) || newText[x] == "")
{
// remove the checked line as well as the next two if not
l.RemoveRange(x, 3);
}
}
EDIT
Corrected the predicate to:
!WordsList.words.Any(w => newText[x].Contains(w));
from
!WordsList.words.Any(w => newText.Contains(w));
EDIT 2
Added the empty string as possibility
The problem was that if the line to test was empty, it would pass the test because it does not contain any word from WordsList.words. The test now includes the empty string as an option and removes it when encountered.
Looking at your logic:
(1) for (int i = newText.Count - 1; i >= 0; i--)
{
(2) for (int x = 0; x < WordsList.words.Length; x++)
{
if (...)
{
(3) newText.RemoveAt(i);
}
}
}
You can see that even if you removed lines in (3), you continue loop in (2) which can try again remove line in (2) for new indexes, which now become out of bounds
you need to add break after (3) to continue loop (1)
// For each 3-word-sets
for (int x = 0; x < newText.Count; x += 3)
{
// For each word in that 3-word-set
for (int k = x; k < 3; k++)
{
// Check each word
bool breakOut = false;
for (int y = 0; y < WordsList.words.Length; y++)
{
if (!newText[k].Contains(WordsList.words[y]))
{
newText.RemoveAt(x+2);
newText.RemoveAt(x+1);
newText.RemoveAt(x);
x -= 3;
breakOut = true;
break;
}
}
if (breakOut) { break; }
}
}
I just wanted to test and experiment with your original code. Haven't tested this. Just make sure the list contains 3×n items.
Ok, it seems your data structure is really bad. Anyway if you have to keep the structure as is I think this can work :
var newList = new List<string>();
for (int index = 0; index < newText.Count; index = index + 3)
{
if (WordsList.Any(t => newText[index].ToLower().Trim().Contains(t.ToLower().Trim())))
{
newList.AddRange(newText.Skip(index).Take(3));
}
}
newText = newList;
I have datagridview and now I would like to change background color of its each row depending whether row number is even or odd.
I thought that there must be easier way to reach that. Then using for example this part of code and modify it so it would change the colours of dtg's row. If this snippet of code is one of the ways to do that, may someone help me to improve it so it wouldn't throw exception when index is out if rabge?
public void bg_dtg()
{
try
{
for (int i = 0; i <= dataGridView1.Rows.Count ; i++)
{
if (IsOdd(i))
{
dataGridView1.Rows[i].DefaultCellStyle.BackColor = Color.LightBlue;
}
}
}
catch (Exception ex)
{
MessageBox.Show(""+ex);
}
}
public static bool IsOdd(int value)
{
return value % 2 != 0;
}
Thank you for your time and answers.
There is a DataGridView alternate row view style option in the forms designer. AlternatingRowsDefaultCellStyle in the properties grid
You are getting exception because you are accessing row that is not present. GridView rows are zero based index, it means if you have ten rows in grid the index will be from 0 to 9 and you should iterate one less then the rows count. The i <= dataGridView1.Rows.Count will give exception on last iteration because when count is 10 (total rows are ten) and dataGridView1.Rows[10] does not exists therefore exception is thrown.
Change <= in loop condition to <
for (int i = 0; i <= dataGridView1.Rows.Count ; i++)
To
for (int i = 0; i < dataGridView1.Rows.Count ; i++)
You Should AlternatingRowsDefaultCellStyle property to set alternative row style to keep it simple and efficient.
you can try this code
for (int i = 0; i < GridView1.Rows.Count; i++) {
if (i % 2 == 0) {
GridView1.Rows[i].Cells[0].Style.BackColor = System.Drawing.Color.Green;
GridView1.Rows[i].Cells[1].Style.BackColor = System.Drawing.Color.Green;
}
else {
GridView1.Rows[i].Cells[0].Style.BackColor = System.Drawing.Color.Red;
GridView1.Rows[i].Cells[1].Style.BackColor = System.Drawing.Color.Red;
}
}
You can use AlternatingRowsDefaultCellStyle
OR
you can also do it manually
foreach (DataGridViewRow row in dataGridView1.Rows)
if (row.Index % 2==0 )
{
row.DefaultCellStyle.BackColor = Color.Red;
}
AlternatingRowStyle-BackColor = "#C5C5C5"
We can directly add the code in the ASP grid
<asp:GridView ID="Gridview1" runat="server"
AlternatingRowStyle-BackColor = "#F3F3F3"
AutoGenerateColumns="true">
</asp:GridView>