1

I am trying to follow this tutorial on how to bind an array to a grid-view: Binding Arrays to GridView in ASP.Net

It is easy enough to follow, but my array structure looks different that the one in the example. I would like to find out how to write an array that looks like this:

{
  "users": [
    {
      "name": "aldo",
      "email": "[email protected]"
    },
    {
      "name": "matias",
      "email": "[email protected]"
    }
  ]
}

Any help would be great.

Thank you, Erasmo

1
  • are you looking to find the class structure? how to display it? can you show what you have tried so far ? Commented Jun 17, 2020 at 19:23

2 Answers 2

2

First thing to say is, this not a multidimensional array. It's an array of User objects. In C# you could implement it like this.

public class User
{
     public string Name {get; set;}
     public string Email {get; set;}
}

List<User> users = new List<User>
{
    new User { Name = "aldo", Email ="[email protected]"},       
    new User { Name = "matias", Email ="[email protected]"}
}

This would generate the JSON you've shown when serialized.

One slight thing to note is that C# uses Pascal-casing (Name,Email) whereas JavaScript and your JSON use Camel-casing (name, email).

Some JSON serializers (such the one for .NET Core WebAPI) automatically make this conversion, but other do not, so you may have to use an inappropriate convention in one language or the other.

Sign up to request clarification or add additional context in comments.

Comments

0

I've used code from the tutorial you mentioned and modified it in order to fit your requirements:

<asp:GridView ID="GridMultiD" runat="server"
    AutoGenerateColumns = "false" Font-Names = "Arial"
    Font-Size = "11pt" AlternatingRowStyle-BackColor = "#C2D69B" 
    HeaderStyle-BackColor = "green" AllowPaging ="true"  
    PageSize = "10" Caption = "Multi-Dimensional Array" >
   <Columns>
    <asp:BoundField ItemStyle-Width = "150px"
     DataField = "Name" HeaderText = "Name" />
    <asp:BoundField ItemStyle-Width = "150px"
     DataField = "Email" HeaderText = "Email" />
   </Columns>
</asp:GridView>  
//Multi-Dimensional Array
string[,] arrMultiD = {
                    { "aldo", "[email protected]" },
                    { "matias", "[email protected]"}
                 };
DataTable dt = new DataTable();
dt.Columns.Add("Name", Type.GetType("System.String"));
dt.Columns.Add("Email", Type.GetType("System.String"));

for (int i = 0; i < 2; i++)
{
    dt.Rows.Add();
    dt.Rows[dt.Rows.Count - 1]["Name"] = arrMultiD[i, 0];
    dt.Rows[dt.Rows.Count - 1]["Email"] = arrMultiD[i, 1];

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

1 Comment

Thank you for your example. I am working on implementing it, I am not very experinced so it takes me way longer than you to follow and understand what I need to do. I will update this question soon after I am done, or if I have a question.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.