0
votes

OK first time I feel I have had to post, lots of information out there on datagrids but nothing explains what I need in a way I understand.

        dataBooks.DataSource = null;
        dataBooks.AutoGenerateColumns = true;
        dataBooks.DataSource = _Author.Books.ToList();

This returns a list of objects, however I want to add another column in which I can call getType() which will return the book format.

I do not know how to bind data when I change autogeneratecolumns to false so I get a list of blanks. Be gentle, it maybe obvious to you but I am a newbie.

I would like to call a method GetBookType() which will return a string.

public abstract partial class Book
{
    public Book()
    {
        this.Orders = new HashSet<Order>();
    }

    public string AuthorName { get; set; }
    public string Title { get; set; }
    public double Price { get; set; }
    public int Quantity { get; set; }
    public int Year { get; set; }

    public virtual Author Author { get; set; }
    public virtual ICollection<Order> Orders { get; set; }
}

And the partial class which will return a string of type

public abstract partial class clsBook { public override string ToString() { return this.Title + "\t" + this.Year + "\t" + this.Price + "\t" + this.Quantity + "\t" + this.GetType(); }

    public abstract void EditDetails();

    public abstract string GetBookType();
2

2 Answers

2
votes

you don't need to set AutoGenerateColumns = false, just add columns manually to your DataGridView normally. I suppose you want to add a column named NewColumn, this column should bind to the data member NewColumData of your DataSource. Here is the code:

DataGridViewTextBoxColumn newColumn = new DataGridViewTextBoxColumn(){Name = "NewColumn", DataPropertyName="NewColumnData"};
dataBooks.Columns.Add(newColumn);
dataBooks.DataSource = _Author.Books.ToList();

Here is how the columns auto-generating works: all the DataMembers in the underlying DataSource will be looped through, if a DataMember is already bound to by some existing Column in your DataGridView, this column is used instead and there is not any new column created. In the code above, your existing column is NewColumn, this column is already bound to the NewColumnData of your DataSource via the Property DataPropertyName, so this column is used instead of creating a new column. All other columns are automatically created normally.

UPDATE

I don't understand why you want to have a method like GetBookType, if you want to display it in a column, just add some Property for that instead like this:

public abstract partial class Book
{
  public Book()
  {
    this.Orders = new HashSet<Order>();
  }
  //other properties of yours go here
  //--------------------------------
  //---------------------------------
  public string BookInfo {
       get {
          return this.Title + "\t" + this.Year + "\t" + this.Price + "\t" + this.Quantity + "\t" + this.GetType();
       }
  }
}
//Then you can bind your list to your DataGridView like this
DataGridViewTextBoxColumn newColumn = new DataGridViewTextBoxColumn(){Name = "NewColumn", DataPropertyName="BookInfo"};//Notice the BookInfo which is assigned as the DataPropertyName for your new column.
dataBooks.Columns.Add(newColumn);
dataBooks.DataSource = _Author.Books.ToList();

I hope this time, it is what you want.

0
votes

When AutoGenerateColumns is false, You must add DataGridTextColumn for each property, like this, where Name and BookType are the properties of the Book class.

<sdk:DataGrid Name="dataGrid" AutoGenerateColumns="False" SelectionMode="Single">
        <sdk:DataGrid.Columns>
            <sdk:DataGridTextColumn 
                Header="Book Name" 
                Binding="{Binding Name, Mode=TwoWay}"/>
            <sdk:DataGridTextColumn 
                Header="Book Type" 
                Binding="{Binding BookType, Mode=TwoWay}"/>
        </sdk:DataGrid.Columns>
    </sdk:DataGrid>