We help IT Professionals succeed at work.

C# : How to display an array in a datagrid

NerishaB
NerishaB asked
on
2,522 Views
Last Modified: 2012-05-11
Hi,

I have an ASP application, and I am testing it's functionality.  I need to display the result of my array to make sure that I am getting correct results.  Can anyone show me how to get my array displayed in a datagrid or even a text file.  Please can someone help?  My array below:

 string[,] HrsWorkedTable = new string[qry.TotalRows, 24];          


Comment
Watch Question

CERTIFIED EXPERT

Commented:
http://support.microsoft.com/kb/315786
hi Nehrisha

pls look this sample code

by
kumareasn
private void ledgerStyleButton_Click(object sender, System.EventArgs e)
{
    // Create a new cell style.
    DataGridViewCellStyle style = new DataGridViewCellStyle();
    {
        style.BackColor = Color.Beige;
        style.ForeColor = Color.Brown;
        style.Font = new Font("Verdana", 8);
    }

    // Apply the style as the default cell style.
    dataGridView1.AlternatingRowsDefaultCellStyle = style;
    ledgerStyleButton.Enabled = false;
}

private void SetUpDataGridView()
{
    this.Controls.Add(dataGridView1);
    dataGridView1.ColumnCount = 5;
    DataGridViewCellStyle style = 
        dataGridView1.ColumnHeadersDefaultCellStyle;
    style.BackColor = Color.Navy;
    style.ForeColor = Color.White;
    style.Font = new Font(dataGridView1.Font, FontStyle.Bold);

    dataGridView1.EditMode = DataGridViewEditMode.EditOnEnter;
    dataGridView1.Name = "dataGridView1";
    dataGridView1.Location = new Point(8, 8);
    dataGridView1.Size = new Size(500, 300);
    dataGridView1.AutoSizeRowsMode = 
        DataGridViewAutoSizeRowsMode.DisplayedCellsExceptHeaders;
    dataGridView1.ColumnHeadersBorderStyle = 
        DataGridViewHeaderBorderStyle.Raised;
    dataGridView1.CellBorderStyle = DataGridViewCellBorderStyle.Single;
    dataGridView1.GridColor = SystemColors.ActiveBorder;
    dataGridView1.RowHeadersVisible = false;

    dataGridView1.Columns[0].Name = "Release Date";
    dataGridView1.Columns[1].Name = "Track";
    dataGridView1.Columns[1].DefaultCellStyle.Alignment = 
        DataGridViewContentAlignment.MiddleCenter;
    dataGridView1.Columns[2].Name = "Title";
    dataGridView1.Columns[3].Name = "Artist";
    dataGridView1.Columns[4].Name = "Album";

    // Make the font italic for row four.
    dataGridView1.Columns[4].DefaultCellStyle.Font = new Font(DataGridView.DefaultFont, FontStyle.Italic);

    dataGridView1.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
    dataGridView1.MultiSelect = false;

    dataGridView1.BackgroundColor = Color.Honeydew;

    dataGridView1.Dock = DockStyle.Fill;

    dataGridView1.CellFormatting += new DataGridViewCellFormattingEventHandler(dataGridView1_CellFormatting);
    dataGridView1.CellParsing += new DataGridViewCellParsingEventHandler(dataGridView1_CellParsing);
    addNewRowButton.Click += new EventHandler(addNewRowButton_Click);
    deleteRowButton.Click += new EventHandler(deleteRowButton_Click);
    ledgerStyleButton.Click += new EventHandler(ledgerStyleButton_Click);
    dataGridView1.CellValidating += new DataGridViewCellValidatingEventHandler(dataGridView1_CellValidating);

}

private void PopulateDataGridView()
{

    // Create the string array for each row of data.
    string[] row0 = { "11/22/1968", "29", "Revolution 9", "Beatles", "The Beatles [White Album]" };
    string[] row1 = { "4/4/1960", "6", "Fools Rush In", "Frank Sinatra", "Nice 'N' Easy" };
    string[] row2 = { "11/11/1971", "1", "One of These Days", "Pink Floyd", "Meddle" };
    string[] row3 = { "4/4/1988", "7", "Where Is My Mind?", "Pixies", "Surfer Rosa" };
    string[] row4 = { "5/1981", "9", "Can't Find My Mind", "Cramps", "Psychedelic Jungle" };
    string[] row5 = { "6/10/2003", "13", "Scatterbrain. (As Dead As Leaves.)", "Radiohead", "Hail to the Thief" };
    string[] row6 = { "6/30/1992", "3", "Dress", "P J Harvey", "Dry" };

    // Add a row for each string array.
    {
        DataGridViewRowCollection rows = this.dataGridView1.Rows;
        rows.Add(row0);
        rows.Add(row1);
        rows.Add(row2);
        rows.Add(row3);
        rows.Add(row4);
        rows.Add(row5);
        rows.Add(row6);
    }

    // Change the order the columns are displayed.
    {
        DataGridViewColumnCollection columns = this.dataGridView1.Columns;
        columns[0].DisplayIndex = 3;
        columns[1].DisplayIndex = 4;
        columns[2].DisplayIndex = 0;
        columns[3].DisplayIndex = 1;
        columns[4].DisplayIndex = 2;
    }
}

Open in new window

hi

try this source code - to assign two dimensional array in datagrid

by
kumaresan
// 1. Create two dimensional array

//

const int  dim = 1000;

double[,]  array = new double[dim,dim];

Random ran = new Random();
for(int r = 0; r < dim; r++)
{
    for(int c = 0; c < dim; c++)
    {
        array[r,c] = (ran.Next(dim)); // fill it with random numbers.

    }
}

// 2. Create ArrayDataView class in which 

// constructor you pass the array 

// and assign it to DataSource property of DataGrid. 


 dataGrid1.DataSource = new ArrayDataView(array);

Open in new window

This one is on us!
(Get your first solution completely free - no credit card required)
UNLOCK SOLUTION

Author

Commented:
Thanks kumaresan2011,

Here is my problem,  I am doing all my calls to the database, and calculations in a Class Library.  The array is built in the class library as well.  What I need now, is a way to call the classlibrary in an .aspx page, and populate the gridview on that page with the array from the classlibrary.

Can you help with that?

Author

Commented:
Thanks for the help.
Unlock the solution to this question.
Join our community and discover your potential

Experts Exchange is the only place where you can interact directly with leading experts in the technology field. Become a member today and access the collective knowledge of thousands of technology experts.

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.