I print unidimensional array fine - it works.
http://aspsnippets.com/Articles/Binding-Arrays-to-GridView-in-ASP.Net.aspx
string[,] arr2D =
{
{ "John", "21" },
{ "Smith", "33" },
{ "Ryder", "15" },
{ "Jake", "18"},
{ "Tom","34" }
};
ArrayList arrList = new ArrayList();
for(int i=0;i<5;i++)
{
arrList.Add(new ListItem(arr2D[i, 0], arr2D[i, 1]));
}
Grid2D.DataSource = arrList;
Grid2D.DataBind();
But the problem is when I try to put "ArrayList", it does not exist in the context of the language. any idea how to fix this, or another method to do this. and i try with this
protected void Page_Load(object sender, EventArgs e)
{
if(!IsPostBack)
{
//enter code here
BindGridview();
}
}
private void BindGridview()
{
string[,] arrlist = {
{"Suresh", "B.Tech"},
{"Nagaraju","MCA"},
{"Mahesh","MBA"},
{"Mahendra","B.Tech"}
};
DataTable dt = new DataTable();
dt.Columns.Add("Name");
dt.Columns.Add("Education");
for (int i = 0; i < arrlist.GetLength(0);i++)
{
dt.Rows.Add();
dt.Rows[i]["Name"] = arrlist[i,0].ToString();
dt.Rows[i]["Education"] = arrlist[i,1].ToString();
}
gvarray.DataSource = dt;
gvarray.DataBind();
}
But DataTable
doensn't exist.
your code to fill the datatable is not correct - please try the below eg.
private void BindGridview()
{
string[,] arrlist = {
{"Suresh", "B.Tech"},
{"Nagaraju","MCA"},
{"Mahesh","MBA"},
{"Mahendra","B.Tech"}
};
DataTable dt = new DataTable();
DataRow dr = null;
dt.Columns.Add(new DataColumn("Name", typeof(string)));
dt.Columns.Add(new DataColumn("Education", typeof(string)));
//dr = dt.NewRow();
for (int i = 0; i < arrlist.GetLength(0);i++)
{
dr = dt.NewRow();
dr["Name"] = arrlist[i,0].ToString();
dr["Education"] = arrlist[i,1].ToString();
}
gvarray.DataSource = dt;
gvarray.DataBind();
}
Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
Dim str_array(,) As String ' 2D array declaration
str_array = {{"Suresh", "B.Tech"}, {"Nagaraju", "MCA"}, {"Mahesh", "MBA"}, {"Mahendra", "B.Tech"}} ' array initialization
For i As Integer = 0 To (str_array.Length / 2) - 1 'limit is set to this because the length includes both the indices and white space
gv.Rows.Add(str_array(i, 0), str_array(i, 1))
Next
End Sub
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.