Solved

# How to assign the values from 2 dimentional array to Two columns Data Table

Posted on 2012-08-21
424 Views
Hi,
Please let me know how to assign the following built in array to two columns Data Table.
I want to assign the Data Table to Drop Down List.

{
DateTime dt = DateTime.Now;
int icuryr = dt.Year;
String curyr =  dt.Year.ToString();
//string [][] yr = new string[8][];
int[][] yr = new int[8][];
int curyear = icuryr + 0;
int fuyear = icuryr + 1;
int p1year = icuryr - 1;
int p2year = icuryr - 2;
//yr[0] = curyear.ToString() + '-' + fuyear.ToString();
yr[0][0] = (icuryr + 0)+ '-' + (icuryr + 1);
yr[0][1] = 1;
yr[1][0] = (icuryr - 2);
yr[1][1] = 2;
yr[2][0] = (icuryr - 3);
yr[2][1] = 3;
yr[3][0] = (icuryr - 4);
yr[3][1] = 4;
yr[4][0] = (icuryr - 5);
yr[4][1] = 5;
yr[5][0] = (icuryr - 6);
yr[5][1] = 6;
yr[6][0] = (icuryr - 7);
yr[6][1] = 7;
yr[6][0] = (icuryr + 1) + '-' + (icuryr - 4);
yr[6][1] = 8;
DataTable dtYear = new DataTable();
// Here we create a DataTable with five columns.
DataColumn Year = new DataColumn("SelectedYear", typeof(string));
DataColumn Value = new DataColumn("Value", typeof(int));

}

Thanks.
0
Question by:KavyaVS

LVL 16

Accepted Solution

Hi,

since the array declaration and the value assignments are not well formed.

i have updated the code to the process you wanted. Now the array values are loaded into a datatable and that datatable is binded to the dropdownlist.

Check the below sample code

``````public void Load2DArrayToDT()
{
DateTime dt = DateTime.Now;
int icuryr = dt.Year;
String curyr = dt.Year.ToString();

int curyear = icuryr + 0;
int fuyear = icuryr + 1;
int p1year = icuryr - 1;
int p2year = icuryr - 2;

int[,] yr = new int[8, 2];
yr[0,0] = (icuryr + 0) + '-' + (icuryr + 1);
yr[0,1] = 1;
yr[1,0] = (icuryr - 2);
yr[1,1] = 2;
yr[2,0] = (icuryr - 3);
yr[2,1] = 3;
yr[3,0] = (icuryr - 4);
yr[3,1] = 4;
yr[4,0] = (icuryr - 5);
yr[4,1] = 5;
yr[5,0] = (icuryr - 6);
yr[5,1] = 6;
yr[6,0] = (icuryr - 7);
yr[6,1] = 7;
yr[7,0] = (icuryr + 1) + '-' + (icuryr - 4);
yr[7,1] = 8;

DataTable dtYear = new DataTable();
// Here we create a DataTable with five columns.
DataColumn Year = new DataColumn("SelectedYear", typeof(string));
DataColumn Value = new DataColumn("Value", typeof(int));

DataRow dr;
for (int i = 0; i < yr.GetUpperBound(0); i++)
{
dr = dtYear.NewRow();
dr["SelectedYear"] = yr[i, 0].ToString();
dr["Value"] = yr[i, 1].ToString();
}

DropDownList1.DataSource = dtYear;
DropDownList1.DataTextField = "SelectedYear";
DropDownList1.DataValueField = "Value";
DropDownList1.DataBind();
}
``````
Hope this helps you.
0

Author Comment

Hi,
1.
yr[0, 0] = (icuryr + 0) + '-' + (icuryr + 1);this value stored in the array as 4070.
yr[7, 0] = (icuryr + 1) + '-' + (icuryr - 4);this value stored in the array as 4066.
There is something wrong here.These years are not showing in the DropDownList.

2.The DropDownList is in GridView.
<asp:TemplateField>
Year
<ItemTemplate>
<asp:Label ID="lblYear" runat="server" Text='<%# Bind("Year") %>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:DropDownList ID="cmbYr" DataTextField="Year" runat="server">
</asp:DropDownList>
</EditItemTemplate>
</asp:TemplateField>

I am populating the dropdown list in RowDataBound event.When I click on the edit button,it is showing the DropDownList,Update and Cancel butttons.When cancel button is clicked it is showing the following error.

Failed to load viewstate.  The control tree into which viewstate is being loaded must match the control tree that was used to save viewstate during the previous request.  For example, when adding controls dynamically, the controls added during a post-back must match the type and position of the controls added during the initial request.

protected void gview_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
{
gview.EditIndex = -1;
DataTable dtData= (DataTable)ViewState["dtData"];
gview.DataSource = dtData;
gview.DataBind();
}

If I used DropDownList in ItemTemplate and not using the EditItemTemplate then it is working fine.
I want to use both Item Template and EditItemTemplate.Label in Itemtemplate and DropDownList is EditItemTemplate.

How to make it work.

Thanks
0

LVL 16

Assisted Solution

hey, i used your concept only.

what is the concept involved in  yr[0][0] = (icuryr + 0)+ '-' + (icuryr + 1); ?

if icuryr = 2012 then should yr[0][0] be displayed like this 2012 - 2013 ?
0

LVL 16

Assisted Solution

change yr array's datatype to string. it will solve the year display problem.

``````public void Load2DArrayToDT()
{
DateTime dt = DateTime.Now;
int icuryr = dt.Year;
String curyr = dt.Year.ToString();

int curyear = icuryr + 0;
int fuyear = icuryr + 1;
int p1year = icuryr - 1;
int p2year = icuryr - 2;

string[,] yr = new string[8, 2];
yr[0, 0] = (icuryr + 0) + " - " + (icuryr + 1);
yr[0, 1] = "1";
yr[1, 0] = (icuryr - 2).ToString();
yr[1, 1] = "2";
yr[2, 0] = (icuryr - 3).ToString();
yr[2, 1] = "3";
yr[3, 0] = (icuryr - 4).ToString();
yr[3, 1] = "4";
yr[4, 0] = (icuryr - 5).ToString();
yr[4, 1] = "5";
yr[5, 0] = (icuryr - 6).ToString();
yr[5, 1] = "6";
yr[6, 0] = (icuryr - 7).ToString();
yr[6, 1] = "7";
yr[7, 0] = (icuryr + 1).ToString() + " - " + (icuryr - 4).ToString();
yr[7, 1] = "8";

DataTable dtYear = new DataTable();

DataColumn Year = new DataColumn("SelectedYear", typeof(string));
DataColumn Value = new DataColumn("Value", typeof(int));

DataRow dr;
for (int i = 0; i < yr.GetUpperBound(0); i++)
{
dr = dtYear.NewRow();
dr["SelectedYear"] = yr[i, 0].ToString();
dr["Value"] = yr[i, 1].ToString();
}

DropDownList1.DataSource = dtYear;
DropDownList1.DataTextField = "SelectedYear";
DropDownList1.DataValueField = "Value";
DropDownList1.DataBind();
}
``````
for the viewstate issue check the below links

http://forums.asp.net/t/1295517.aspx/1

0

Author Comment

Hi,
1.yr[0, 0] = (icuryr + 0).ToString() + '-' + (icuryr + 1).ToString();this value is not storing in the DropDownList.Why that Value is not loading.
I am assigning the value to DropDownList in the gview_RowDatabound event.

2.The GridView has CommandField. When I click on the Edit button it will show the update and cancel button. When update button is clicked the dropdown list is empty. Why it is not showing the selected value and populated values in the Ipdate button click.

{

DateTime dt = DateTime.Now;
int icuryr = dt.Year;
String curyr = dt.Year.ToString();
int curyear = icuryr + 0;
int fuyear = icuryr + 1;
int p1year = icuryr - 1;
int p2year = icuryr - 2;
string[,] yr = new string[11, 2];
yr[0, 0] = (icuryr + 0).ToString() + '-' + (icuryr + 1).ToString();
yr[0, 1] = "1";
yr[1, 0] = (icuryr + 0).ToString() + '-' + (icuryr + 1).ToString();
yr[1, 1] = "2";
yr[2, 0] = (icuryr - 1).ToString() + '-' + (icuryr - 2).ToString();
yr[2, 1] = "3";
yr[3, 0] = (icuryr + 1).ToString() + '-' + (icuryr - 4).ToString();
yr[3, 1] = "4";
yr[4, 0] = (icuryr - 1).ToString();
yr[4, 1] = "5";
yr[5, 0] = (icuryr - 2).ToString();
yr[5, 1] = "6";
yr[6, 0] = (icuryr - 3).ToString();
yr[6, 1] = "7";
yr[7, 0] = (icuryr - 4).ToString();
yr[7, 1] = "8";
yr[8, 0] = (icuryr - 5).ToString();
yr[8, 1] = "9";
yr[9, 0] = (icuryr - 6).ToString();
yr[9, 1] = "10";
yr[10, 0] = (icuryr - 7).ToString();
yr[10, 1] = "11";

DataTable dtYear = new DataTable();
// Here we create a DataTable with two columns.
DataColumn Year = new DataColumn("SelectedYear", typeof(string));
DataColumn Value = new DataColumn("Value", typeof(int));

DataRow dr;
for (int i = 0; i <= yr.GetUpperBound(0); i++)
{
dr = dtYear.NewRow();
dr["SelectedYear"] = yr[i, 0].ToString();
dr["Value"] = yr[i, 1].ToString();
}

drpLst.DataSource = dtYear;
drpLst.DataTextField = "SelectedYear";
drpLst.DataValueField = "Value";
drpLst.DataBind();

}

protected void gview_RowDatabound(object sender, GridViewRowEventArgs e)
{

GridViewRow gRow = e.Row;
if (gRow.RowType == DataControlRowType.DataRow)
{
DataRowView drv = (DataRowView)grow.DataItem;
Control ctrl = gRow.FindControl("cmbYear");
if (ctrl != null)
{
DropDownList dd = ctrl as DropDownList;
dd.SelectedItem.Text = drv["MYear"].ToString();

}

}

}

.aspx
<asp:TemplateField>
Year
<ItemTemplate>
</asp:DropDownList>

</ItemTemplate>
<EditItemTemplate>
<asp:DropDownList ID="cmbYear"  runat="server">
</asp:DropDownList>
</EditItemTemplate>
</asp:TemplateField>

I used DropDownList both in ItemTemplate and EditItemTemplate.So ViewState problem is not showing up.

Thanks.
0

LVL 16

Assisted Solution

Hi,

The problem is because of the hypen with single quotes. for that i have already updated the code for the hyphen with a space and a double quotes, see the below code

``````yr[0, 0] = (icuryr + 0) + " - " + (icuryr + 1);
``````
same way, can you update your code like below and check

``````yr[0, 0] = (icuryr + 0).ToString() + " - " + (icuryr + 1).ToString();
yr[0, 1] = "1";
yr[1, 0] = (icuryr + 0).ToString() + " - " + (icuryr + 1).ToString();
yr[1, 1] = "2";
yr[2, 0] = (icuryr - 1).ToString() + " - " + (icuryr - 2).ToString();
yr[2, 1] = "3";
yr[3, 0] = (icuryr + 1).ToString() + " - " + (icuryr - 4).ToString();
``````
by updating the code you should have the dropdownlist structure like below

2012 - 2013
2010
2009
2008
2007
2006
2005

Binding the data to grid, just display the year value in a label to the itemtemplate and bind the year values to the dropdownlist in edititemtemplate.

and the binding process is not in a correct way for the edit command.

see my working code attached below

Design Page (EE_BindDDLInGrid.aspx)

``````<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false"
AutoGenerateEditButton="true" onrowcancelingedit="GridView1_RowCancelingEdit"
onrowdatabound="GridView1_RowDataBound" onrowediting="GridView1_RowEditing">
<Columns>
<ItemTemplate>
<asp:Label ID="lblYear" runat="server" Text=""><%# Eval("Year") %></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:DropDownList ID="cmbYear" runat="server">
</asp:DropDownList>
</EditItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
``````
Code Behind (EE_BindDDLInGrid.aspx.cs)

``````using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;

public partial class EE_BindDDLInGrid : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
BindGrid();
}
}

public void BindGrid()
{
DataTable dt = new DataTable();

DataRow dr;

dr = dt.NewRow();
dr["Year"] = "2010";

dr = dt.NewRow();
dr["Year"] = "2008";

dr = dt.NewRow();
dr["Year"] = "2009";

GridView1.DataSource = dt;
GridView1.DataBind();
}

protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
{
GridView1.EditIndex = e.NewEditIndex;
BindGrid();
}

protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
{
GridView1.EditIndex = -1;
BindGrid();
}

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
// While Edit
if ((e.Row.RowState == (DataControlRowState.Edit | DataControlRowState.Alternate)) || (e.Row.RowState == DataControlRowState.Edit))
{
DropDownList ddl = (DropDownList)e.Row.FindControl("cmbYear");
}
}
}

{
DateTime dt = DateTime.Now;
int icuryr = dt.Year;
String curyr = dt.Year.ToString();

int curyear = icuryr + 0;
int fuyear = icuryr + 1;
int p1year = icuryr - 1;
int p2year = icuryr - 2;

string[,] yr = new string[11, 2];
yr[0, 0] = (icuryr + 0).ToString() + " - " + (icuryr + 1).ToString();
yr[0, 1] = "1";
yr[1, 0] = (icuryr + 0).ToString() + " - " + (icuryr + 1).ToString();
yr[1, 1] = "2";
yr[2, 0] = (icuryr - 1).ToString() + " - " + (icuryr - 2).ToString();
yr[2, 1] = "3";
yr[3, 0] = (icuryr + 1).ToString() + " - " + (icuryr - 4).ToString();
yr[3, 1] = "4";
yr[4, 0] = (icuryr - 1).ToString();
yr[4, 1] = "5";
yr[5, 0] = (icuryr - 2).ToString();
yr[5, 1] = "6";
yr[6, 0] = (icuryr - 3).ToString();
yr[6, 1] = "7";
yr[7, 0] = (icuryr - 4).ToString();
yr[7, 1] = "8";
yr[8, 0] = (icuryr - 5).ToString();
yr[8, 1] = "9";
yr[9, 0] = (icuryr - 6).ToString();
yr[9, 1] = "10";
yr[10, 0] = (icuryr - 7).ToString();
yr[10, 1] = "11";

DataTable dtYear = new DataTable();

DataColumn Year = new DataColumn("SelectedYear", typeof(string));
DataColumn Value = new DataColumn("Value", typeof(int));

DataRow dr;
for (int i = 0; i < yr.GetUpperBound(0); i++)
{
dr = dtYear.NewRow();
dr["SelectedYear"] = yr[i, 0].ToString();
dr["Value"] = yr[i, 1].ToString();
}

ddl.DataSource = dtYear;
ddl.DataTextField = "SelectedYear";
ddl.DataValueField = "Value";
ddl.DataBind();
}
}
``````
Try this sample code and integrate it correctly in your code. Hope you could get this cleraly.
0

Author Comment

The problem is not because of Hypen with single quotes.I tried hyphen with space and double quotes.But still the drop down list is not showing 2012-2013.
we need to use nested for loop.

Whenever I am doing something with the grid The DropDownList becomes empty. I switched to TextBox instead of DropDownList.

0

Author Closing Comment

Thanks
0

## Featured Post

This article introduced a TextBox that supports transparent background.   Introduction TextBox is the most widely used control component in GUI design. Most GUI controls do not support transparent background and more or less do not have the…
User art_snob (http://www.experts-exchange.com/M_6114203.html) encountered strange behavior of Android Web browser on his Mobile Web site. It took a while to find the true cause. It happens so, that the Android Web browser (at least up to OS ver. 2.…
This video is in connection to the article "The case of a missing mobile phone (https://www.experts-exchange.com/articles/28474/The-Case-of-a-Missing-Mobile-Phone.html)". It will help one to understand clearly the steps to track a lost android phone.
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…