Binding Struct to DataGrid

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • programmerboy
    New Member
    • Jul 2007
    • 84

    Binding Struct to DataGrid

    Hi I am returning an array of struct from my webservice. Is there a way to bind that array of struct to DataGrid. I am using C#. Following is my struct.

    [CODE=C#]//Struct to be sent
    public struct UserDataSent
    {
    public string userID , fName , lName , IMO , status, businessAddress , city, state, zip, telephoneNumber , mail;
    }[/CODE]
  • nateraaaa
    Recognized Expert Contributor
    • May 2007
    • 664

    #2
    Originally posted by programmerboy
    Hi I am returning an array of struct from my webservice. Is there a way to bind that array of struct to DataGrid. I am using C#. Following is my struct.

    [CODE=C#]//Struct to be sent
    public struct UserDataSent
    {
    public string userID , fName , lName , IMO , status, businessAddress , city, state, zip, telephoneNumber , mail;
    }[/CODE]
    I know that you can use an array or arraylist as the datasource of a datagrid. Could you add the items of the struct into an array and then use the array as the datasource of your datagrid?

    Nathan

    Comment

    • programmerboy
      New Member
      • Jul 2007
      • 84

      #3
      Originally posted by nateraaaa
      I know that you can use an array or arraylist as the datasource of a datagrid. Could you add the items of the struct into an array and then use the array as the datasource of your datagrid?

      Nathan
      Nathan,
      it still doesnt work. If I turn Auto Generate Column to false and try to bind every field that it says "A field or property with the name 'LName' was not found on the selected datasource.

      And if I turn Auto Generate columns to true and dont bind any column manually then it says

      System.Web.Http Exception: DataGrid with id 'dgCheck' could not automatically generate any columns from the selected data

      What I am doing wrong?

      Comment

      • Plater
        Recognized Expert Expert
        • Apr 2007
        • 7872

        #4
        Are you using a DataGrid or a DataGrid view?

        The datagridview's datasource objects seem to auto-create columns based on public methods and not public variables

        Such as:
        Code:
        public string MyString
        {
           get
           {
              return "hi there";
           }
        }
        Would create a column called "MyString"

        Comment

        • programmerboy
          New Member
          • Jul 2007
          • 84

          #5
          Originally posted by Plater
          Are you using a DataGrid or a DataGrid view?

          The datagridview's datasource objects seem to auto-create columns based on public methods and not public variables

          Such as:
          Code:
          public string MyString
          {
             get
             {
                return "hi there";
             }
          }
          Would create a column called "MyString"
          I am using datagrid. And also I have struct defined as public with public variables in it.

          Comment

          • cleanlines
            New Member
            • Jul 2007
            • 1

            #6
            As Plater said,

            You need to put properties inside your struct. The gridview binds to public properties, not fields.

            Code:
            //Struct to be sent
            public struct UserDataSent
            {
            public string userID , fName , lName , IMO , status, businessAddress, city, state, zip, telephoneNumber, mail;
            
            public string UserID {[INDENT]get { return userID; }
            }
            
            ...
            
            }
            It maybe possible to bind to fields using code attributes, but if it is I don't what the attribute is.

            Cheers,
            Graham

            Comment

            Working...