By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,491 Members | 3,230 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,491 IT Pros & Developers. It's quick & easy.

DataBind a Two or Multi Dimensional Array to Gridview

P: 12
Can anyone tell me why I am not allowed to bind a two or multi dimensional array to GridView and possibly suggest how they would deal with similar scenarios?

The array is simply a two dimensional monthly volume array:

Extract from Report class:

// Query all Messages Sent for a Given Month
public string[,] qVolumeByMonth()
{
string[,] volumeByMonth = new string[2,13];
SqlCommand comm = Data.getCommand("spVolumeByMonth");
DataTable table = Data.ExecuteQueryCommand(comm);
// Initialise the array month values
for (int months = 0; months < 12; months++)
{
volumeByMonth[0,months] = Convert.ToString(months + 1);
volumeByMonth[1,months] = "0";
}
volumeByMonth[0,12] = "Total";
volumeByMonth[1,12] = "0";
// Enter the database information into the array
int totalMessages = 0;
for (int rows = 0; rows < table.Rows.Count; rows++)
{
volumeByMonth[1,(Convert.ToInt32(table.Rows[rows]["month"].ToString()) - 1)] = table.Rows[rows]["messages"].ToString();
totalMessages += Convert.ToInt32(table.Rows[rows]["messages"].ToString());
}
volumeByMonth[1,12] = totalMessages.ToString();
return volumeByMonth;
}

This all works to provide a simple two dimensional array. Then when I try to bind to GridView with the following code:

Report report = new Report();
string[,] volumeByMonth = report.qVolumeByMonth();
gvVolumeByMonth.DataSource = volumeByMonth;
gvVolumeByMonth.DataBind();

I get the following eror: "Array was not a one-dimensional array."

Can the GridView simply not be bound to multidimensional arrays?

Kind Regards,

Ian
Jun 8 '07 #1
Share this Question
Share on Google+
3 Replies


P: 99
Can anyone tell me why I am not allowed to bind a two or multi dimensional array to GridView and possibly suggest how they would deal with similar scenarios?

The array is simply a two dimensional monthly volume array:

Extract from Report class:

// Query all Messages Sent for a Given Month
public string[,] qVolumeByMonth()
{
string[,] volumeByMonth = new string[2,13];
SqlCommand comm = Data.getCommand("spVolumeByMonth");
DataTable table = Data.ExecuteQueryCommand(comm);
// Initialise the array month values
for (int months = 0; months < 12; months++)
{
volumeByMonth[0,months] = Convert.ToString(months + 1);
volumeByMonth[1,months] = "0";
}
volumeByMonth[0,12] = "Total";
volumeByMonth[1,12] = "0";
// Enter the database information into the array
int totalMessages = 0;
for (int rows = 0; rows < table.Rows.Count; rows++)
{
volumeByMonth[1,(Convert.ToInt32(table.Rows[rows]["month"].ToString()) - 1)] = table.Rows[rows]["messages"].ToString();
totalMessages += Convert.ToInt32(table.Rows[rows]["messages"].ToString());
}
volumeByMonth[1,12] = totalMessages.ToString();
return volumeByMonth;
}

This all works to provide a simple two dimensional array. Then when I try to bind to GridView with the following code:

Report report = new Report();
string[,] volumeByMonth = report.qVolumeByMonth();
gvVolumeByMonth.DataSource = volumeByMonth;
gvVolumeByMonth.DataBind();

I get the following eror: "Array was not a one-dimensional array."

Can the GridView simply not be bound to multidimensional arrays?

Kind Regards,

Ian
Hope the following helps:
http://forums.microsoft.com/MSDN/Sho...05432&SiteID=1
http://www.devx.com/dotnet/Article/33748
Jun 8 '07 #2

P: 12
Excellent, I'll give these a try.

Much appreciated!

Ian
Jun 8 '07 #3

P: 12
That second article was the business and helped me considerably on this and now future projects. Thank You!!

Ian
Jun 11 '07 #4

Post your reply

Sign in to post your reply or Sign up for a free account.