• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 446
  • Last Modified:

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

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.
 
 protected void LoadYears(DropDownList drpLst)
        {
            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));
            dtYear.Columns.Add(Year);
            DataColumn Value = new DataColumn("Value", typeof(int));
            dtYear.Columns.Add(Value);
         

        }

Thanks.
0
KavyaVS
Asked:
KavyaVS
  • 4
  • 4
4 Solutions
 
Rose BabuSenior Team ManagerCommented:
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));
	dtYear.Columns.Add(Year);
	DataColumn Value = new DataColumn("Value", typeof(int));
	dtYear.Columns.Add(Value);
	
	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();
		dtYear.Rows.Add(dr);
	}

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

Open in new window

Hope this helps you.
0
 
KavyaVSAuthor Commented:
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>
       <HeaderTemplate>
        Year
       </HeaderTemplate>
        <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
 
Rose BabuSenior Team ManagerCommented:
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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
Rose BabuSenior Team ManagerCommented:
change yr array's datatype to string. it will solve the year display problem.

updated Load2DArrayToDT function

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));
	dtYear.Columns.Add(Year);
	DataColumn Value = new DataColumn("Value", typeof(int));
	dtYear.Columns.Add(Value);

	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();
		dtYear.Rows.Add(dr);
	}

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

Open in new window

for the viewstate issue check the below links

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

http://stackoverflow.com/questions/5165384/failed-to-load-viewstate-the-control-tree-into-which-viewstate-is-being-loaded
0
 
KavyaVSAuthor Commented:
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.

protected void Load2DArrayToDT(DropDownList drpLst)
        {
       
            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));
            dtYear.Columns.Add(Year);
            DataColumn Value = new DataColumn("Value", typeof(int));
            dtYear.Columns.Add(Value);

            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();
                dtYear.Rows.Add(dr);
            }

            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;
                    Load2DArrayToDT(dd);
                    dd.SelectedItem.Text = drv["MYear"].ToString();

                }

            }

        }

.aspx
 <asp:TemplateField>
       <HeaderTemplate>
        Year
       </HeaderTemplate>
        <ItemTemplate>
     <asp:DropDownList ID="cmbYear"  disabled="disabled" ReadOnly="true" runat="server">
                                                        </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
 
Rose BabuSenior Team ManagerCommented:
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);

Open in new window

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();

Open in new window

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>                
		<asp:TemplateField HeaderText="Year">
			<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>

Open in new window

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();
        dt.Columns.Add("Year");

        DataRow dr;

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

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

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

        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");
                Load2DArrayToDT(ddl);
            }
        }
    }

    public void Load2DArrayToDT(DropDownList ddl)
    {
        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));
        dtYear.Columns.Add(Year);
        DataColumn Value = new DataColumn("Value", typeof(int));
        dtYear.Columns.Add(Value);

        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();
            dtYear.Rows.Add(dr);
        }

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

Open in new window

Try this sample code and integrate it correctly in your code. Hope you could get this cleraly.
0
 
KavyaVSAuthor Commented:
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.

Thanks for your help.
0
 
KavyaVSAuthor Commented:
Thanks
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 4
  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now