Checking if the Rows of the 2D array is true - c#

I have a 2D array of boolean[30,10]. those are all set to false. I only want to check if the Rows are filled. so example. if the rows of the 0th column are all true. Then I debug returning say: "First row are all true". Same goes for all other columns. If the 5th column and his rows are also all true. Then I return say: "Fifth row of booleans are all true"
private bool[,] grid = new bool[30,10];
for( int y = 0; x < grid.GetLength(0); y++ )
{
for( int x = 0; x < grid.GetLength(1); x++ )
{
if ( grid[y,x] == true )
{
Debug.Log(y + "th rows are filled!");
}
}
}
There are just my pseudo codes. they don't work i know. but does someone know how to?

first of, a very small issue, in your first for loop you, you wrote
for (int y = 0; **x** < grid.GetLength(0); y++)
instead you should write:
for (int y = 0; **y** < grid.GetLength(0); y++) ;
but unfortunately it's not the problem :(
In your code you are going through every element in a row and checking if it is true, and here is the problem, this if statement:
if(grid[y,x] == true){
Debug.Log(y + "th rows are filled!");
is checking if the first element is true, if it is, it will print out that the whole
row is filled with true variables. Instead I offer this solution:
bool[,] grid = new bool[2,2];
bool isRowTrue = true;
for (int y = 0; y < grid.GetLength(0); y++)
{
for (int x = 0; x < grid.GetLength(1); x++)
{
if (grid[y, x] == false)
{
isRowTrue = false;
break;
}
}
if (isRowTrue == true)
{
Debug.Log(y + "th are all true!");
}
else {
isRowTrue = true;
}
}
I hope I've understood your problem correctly, if not, my apologies, tell me and I will try to understand and help.

To loop through check if an array of values is all true do this:
private bool[,] grid = new bool[30,10];
for(int y = 0; x < grid.GetLength(0); y++){
bool row = true;
for(int x = 0; x < grid.GetLength(1); x++)
row &&= grid[y,x]; // row stays true until it encounters a false
if (row) Debug.Log(y + " row is filled");
}

Try this:
int rows = 30;
int cols = 10;
bool[,] grid = new bool[rows,cols];
InitializeGrid(grid);
for (int row = 0 ; row < rows ; ++row )
{
bool isAllTrue = true;
for( int col = 0 ; col < cols ; ++col {
if ( !grid[row,col] )
{
isAllTrue = false;
break;
}
}
if (isAllTrue)
{
Console.WriteLine($"Row {row} is all true");
}
}

You can do it in a single foreach loop like
private bool[,] grid = new bool[30,10];
bool allAreTrue = true;
foreach(var b in grid)
{
if(!b)
{
allAreTrue = false;
// No need to check others anymore
break;
}
}
Or you could use Linq All after converting the multi dimensional array to an enumerable using Linq OfType
using System.Linq;
...
private bool[,] grid = new bool[30,10];
var allAreTrue = grid.OfType<bool>().All(b => !b);

Related

Getting datagridview cell index in C#

How to get Cell index after comparing values, for example i have this
for (int i = 0; i < dataGridView1.Rows.Count; i++)
{
if (dataGridView1.Rows[i].Cells[value].Value.ToString() == radTextBox1.Text)
{
//If the value matches how to get the row index of that
}
}
This might do the job:
for (int i = 0; i < dataGridView1.Rows.Count; i++)
{
if (dataGridView1.Rows[i].Cells[0].Value.ToString() == radTextBox1.Text)
{
dataGridView1.CurrentCell = dataGridView1.Rows[i].Cells[0];
}
}
If you are wanting to find the value in any cell then you need to use a nested loop.
dataGridView1.ClearSelection();
for (int i = 0; i < dataGridView1.Rows.Count; i++)
{
for (int c = 0; c < dataGridView1.Columns.Count; c++)
{
if (dataGridView1.Rows[i].Cells[c].Value.ToString() == radtextBox1.Text)
{
dataGridView1.Rows[i].Selected = true;
}
}
}
This will move through each row while checking all the columns for a value and will hilight any row where a cell has that value. Note that this code is case sensitive so "City" <> "city" but that is easily fixed using .ToUpper or .ToLower as needed.
One other thing to add, this code also is based off a DGV that has AllowUserToAddRows set to false. If you need the edit row, you either need to -1 from the count in the rows loops or check to ensure that the current row is false for .IsNewRow.
You found the Row you're looking for.
It's i variable in your code.
var requiredRowIndex = -1;
for (int i = 0; i < dataGridView1.Rows.Count; i++)
{
if (dataGridView1.Rows[i].Cells[value].Value.ToString() == radTextBox1.Text)
{
requiredRowIndex = i;
break;
}
}
if (requiredRowIndex != -1)
{
// It was found.
}
else
{
// It was not found.
}
You dont show us what is the value? It's actualy index of Cell you're looking for.

Remove Nulls from string[,]

I have a string array defined in c# as
string[,] options = new string[100,3];
Throughout the code it gets populated with data but not always filled.
So if I have 80 parts of it filled and 20 parts of it not filled. The 20 parts have nulls in them or 60 nulls at the end. Is there an easy way to resize the array so that after filling it the array is the same as
String[,] options = new string[80,3];
It would have to be resized based on the position of the first set of 3 nulls it found.
If this was a jagged array I would have done
options = options.Where(x => x != null).ToArray();
The method is quite long, because it has to check every row twice...
public static string[,] RemoveEmptyRows(string[,] strs)
{
int length1 = strs.GetLength(0);
int length2 = strs.GetLength(1);
// First we count the non-emtpy rows
int nonEmpty = 0;
for (int i = 0; i < length1; i++)
{
for (int j = 0; j < length2; j++)
{
if (strs[i, j] != null)
{
nonEmpty++;
break;
}
}
}
// Then we create an array of the right size
string[,] strs2 = new string[nonEmpty, length2];
for (int i1 = 0, i2 = 0; i2 < nonEmpty; i1++)
{
for (int j = 0; j < length2; j++)
{
if (strs[i1, j] != null)
{
// If the i1 row is not empty, we copy it
for (int k = 0; k < length2; k++)
{
strs2[i2, k] = strs[i1, k];
}
i2++;
break;
}
}
}
return strs2;
}
Use it like:
string[,] options = new string[100, 3];
options[1, 0] = "Foo";
options[3, 1] = "Bar";
options[90, 2] = "fiz";
options = RemoveEmptyRows(options);
As suggested by Alexei, there is another way of doing this:
public static string[,] RemoveEmptyRows2(string[,] strs)
{
int length1 = strs.GetLength(0);
int length2 = strs.GetLength(1);
// First we put somewhere a list of the indexes of the non-emtpy rows
var nonEmpty = new List<int>();
for (int i = 0; i < length1; i++)
{
for (int j = 0; j < length2; j++)
{
if (strs[i, j] != null)
{
nonEmpty.Add(i);
break;
}
}
}
// Then we create an array of the right size
string[,] strs2 = new string[nonEmpty.Count, length2];
// And we copy the rows from strs to strs2, using the nonEmpty
// list of indexes
for (int i1 = 0; i1 < nonEmpty.Count; i1++)
{
int i2 = nonEmpty[i1];
for (int j = 0; j < length2; j++)
{
strs2[i1, j] = strs[i2, j];
}
}
return strs2;
}
This one, in the tradeoff memory vs time, chooses time. It is probably faster, because it doesn't have to check every row twice, but it uses more memory, because it puts somewhere a list of the non-empty indexes.
I went for all rows until you find an row with all null values:
Needs some clean up and will obviously remove non-null rows that occur after the first all null row. The requirement wasn't too clear here
EDIT: Just seen the comment clarifying requirement to remove all null rows - I've tweaked the below to avoid downvotes but a more comprehensive answer is already accepted (and is more efficient) :)
void Main()
{
string[,] options = new string[100,3];
options[0,0] = "bleb";
options[1,1] = "bleb";
options[2,0] = "bleb";
options[2,1] = "bleb";
options[3,2] = "bleb";
options[4,1] = "bleb";
string[,] trimmed = TrimNullRows(options);
Console.WriteLine(trimmed);
}
public string[,] TrimNullRows(string[,] options)
{
IList<string[]> nonNullRows = new List<string[]>();
for (int x = 0; x < options.GetLength(0); x++)
{
bool allNull = true;
var row = new string[options.GetLength(1)];
for (int y = 0; y < options.GetLength(1); y++)
{
row[y] = options[x,y];
allNull &= options[x,y] == null;
}
if (!allNull)
{
nonNullRows.Add(row);
}
}
var optionsTrimmed = new string[nonNullRows.Count, options.GetLength(1)];
for (int i=0;i<nonNullRows.Count;i++)
{
for (int j=0;j<options.GetLength(1);j++)
{
optionsTrimmed[i, j] = nonNullRows[i][j];
}
}
return optionsTrimmed;
}
You can also get yourself some helpers to convert between jagged and multi-dimensional representations. This is pretty silly, of course, but for arrays as small as the ones you're showing (and also, very sparse arrays), it'll be fine.
void Main()
{
string[,] options = new string[100,3];
options[3, 1] = "Hi";
options[5, 0] = "Dan";
var results =
options
.JagIt()
.Where(i => i.Any(j => j != null))
.UnjagIt();
results.Dump();
}
static class Extensions
{
public static IEnumerable<IEnumerable<T>> JagIt<T>(this T[,] array)
{
for (var i = 0; i < array.GetLength(0); i++)
yield return GetRow(array, i);
}
public static IEnumerable<T> GetRow<T>(this T[,] array, int rowIndex)
{
for (var j = 0; j < array.GetLength(1); j++)
yield return array[rowIndex, j];
}
public static T[,] UnjagIt<T>(this IEnumerable<IEnumerable<T>> jagged)
{
var rows = jagged.Count();
if (rows == 0) return new T[0, 0];
var columns = jagged.Max(i => i.Count());
var array = new T[rows, columns];
var row = 0;
var column = 0;
foreach (var r in jagged)
{
column = 0;
foreach (var c in r)
{
array[row, column++] = c;
}
row++;
}
return array;
}
}
The JagIt method is pretty simple of course - we'll just iterate over the rows, and yield the individual items. This gives us an enumerable of enumerables, which we can use in LINQ quite easily. If desired, you could transform those into arrays, of course (say, Select(i => i.ToArray()).ToArray()).
The UnjagIt method is a bit more talkative, because we need to create the target array with the correct dimensions first. And there's no unyield instruction to simplify that :D
This is pretty inefficient, of course, but that isn't necessarily a problem. You could save yourself some of the iterations by keeping the inner enumerable an array, for example - that will save us having to iterate over all the inner items.
I'm mostly keeping this as the memory-cheap, CPU-intensive alternative to #xanatos' memory-intensive, CPU-cheap (relatively).
Of course, the main bonus is that it can be used to treat any multi-dimensional arrays as jagged arrays, and convert them back again. General solutions usually aren't the most efficient :D
Yet another variant with linq
static string[,] RemoveNotNullRow(string[,] o)
{
var rowLen = o.GetLength(1);
var notNullRowIndex = (from oo in o.Cast<string>().Select((x, idx) => new { idx, x })
group oo.x by oo.idx / rowLen into g
where g.Any(f => f != null)
select g.Key).ToArray();
var res = new string[notNullRowIndex.Length, rowLen];
for (int i = 0; i < notNullRowIndex.Length; i++)
{
Array.Copy(o, notNullRowIndex[i] * rowLen, res, i * rowLen, rowLen);
}
return res;
}

How to get all values from a column in a DataGridView

You see, I'm trying to add the quantity if the item name already exists in the DataGridView. So what I did was made a for loop to check each row in the DataGridView. What it did was just add more rows.
for (int x = 0; x < dataGridView.Rows.Count; x++)
{
if (dataGridView.Rows[x].Cells[1].Value.ToString() == dataTable.Rows[0][2].ToString())
{
dataGridView.Rows[x].Cells[0].Value = int.Parse(dataGridView.Rows[x].Cells[0].Value.ToString()) + 1;
dataGridView.Rows[x].Cells[2].Value = Convert.ToDecimal(dataTable.Rows[0][4].ToString()) * Convert.ToDecimal(dataGridView.Rows[x].Cells[0].Value.ToString());
}
else
{
quantity = 1;
dataGridView.Rows.Add(quantity, dataTable.Rows[0][2], dataTable.Rows[0][4]);
}
}
I think it's better to using .ToLower() or .ToUpper() in string comparation.
I solved it! I just made a checker using a for loop!
for (int x = 0; x < dataGridView.Rows.Count; x++)
{
if (dataGridView.Rows[x].Cells[1].Value.ToString() == dataTable.Rows[0][2].ToString())
{
same = true;
counter = x;
}
}
if (same == true)
{
dataGridView.Rows[counter].Cells[0].Value = int.Parse(dataGridView.Rows[counter].Cells[0].Value.ToString()) + 1;
dataGridView.Rows[counter].Cells[2].Value = Convert.ToDecimal(dataTable.Rows[0][4].ToString()) * Convert.ToDecimal(dataGridView.Rows[counter].Cells[0].Value.ToString());
}
else
{
quantity = 1;
dataGridView.Rows.Add(quantity, dataTable.Rows[0][2], dataTable.Rows[0][4]);
}

Seeded random plotting 2d grid

This has been bugging me for a few hours so I was wondering if anyone could help me out as I might be thinking about this the wrong way.
I want to be able to get a boolean value from a set of x and y coordinates on an grid with an infinite width and height. There are also other constraints, along the x axis there needs to be at least n places between two true values, I would also need to know the number of true values in the area from 0,0 to x,y.
The width and height of the area given to getTrueCoordinatesInArea is equal to x and y as its the area is created from 0,0 to x,y
If that makes sense..
So for example:
value = coordinateContainsTrue( x, y );//return true or false.
total = getTrueCoordinatesInArea( x , y );//Returns the total true values inside the area.
Edit: This would work off a seed.
I don't understand exacly what you need but I thought it sounded like a good and fun exercise. I hoped it's what you wanted. =) It's not coded exactly how I wanted. Would rather have used a bool[,] array but don't know how to make that one dynamic and didn't want to code my own class for this, but maybe I should have.
However, this solution should work.
private List<List<bool>> grid = new List<List<bool>>();
private int N = 4;
Random randomNumberGenerator = new Random();
private bool getRandomBoolean()
{
return this.randomNumberGenerator.Next(0, 2) == 1;
}
private void testProgram()
{
var containsTrue = coordinateContainsTrue(0, 10);
var total = getTrueCoordinatesInArea(14, 2);
var isTrue = coordinateIsTrue(15, 11);
}
private bool coordinateIsTrue(int x, int y)
{
expandGridOfNeeded(x, y);
return grid[x][y];
}
private bool coordinateContainsTrue(int x, int y)
{
expandGridOfNeeded(x, y);
for (int xTemp = 0; xTemp < x; xTemp++) // Loop columns
{
for (int yTemp = 0; yTemp < y; yTemp++) // Loop rows
{
if (grid[xTemp][yTemp])
return true; // Return true if any true was found
}
}
return false;
}
private int getTrueCoordinatesInArea(int x, int y)
{
expandGridOfNeeded(x, y);
var total = 0;
for (int xTemp = 0; xTemp < x; xTemp++) // Loop columns
{
for (int yTemp = 0; yTemp < y; yTemp++) // Loop rows
{
if (grid[xTemp][yTemp])
total++; // Increase count if true was found
}
}
return total;
}
private void expandGridOfNeeded(int x, int y)
{
if (x < 0 || y < 0)
return;
// Add needed columns
while (grid.Count <= x)
{
var column = new List<bool>();
// Only add rows if the others have rows
if (grid.Count > 0)
{
while (column.Count < grid[0].Count)
{
// Check if legal to add a true value, if it is then add random else add false
bool forceFalseValue = false;
for (int i = grid.Count - 1; i > grid.Count + N && forceFalseValue == false; i--)
{
forceFalseValue = grid[i][column.Count - 1];
}
column.Add(forceFalseValue ? false : getRandomBoolean());
}
}
grid.Add(column);
}
// Add needed rows
while (grid[0].Count <= y)
{
var counter = N;
foreach (var column in grid)
{
// Check if legal to add a true value, if it is then add random else add false
if (counter < N)
{
column.Add(false);
counter++;
}
else
{
var randomValue = getRandomBoolean();
if (randomValue)
counter = 0;
else
counter++;
column.Add(randomValue);
}
}
}
}

Need help in arranging Columns in a Multiband Infragistics UltrawinGrid

I have a grid which has 4 bands and the columns in band[3] are not arranged in order as in its parent bands. My primary requirement is to arrange the Band[3] columns.
This is a part of "Export to Excel" code. The grids are already displayed in the form.
I have tried the below approach -
private UltraGrid rearrangeCol(UltraGrid grid)
{
int bandCount = grid.DisplayLayout.Bands.Count;
int band = 0;
List<String> colPos = new List<string>();
for (int i = grid.DisplayLayout.Bands[band].Columns["EndurEndInv"].Index; i < grid.DisplayLayout.Bands[band].Columns.Count; i++)
{
colPos.Add(grid.DisplayLayout.Bands[band].Columns[i].Key);
}
band++;
while (band < bandCount)
{
int initialColPos = grid.DisplayLayout.Bands[band].Columns["EndurEndInv"].Index;
for (int i = initialColPos, j = 0; i < grid.DisplayLayout.Bands[band].Columns.Count && j < colPos.Count; i++, j++)
{
if(!grid.DisplayLayout.Bands[band].Columns[i].Key.Equals(colPos[j], StringComparison.InvariantCulture))
{
grid.DisplayLayout.Bands[band].Override.AllowColSwapping = Infragistics.Win.UltraWinGrid.AllowColSwapping.WithinBand;
int xcngPos = grid.DisplayLayout.Bands[band].Columns[colPos[j]].Index;
grid.DisplayLayout.Bands[band].Columns.Insert(i, "Temp");
grid.DisplayLayout.Bands[band].Columns[xcngPos + 1].Swap(grid.DisplayLayout.Bands[band].Columns[i]);
grid.DisplayLayout.Bands[band].Columns.Remove("Temp");
grid.DisplayLayout.Bands[band].Override.AllowColSwapping = Infragistics.Win.UltraWinGrid.AllowColSwapping.Default;
}
else
continue;
}
band++;
};
return grid;
}
Actually, nothing happens when I use SWAP, the keys, index remains same.
Is there any better approach?
Yes, to rearrange to position of colums you need to work on
grid.DisplayLayout.Bands[index].Columns[colName].Header.VisiblePosition = newPos;
no need to swap the column positions, just rearrange the Header position.
This code below has not been tested and I write as an example
private UltraGrid rearrangeCol(UltraGrid grid)
{
int bandCount = grid.DisplayLayout.Bands.Count;
int band = 1;
UltraGridBand b0 = grid.DisplayLayout.Bands[0];
while (band < bandCount)
{
UltraGridBand b = grid.DisplayLayout.Bands[band]
for (int i = b0.Columns["EndurEndInv"].Index; i < b0.Columns.Count; i++)
{
string colKey = b0.Columns[i].Key;
if(b.Columns.Exists(colKey))
b.Columns[colKey].Header.VisiblePosition =
b0.Columns[colKey].Header.VisiblePosition;
}
band++;
}
return grid;
}

Categories

Resources