how to display values stored in multi dimenssional array? - c#

I have created a multidimensional array.I wants to display it in the .aspx page. please help.my code is here.It doesnt shows the array values
string[,] array_questions = new string[dt.Rows.Count, dt.Columns.Count];
for (i = 0; i < dt.Rows.Count; i++)
{
for (j = 0; j < dt.Columns.Count; j++)
{
array_questions[i, j] = dt.Rows[i][j].ToString();
//TextBox1.Text = array_questions[i,j];
}
}
Console.Write(array_questions[i, j] + " ");

you can also use Response.write

Console.Write is used for console applications.
If you want to display the data on the aspx webpage you need to create a webcontrol (for example label) and set its text property with an appropriate value.

If you want to write the values ​​in the table in the console visual studio, call the method
Console.Out.Write() and not Console.Write().
Otherwise call Response.Write() to write on the page

// display the array
for (int i = 0; i < array_questions.GetLength(0); i++)
{
for (int j = 0; j < array_questions.GetLength(1); j++)
{
Response.Write("i="+i+" "+"j="+j);
Response.Write("<br>");
}
}

Related

I am trying to create chart in c# from non-constant values in an array

I am loading csv file values in array and want to create a chart.
for(int i = 0; i < array.GetLength(1); i++)
{
int x = sum[i];
chart1.Series.Add(array[0, i]);
chart1.Series[array[0, i]].Points.AddXY(i, x.ToString());
}
only last bar is showing as output correctly.
here is csv file snapshot.
enter image description here
when i tried with hardcoded values like
chart1.Series["S1"].Points.AddXY(1.ToString(), 99);
chart1.Series["S1"].Points.AddXY(2.ToString(), 88);
chart1.Series["S1"].Points.AddXY(3.ToString(), 33);
it was showing correct output as shown in snapshot.
hardcoded values
Try something like the syntax used here for a bar chart. There is also an update to your sum function, since I don't know other ways to sum the columns of a 2D array.
for(int i = 0; i < array.GetLength(1); i++)
{
int x = 0;
for (int j = 1; j < array.GetLength(1); j++) {
x += array[j, i]
}
chart1.Series.Add(array[0, i]);
chart1.Points.AddXY(array[0, i], x);
}

C# Setting image to multiple controls is slow

I am trying to clone the game "Minesweeper" in c#.
When I create a field, I want the background of the button control to be the same as in the original game "Minesweeper".
However, when I create my field and I add all the images it takes at least 1-5 seconds to completely load the field.
Code for creating the field:
for (int i = 0; i < fieldsize; i++)
{
for (int j = 0; j < fieldsize; j++)
{
panel1.controls.add(new Button(){backgroundimage = properties.resources.field})
}
}
Any help is appreciated!

How do I bind a non database column value to repeater label using asp.net

I have a little challenge that I can't get around. I have a result from the code below.
if (Request.Cookies["ProductRecord"] != null)
{
s = Convert.ToString(Request.Cookies["ProductRecord"].Value);
string[] strArr = s.Split('|');
for (int i = 0; i < strArr.Length; i++)
{
t = Convert.ToString(strArr[i].ToString());
string[] strArr1 = t.Split(',');
for (int j = 0; j < strArr1.Length; j++)
{
a[j] = strArr1[j].ToString();
}
SingleCount = (Convert.ToDecimal(a[1].ToString()) * Convert.ToDecimal(a[3].ToString()));
}
}
How do I bind the results of SingleCount to a label inside a repeater.
RepeaterId.DataSource = SingleCount is the closest i have thought of but its not working.
I'm making some assumptions based upon your code so I may be off-base on what you're trying to achieve, but couldn't you take SingleCount and add it to a list of decimals and then bind that list to the DataSource of your repeater?

Flling a two-dimensional array with buttons

I have a litte problem here. I have a two-dimensional array which is [5,5] big.
I have a windows form with 25 buttons. Now I want to store the buttons in the object array, but my problem is, how do I tell the program to know which button to put in the array? Is it possible in some kind of this way:
//_array[i] = button(i);
This is my first time storing objects in an array and I dont know how to do that.
EDIT: The buttons have all the standard names (button1,button,button3...)
EDIT2: I know how to it by hand (_array[x,y] = button1) but I want to know how to it ith a for-loop.
You could filter the controls by using IEnumerable.OfType():
//get all buttons and order them by name
var buttons = Controls.OfType<Button>().OrderBy(x => x.Name).ToList();
for(int i = 0; i < 5; i++)
for(int j = 0; j < 5; j++)
_array[i, j] = buttons[i*5+j];
You can access the buttons using their name as index of the Controls collection:
for (int i = 0; i < 5; i++) {
for (int k = 0; k < 5; k++) {
_array[i, k] = Controls["Button" + (5 * i + k + 1).ToString()];
}
}
Try this:
for (int i=0; i<5; i++)
for (int j=0; j<5; j++)
_array[i,j] = Controls.Item["Button" + (i*5 +j).ToString()];

Printing entire array in C#

I have a simple 2D array:
int[,] m = { {0,0,0,0,0,0,0,0,0,0}, {0,0,0,0,0,0,0,0,0,0} };
How can I print this out onto a text file or something? I want to print the entire array onto a file, not just the contents. For example, I don't want a bunch of zeroes all in a row: I want to see the
{{0,0,0,0,0,0,0,0,0,0}, {0,0,0,0,0,0,0,0,0,0} };
in it.
Just iterate over it and produce the output. Something like
static string ArrayToString<T>(T[,] array)
{
StringBuilder builder = new StringBuilder("{");
for (int i = 0; i < array.GetLength(0); i++)
{
if (i != 0) builder.Append(",");
builder.Append("{");
for (int j = 0; j < array.GetLength(1); j++)
{
if (j != 0) builder.Append(",");
builder.Append(array[i, j]);
}
builder.Append("}");
}
builder.Append("}");
return builder.ToString();
}
There is no standard way to get those { brackets, you have to put them through the code while iterating through your array and writing them to the file

Categories