0

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

2 Answers 2

2

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.

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

8 Comments

Thanks King King, so how would I call a method to (populate) that column for each object in the list?
@redstubble I don't understand your question well, I think each object in a list is displayed in a row of a DataGridView whose DataSource is the list.
So I have a list of all my book objects from the datasource (_Author.Books.ToList();) but I wanted to add another column that I can call a method 'GetType()'. This will return a string of what kind of book it is. I wanted to place this information in the new column you helped me create.
Could you show me the class definition of your Book, I guess that's Book, the item type in your list. List type should have some Property describing what kind of book it is.
@redstubble please see my update and feel free to ask more :)
|
0

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>

4 Comments

how can I use this kind of scripting stuff in Winforms?
You can't use XAML scripting in WinForms, but You can use separate WPF control inside the WinForms
but the OP tagged his question as Winforms and not WPF and I don't think the OP wants a work around while he can solve it directly in Winforms.
I would like to solve the question in winforms without adding additional layers if possible.

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.