如何在GridView中打印多维数组?

时间:2021-12-12 21:27:24

I print unidimensional array fine - it works.

我打印一维数组很好 - 它的工作原理。

http://aspsnippets.com/Articles/Binding-Arrays-to-GridView-in-ASP.Net.aspx

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

但问题是当我尝试放置“ArrayList”时,它在语言的上下文中不存在。任何想法如何解决这个,或另一种方法来做到这一点。我试着用这个

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.

但DataTable并不存在。

2 个解决方案

#1


1  

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

#2


0  

  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

#1


1  

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

#2


0  

  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